Modifying PDF and reuse embedded font

1.9k views Asked by At

I want to modify a PDF document with PHP. I found the libs FPDF and FPDI which allows to create and modify PDF files. Here is my very simple code:

<?php
require_once('include/fpdf.php');
require_once('include/fpdi.php');

// initiate FPDI
$pdf = new FPDI();
// add a page
$pdf->AddPage();
// set the sourcefile
$pdf->setSourceFile('input.pdf');
// import page 1
$page1 = $pdf->importPage(1);
// insert the page
$pdf->useTemplate($page1);

// now write some text above the imported page
$pdf->AddFont('calibri');
$pdf->SetFont('calibri','',11);
$pdf->Write(0, "This is just a simple text");

header("Content-Type: application/pdf");
$pdf->Output();

That works very well, but I see that the inserted text adds something like a reference to the system font instead of the already embedded font. How can I simply reuse the embedded font?

If that is not possible with that libs feel free to point me to a third free lib.


I know that I can just use characters which are already used, but this is no problem in my special case. I have checked that all possible characters are used in the right case sensitivity.

2

There are 2 answers

2
Kurt Pfeifle On BEST ANSWER

I bet your embedded font from the imported PDF page is not called calibri. (Lower-case only spelling of fontnames in PDFs is very rare.)

And I also bet, that the PDF will not have Calibri embedded as a full set. It's rather likely that it is a subset only. And as a subset, the fontname will be composed of a random 6 letter uppercase prefix + the original font name, like this:

 AXBTZV+Calibri

You have to find that exact name and try with this. (However, I'm not sure if how your PHP library works, if it can do that at all, and if it would handle the modification of the PDF text writing code in the correct way. Gimme the PDF and I most likely can do it in a Text Editor, though...)

2
cweiske On

Reusing fonts in PDFs is mostly not possible since only the characters that are used in the PDF are stored. So if you never used an uppercase W in your PDF template and want to add text with one, then it cannot be displayed.

This does not answer your question but rather tells you not to try it at all.