transformDocument

transformDocument

ADVANCED / PREMIUM TRIAL

Transforms documents into other formats (DOCX, PDF, (X)HTML, DOC, RTF, PNG, TXT).

Description
public transformDocument (string $source, string $target [, string $method [, array $options]])

This method allows to transform a document, generated or not with phpdocx, into DOCX, PDF, HTML, DOC, ODT, RTF, PNG and TXT preserving, as much as possible, the original formatting options.

You may find more info regarding this method in the Conversion plugin section.

Parameters

source

Path to the document that you want to convert to a different format.

target

Path to the resulting transformed document (PDF, HTML, XHTML, DOCX, DOC, ODT, RTF, PNG or TXT).

method

Method used to transform the document: 'libreoffice', 'native', 'msword', 'openoffice'

'libreoffice' method options

The possible keys and values are:

Key Type Description
comments bool False by default. Export comments. Check the macros-libreoffice folder included in Advanced and Premium packages to use this option.
debug bool False by default. Returns debug information about the conversion plugin.
escapeshellarg bool False by default. Applies escapeshellarg to escape source and LibreOffice path strings.
extraOptions string Extra parameters to be used when doing the conversion.
formsfields bool False by default. Export form fields. Check the macros-libreoffice folder included in Advanced and Premium packages to use this option.
homeFolder string Set a custom home folder to be used for the conversions.
lossless bool False by default. Lossless compression. Check the macros-libreoffice folder included in Advanced and Premium packages to use this option.
outdir string Set the outdir path. Useful when the PDF output path is not the same than the running script.
path string Sets the LibreOffice path. This option can be used instead of the path option in config/phpdocxconfig.ini.
pdfa1 bool False by default. Generate PDF/A-1 document. Check the macros-libreoffice folder included in Advanced and Premium packages to use this option.
pdfa2 bool False by default. Generate PDF/A-2 document. Check the macros-libreoffice folder included in Advanced and Premium packages to use this option.
pdfa3 bool False by default. Generate PDF/A-3 document. Check the macros-libreoffice folder included in Advanced and Premium packages to use this option.
toc bool False by default. If true updates the TOC before transforming the document. Check the macros-libreoffice folder included in Advanced and Premium packages to use this option.

'native' method options

The possible keys and values are:

Key Type Description
dompdf Dompdf dompdf instance.
dompdfVersion string

Sets dompdf version

  • null (default, autodetect)
  • 1 (dompdf 1.2.2 or previous version)
  • 2 (dompdf 2)
  • 3 (dompdf 3)
addHeadersAndFooters bool If true, add header/footer default type. Default as true.
includeBlankSpacesInEmptyParagraphs bool If true add a blank space to empty paragraphs. Useful to keep blank paragraphs if needed. Default as true.
margins array

Custom margins:

  • header
  • footer
stream bool If true, returns the document as stream. Default as false.
transformEmfImages bool If true and required extensions are available, transform EMF images. Default as true.

'msword' method options

The possible keys and values are:

Key Type Description
selectedContent string Scope: 'active' (default) or 'documents'.
toc bool False by default. If true updates the TOC before transforming the document.

'openoffice' method options

The possible keys and values are:

Key Type Description
debug bool False by default. Returns debug information about the conversion plugin.
homeFolder string Set a custom home folder to be used for the conversions.
odfconverter bool True by default. If set to false the conversion plugin does not use ODFConverter package. This may give better results in some cases.
tempDir string Set a custom temp folder to be used for the conversions.
version string 32-bit or 64-bit architecture. 32, 64 or null (default). If null autodetect.
Exceptions

Method not available.

Code samples

Example #1

The resulting output looks like:

Example #2

The resulting output looks like:

