embedFont

embedFont

ADVANCED / PREMIUM TRIAL

Embeds a TTF font in a Word document.

Description
public embedFont (string $fontSource, string $fontName, [array $options])
Parameters

fontSource

TTF font path.

fontName

Font name.

options

The possible keys and values of this array are:

Key Type Description
charset string 00 as default.
styleEmbedding string Regular (default), Bold, Italic, BoldItalic.
Exceptions

Method not available.

Code samples

Example #1

x
 
1
// embed a font and add new text contents applying the embedded font
2
require_once 'classes/CreateDocx.php';
3
4
$docx = new CreateDocx();
5
6
// embed a TTF font
7
$docx->embedFont('Pacifico.ttf', 'Pacifico');
8
9
// add a text content using the default font
10
$docx->addText('Text using the default font.');
11
12
// add a text content using the embedded font
13
$paragraphOptions = array(
14
    'font' => 'Pacifico',
15
);
16
$docx->addText('Text using the new font.', $paragraphOptions);
17
18
// generate the DOCX
19
$docx->createDocx('output');
20

The resulting Word document looks like:

Example #2

28
 
1
// embed two fonts and add HTML applying the embedded fonts
2
require_once 'classes/CreateDocx.php';
3
4
$docx = new CreateDocx();
5
6
// embed TTF fonts
7
$docx->embedFont('Pacifico.ttf', 'Pacifico');
8
$docx->embedFont('KOMIKRAK.ttf', 'Komika Krak');
9
10
// import HTML applying the new fonts to some contents
11
$hml = '
12
    <style>
13
        .nf {
14
            font-family: "Pacifico";
15
        }
16
        .nfs {
17
            font-family: "Komika Krak";
18
        }
19
    </style>
20
    <p>Text content using the default font family when importing HTML.</p>
21
    <p class="nf">Text content using Pacifico font.</p>
22
    <p class="nf">My text content <span class="nfs">Komika Krak</span></p>
23
';
24
25
$docx->embedHTML($hml);
26
27
$docx->createDocx('output');
28

The resulting Word document looks like:

Release notes
  • phpdocx 13.0:
    • updated the internal algorithm.
  • phpdocx 11.0:
    • prevent adding duplicated fonts.
    • fixed misspelled styleEmbedding option.
  • phpdocx 10.0:
    • new method.
­
­