Embedding SVG in PDFs with Embedded Fonts

I want to embed an SVG image in a PDF and create the font families specified in the SVG files text

using the correct fonts.

The fonts I am using are 'Lato' TTF fonts .

Here is the code I used to convert the fonts to native TCPDF format:

$variants = [
    'Black',
    'BlackItalic',
    'Bold',
    'BoldItalic',
    'Hairline',
    'HairlineItalic',
    'Heavy',
    'HeavyItalic',
    'Italic',
    'Light',
    'LightItalic',
    'Medium',
    'MediumItalic',
    'Regular',
    'Semibold',
    'SemiboldItalic',
    'Thin',
    'ThinItalic',
];

foreach ($variants as $variant) {
    \TCPDF_FONTS::addTTFfont(
        '/var/www/fonts/Lato-' . $variant . '.ttf',
        '',
        '',
        32,
        '/var/www/fonts/output/'
    );
}

      

And here is the code I used to create a test PDF with embedded SVG:

$svg_content = '<svg width="600px" height="800px">';
foreach ($variants as $key => $variant) {
    $svg_content .= '<text x="30" y="' . ( 30 * ( $key + 1 ) ) . '" fill="#ED6E46" font-size="20" font-family="\'Lato-' . $variant . '\'" text-anchor="start">The quick brown fox jumped over the lazy dog (' . $variant . ').</text>';
}
$svg_content .= '</svg>';

$pdf = new TCPDF();

$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->SetMargins(0, 0, -1, true);
$pdf->SetAutoPageBreak(false, 0);

$pdf->AddPage();

$pdf->AddFont('Lato-Black', '', '/var/www/fonts/output/latoblack.php');

$pdf->ImageSVG('@' . $svg_content, 0, 0, 300, 300, '', $align='', $palign='C', $border=0, false);

$pdf->Output(
    preg_replace('/[^A-Za-z0-9]/', '', $influencer->getName()) . '.pdf',
    'I'
);

      

As a result, I get that all the text is displayed in what I assume is Helvetica:

Latin fonts are not used!

If I add a line just before the call ImageSVG()

like this:

$pdf->SetFont('Lato-Black', '', null, '/var/www/fonts/output/latoblack.php');

      

Then all the text is rendered in Lato-Black:

All text displayed in Lato-Black

It looks to me like the fonts are embedded in the PDF, but SetFont

obviously sets the current font for subsequent PDF text elements, and the elements text

in the SVG image are rendered using that font.

My desired behavior is that the attributes font-family

defined on the SVG elements text

are done in PDF, so each line displays using the font variant specified in parentheses at the end of the line.

How can i do this?

+3


source to share


1 answer


You can start with the following code and work back, including your changes.

To be clear, I am not specifically promoting this as a way to do it, only that this is one way that works, and therefore should help as a starting point.

Font converter:  

$variants = [
    'Black',
    'BlackItalic',
    'Bold',
    'BoldItalic',
    'Hairline',
    'HairlineItalic',
    'Heavy',
    'HeavyItalic',
    'Italic',
    'Light',
    'LightItalic',
    'Medium',
    'MediumItalic',
    'Regular',
    'Semibold',
    'SemiboldItalic',
    'Thin',
    'ThinItalic',
];

foreach ($variants as $variant) {
    \TCPDF_FONTS::addTTFfont(dirname(__FILE__) . '/fonts/Lato-' . $variant . '.ttf', 'TrueTypeUnicode', '', 96);
}

echo 'Done!';

      

This places files .php

, .z

and .ctg.z

into /tecnickcom/tcpdf/fonts

.



PDF generator:

Note the different names font-family

in the array $variants

:   

$variants = [
    'lato',
    'latob',
    'latobi',
    'latoblack',
    'latoblacki',
    'latohairline',
    'latohairlinei',
    'latoheavy',
    'latoheavyi',
    'latoi',
    'latolight',
    'latolighti',
    'latomedium',
    'latomediumi',
    'latosemib',
    'latosemibi',
    'latothin',
    'latothini'
];

$svg_content = '<svg width="600px" height="800px">';

foreach ($variants as $key => $variant) {
    $svg_content .= '<text x="30" y="' . ( 30 * ( $key + 1 ) ) . '" fill="#ED6E46" font-size="20" font-family="\'' . $variant . '\'" text-anchor="start">The quick brown fox jumped over the lazy dog (' . $variant . ').</text>';
}

$svg_content .= '</svg>';

ob_start();

$pdf = new TCPDF();

$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->SetMargins(0, 0, -1, true);
$pdf->SetAutoPageBreak(false, 0);

$pdf->AddPage();
//$pdf->AddFont('latoblack', '', dirname(__FILE__) . '/fonts/output/latoblack.php');

$pdf->ImageSVG('@' . $svg_content, 0, 0, 300, 300, '', $align='', $palign='C', $border=0, false);

$pdf->Output('example_001.pdf', 'I');

      

Output: enter image description here

+2


source







All Articles