tecnickcom / TCPDF

Official clone of PHP library to generate PDF documents and barcodes
https://tcpdf.org
Other
4.22k stars 1.52k forks source link

RobotoSans truetype font does not get converted properly. #618

Open catalystfd opened 1 year ago

catalystfd commented 1 year ago

I've been attempting to embed the RobotoSans font family from https://fonts.google.com/specimen/Roboto+Slab into a Moodle LMS distribution.

Specifically in this case just the regular size font from the static distribution.

I've converted it using the recommended process of calling addTTFfont passing the truetype unicode parameter.

<?php 
include('../tcpdf.php');
$pdf = new TCPDF_FONTS('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->addTTFfont('./RobotoSlab-Regular.ttf', 'TrueTypeUnicode', '', 32);

run from the CLI to generate the required font files.

This font can then be selected from the UI as required

However when you generate a test pdf, although the font is supposedly embedded, it does not appear to be a correctly formatted stream

Example PDF test-14.pdf

Output of PDF fonts

pdffonts test-14.pdf 
name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
AAAAAB+FreeSerif                     CID TrueType      Identity-H       yes yes yes      3  0
AAAAAC+RobotoSlab-Regular            CID TrueType      Identity-H       yes yes yes      4  0
AAAAAD+Roboto-Bold                   CID TrueType      Identity-H       yes yes yes      5  0

If you try to open it in something like ghostscript you can see that the embedded font stream breaks

gs test-14.pdf 
GPL Ghostscript 9.50 (2019-10-15)
Copyright (C) 2019 Artifex Software, Inc.  All rights reserved.
This software is supplied under the GNU AGPLv3 and comes with NO WARRANTY:
see the file COPYING for details.
Processing pages 1 through 1.
Page 1
   **** Error: can't process embedded font stream,
        attempting to load the font using its name.
               Output may be incorrect.
Can't find CID font "RobotoSlab-Regular".
Attempting to substitute CID font /Adobe-Identity for /RobotoSlab-Regular, see doc/Use.htm#CIDFontSubstitution.
The substitute CID font "Adobe-Identity" is not provided either. attempting to use fallback CIDFont.See doc/Use.htm#CIDFontSubstitution.
Loading a TT font from /usr/share/ghostscript/9.50/Resource/CIDFSubst/DroidSansFallback.ttf to emulate a CID font Adobe-Identity ... Done.

Here are the generated versions of the php and font files from the convert script robotoslab.zip

The bundled TCPDF version in moodle is v6.5.0 (2022-08-12)

and i'm generating using PHP 7.4.32 (cli) (built: Sep 29 2022 14:27:24) ( NTS )

The normal roboto bold font was embeddable just fine, so I'm wondering what might be different about RobotoSlab here that could explain the difference.

catalystfd commented 1 year ago

I should add that the text is added via code that looks like this

 $pdf->setFont('robotoslab', "", 60);
$pdf->writeHTMLCell(296.0001, 0, 1, 0, "The quick brown fox jumps over the lazy dog", 0, 0, false, true, "C");

where font would be 'robotoslab" and attr an empty string "" for the regular font.