Good practices when working with font families
When working with Word documents you can choose any font family you want, besides the usual Calilbri or Times New Roman ones.
To see a font properly, it has to be either installed in the operating system or embedded in the document.
Setting fonts in the added contents
phpdocx has methods and options to set the font family of the document, either the default one for all contents, which you can set with the setDefaultFont method, or for specific parts of the content with the 'font' option, which is available in the methods to add texts.
For example, let's set Arial Narrow as the default font for all contents of the document. This would be the code:
To assign fonts with the methods of the phpdocx API, like texts for the main body, footnotes, endnotes or any other type of content, you must use the font parameter besides its family name. E.g.: the following code adds a text with the Arial font and another one with Times New Roman.
Adding new fonts to a DOCX
If you need to write using a specific font family but you are unsure if it will be available in the computers that will read it, you need to embed the font in the document.
phpdocx includes the embedFont method to embed fonts using Advanced and Premium licenses:
Fonts can also be embedded with MS Word generating a document with the fonts embedded in it. Then you can choose that document as a template for phpdocx. The embedded fonts must be TrueType or OpenType.
To embed fonts in a DOCX, just enable the MS Word option Embed fonts in the file. E.g.: You can find this option in MS Word 2010 in FILE > Save > Preserve fidelity when sharing this document > Embed fonts in the file.
After saving the document you can open it as a phpdocx template:
Now you can apply the new font families just like any other one:
Old versions of LibreOffice reads only one embedded font from a DOCX, others are ignored.
Fonts in HTML: @font-face rule
Premium licenses allow using HTML Extended and CSS Extended to download and embed TTF fonts from @font-face rules available in CSS styles.
To parse and embed TTF fonts from HTML, the following options must be set as true: useHTMLExtended and embedFonts.
The following code embeds Lato-Regular.ttf in the DOCX and sets it as font family of the paragraph:
embedHTML and replaceVariableByHTML methods support this feature.
Fonts in the conversion plugin
The conversion plugin, available in the Advanced and Premium licenses, works with the font families available in the operating system. This way, to keep the fonts in the resulting PDF and other compatible formats, they must be installed.
To install a new font in Windows is as easy as to double click in the file that contains the font and then accept the installation.
To install new fonts in Linux, just copy the fonts in the file system directory where the rest of the fonts are, e.g., /usr/share/fonts/truetype. After that, refresh the available fonts by executing:
To see the available fonts list, type the folowing command:
To use regular MS Word like Arial or Courier, it is advisable to download and install the msttcorefonts (ttf-mscorefonts-installer) package, which you can find in the majority of Linux distributions, instead of manually installing them.
To install fonts in Mac, double click in the file that contains the font (the one with a .ttf extension). Mac then will open the Font Book application, where you can see a preview of the font and its possible variations (italic, bold and so on). Click the Install font button and you're done.
Fonts in the conversion plugin, native method
The native conversion plugin included in phpdocx uses the fonts available in the DOMPDF installation used to transform the documents to PDF. It is possible to include additional fonts as explained on the DOMPDF documentation: https://github.com/dompdf/dompdf/wiki/About-Fonts-and-Character-Encoding
Thanks to the Indexer class, it is possible to obtain dinamically the list of fonts used in the DOCX.
If a text font present in a document is not available, a default one is chosen.