Release notes
  • phpdocx 16.0:
    • added "--norestore" to all conversions in the LibreOffice conversion plugin.
    • new documentation in the macros-libreoffice folder to enable and use lossless compression without adding a macro.
    • path and transformEmfImages options.
    • the escapeshellarg option applies to the LibreOffice path option.
    • EMF images support in the native conversion plugin.
    • merge fields in the native conversion plugin.
    • maximum value of 6 for headings in the native conversion plugin.
    • handle baseline vertAlign value in the native conversion plugin.
    • structured document tags in the native conversion plugin.
    • extra check not to apply a default cell margin if only table cell margin styles are applied in the native conversion plugin.
    • target value used in Content-Disposition when the stream mode is enabled in the native conversion plugin.
    • apply an absolute position to images set as back position only if behindDoc is active in the native conversion plugin.
    • PDF to PNG transformation allows setting a custom path for the target files distinct than the source folder in the native conversion plugin.
    • removed a deprecated message when a rowspan is not set in a table in the native conversion plugin.
  • phpdocx 15.5:
    • don't apply margin-top in textboxes that use absolute positions in the native conversion plugin.
    • landscape orientation in the native conversion plugin.
    • supported dompdf 3 in the native conversion plugin.
  • phpdocx 15.0:
    • PDF to DOCX, DOC, ODT, PNG and RTF in the LibreOffice conversion plugin.
  • phpdocx 14.5:
    • PDF to PNG in the native conversion plugin.
    • escapeshellarg option in the LibreOffice conversion plugin.
    • partial numbering styles in the native conversion plugin.
    • partial numbering styles in the native conversion plugin.
    • clean fillColor extra information in textboxes when Theme colors are used in the native conversion plugin.
    • majorHAnsi and minorHAnsi font family styles are parsed and applied from the asciiTheme attribute in the native conversion plugin.
    • basedOn table styles in the native conversion plugin.
    • exact tr height styles in tables in the native conversion plugin.
    • improved center and right alignments applied to tables in the native conversion plugin.
  • phpdocx 14.0:
    • conditional table styles and improvements transforming table styles without internal pPr and rPr styles in the native conversion plugin.
    • pdfa2 and pdfa3 options in the LibreOffice conversion plugin.
  • phpdocx 13.5:
    • supported dompdf 2 in the native conversion plugin.
    • supported AppImage format in the LibreOffice conversion plugin.
  • phpdocx 13.0:
    • headers and footers in the native conversion plugin.
    • replaced empty paragraphs with   paragraphs to avoid hiding them in the output in the native conversion plugin.
    • includeBlankSpacesInEmptyParagraphs option in the native conversion plugin.
    • replaced &nbsp; with<br> when the includeBlankSpacesInEmptyParagraphs is enabled to keep empty paragraphs in the native conversion plugin.
    • use &nbsp; to set tabs instead of a margin-left style in the native conversion plugin.
    • added #fff as border color in table styles when nil border is used in the native conversion plugin.
    • added UTF-8 as meta charset in the native conversion plugin.
    • removed CreateChartImageJpgraph and CreateChartImageEzComponents classes.
  • phpdocx 12.5:
    • comments, endnotes, and footnotes are added inside their own section tags with a specific class in the native conversion plugin.
    • simple fields: AUTHOR, COMMENTS, LASTSAVEDBY, TITLE in the native conversion plugin.
    • tables: cell padding styles in the native conversion plugin.
    • textboxes: margin-top, default border when none is set in the native conversion plugin.
    • tracked contents: ins and del tags in the native conversion plugin.
    • highlight style added as w:pPr/w:rPr style in the native conversion plugin.
  • phpdocx 12.0:
    • endnotes, footnotes and comments in the native conversion plugin.
    • w:vanish style in the native conversion plugin.
    • improved cell margins when only table margins are set in the native conversion plugin.
    • supported on/off, 1/0 and true/false values for w:pPr and w:rPr styles in the native conversion plugin.
    • improved hyperlinks, list margins and border styles in the native conversion plugin.
    • removed OdfConverter and openoffice folders from the package.
    • removed TCPDF as native conversion plugin method.
  • phpdocx 11.0:
    • supported LibreOffice 7 in the LibreOffice conversion plugin.
    • extraOptions option in the LibreOffice conversion plugin.
  • phpdocx 10.0:
    • new native conversion plugin based on DOMPDF.
  • phpdocx 9.0:
    • new native conversion plugin based on TCPDF.
    • comments, formsfields and lossless options in the LibreOffice conversion plugin.
    • toc option in the MS Word conversion plugin.
    • new method signature.
    • new MS Word conversion plugin.
  • phpdocx 8.0:
    • supported the toc option for DOCX targets in the LibreOffice conversion plugin.
    • supported LibreOffice 6 in the LibreOffice conversion plugin.
    • removed OpenOffice folder from the packages.
  • phpdocx 7.5:
    • DOCX to XHTML in the LibreOffice conversion plugin.
  • phpdocx 5.5:
    • improved working with the LibreOffice conversion plugin.
  • phpdocx 4.6:
    • toc option in the LibreOffice conversion plugin.
  • phpdocx 4.5:
    • new LibreOffice conversion plugin.