Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain font data to embed in a PDF?

My MFC application creates a PDF and now I'm trying to embed fonts, the PDF created opens in Foxit, Chrome, and the Windows 8 PDF Reader but doesn't in Acrobat.

I tried validating the pdf file with this online validator and it says

The object's identity 6 doesn't match with the object's reference identity 5.

The embedded font program 'Candara' cannot be read.


I think the way I am embedding it's correct

3 0 obj
<</Type /Font
    /Subtype /TrueType
    /BaseFont /Candara
    /FirstChar 0
    /LastChar 255
    /Widths 4 0 R
    /FontDescriptor 5 0 R
>>
endobj

5 0 obj
<<
    /Type /FontDescriptor
    /FontName /Candara
    /Flags 32
    /FontBBox [-700 -500 1800 1500]
    /ItalicAngle 0
    /Ascent 12
    /Descent -4
    /CapHeight 8
    /StemV 109
    /FontFile2 6 0 R
>>
endobj

6 0 obj
<<
    /Length 100376
    /Length1 100376
>>
stream
    ... font bytes ...
endstream
endobj


I believe my problem it is with the font data which is obtained with

LONG ret = ::TTEmbedFont(pDC->GetSafeHdc(),
                    TTEMBED_TTCOMPRESSED, 
                    CHARSET_UNICODE,
                    &ulPrivStatus,
                    &ulStatus,
                    WriteEmbedProc,
                    lpvVecBytes,
                    nullptr,
                    0,
                    0,
                    nullptr);

I also tried using TTEMBED_RAW and TTEMBED_EMBEDEUDC



Sorry for the long and maybe vague explanation but I'm kinda lost,

So Is this the right way to obtain the font data to embed? If it is, should I applied a filter to the Font file object?

EDIT: I Changed my application to use GetFontData which returns the bytes of the font file and according to my research it is ok to use like this, But I still can't open in Acrobat and the online validation site still gives me the same error message.

link to an example PDF with my issue.

Thanks and any help will be appreciated!

like image 254
Penachia Avatar asked Oct 31 '25 18:10

Penachia


1 Answers

The font emedding itself look ok. There's actually a lower-level error fetching the font objects.

Attempting the view the PDF with Xpdf gives an error with the cross reference table: xref num 5 not found but needed, try to reconstruct<0a>.

Looking at the cross reference table at the end of the PDF:

xref 0 10 0000000000 65535 f 0000242485 00000 n 0000242426 00000 n 0000000016 00000 n 0000000155 00000 n 0000001081 00000 n % Wrong should be 0000241738 0000001081 00000 n
0000241929 00000 n 0000242056 00000 n 0000242353 00000 n

The actual error is on line 6 of the cross reference table, which contains the byte offset for object 6 0 R, instead of 5 0 R.

The message from the online validator The object's identity 6 doesn't match with the object's reference identity 5. is caused by the online validator trying to fetch object 5 0 R via the index, but actually getting object 6 0 R.

Cross reference table for this PDF needs to be fixed.

like image 197
dwarring Avatar answered Nov 02 '25 09:11

dwarring