Solved

Embedding fonts with PDF-lib

Posted on 2002-04-11
6
3,117 Views
Last Modified: 2013-12-03
Could somebody explain to me how to embed a font into the pdf using pdf-lib?

Here's some simple code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<body>
<?
$filename = "batalf.pdf";
$f = fopen($filename, "w");
$pdf = PDF_open($f);
pdf_set_parameter($pdf, "resourcefile", "/pathtoresourcefile/pdflib.upr");
PDF_begin_page($pdf, "500","800");
PDF_set_font($pdf, "Sabon-Roman" , 14, "host");
PDF_set_text_rendering($pdf, 0);
pdf_show_xy($pdf,"Hei",200,200);
PDF_end_page($pdf);
PDF_close($pdf);
fclose($f);
?>
<a href="<? echo $filename; ?>" target="_blank"><? echo $filename; ?></a>
</body>
</html>

The font Sabon-Roman is on the server.

When I try to open this file in Acrobat Reader and clicks file->document properties->fonts the system is telling me that the original font was "Sabon Roman", while the actual font is "Adobe Serif MM". In other words: The font isn't embedded into the pdf.

Hopefully, somebody has a solution to this problem.

Batalf
0
Comment
Question by:Batalf
  • 2
  • 2
  • 2
6 Comments
 
LVL 4

Accepted Solution

by:
lokeshv earned 100 total points
ID: 6934601
from php.net

For using pdf_findfont with pdf_setfont it's a good idea to copy your fonts
to a separate directory, i.e. /usr/local/fonts. You should also copy the
upr file which comes with PDFlib to this directory. The example code above
did not work here until I put a

pdf_set_parameter($pdf, "resourcefile",
"/usr/local/fonts/pdflib.upr");

before the line with pdf_findfont().




and use pdf_findfont somthing like..


$font = pdf_findfont($pdf, "Times New Roman", "host",
1);
if ($font) {
    pdf_setfont($pdf, $font, 30);
}



hope this helps..

Lk


0
 
LVL 32

Author Comment

by:Batalf
ID: 6935065
I've read this article from the manual my self before I posted this question, but we only have pdf-lib 3.06 installed on our server, and there you'll have to use pdf_set_font().

Now, I can see that pdf_set_font() has a last parameter for "embedding" font. When I'm trying to make use of that one, I got this error-message from the server:

” PHP Fatal error:  Internal pdflib error: Outline data for font 'Helvetica' not found in /data/dev/tmp_pdf.html on line 17”

Line 17 is this one:

pdf_set_font($pdf,"Helvetica",14,"host",1);

Anyone familiar with this problem?

Batalf




0
 
LVL 5

Expert Comment

by:dkjariwala
ID: 6935956
Actually my experience was that you specify the file name instead of font name. So try like,


pdf_set_font($pdf,"font.ttf",14,"host",1);


Where font.ttf is the filename.

JD

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:dkjariwala
ID: 6935957
Actually my experience was that you specify the file name instead of font name. So try like,


pdf_set_font($pdf,"font.ttf",14,"host",1);


Where font.ttf is the filename.
Place font.ttf in script's directory.

JD

0
 
LVL 32

Author Comment

by:Batalf
ID: 6959507
I didn't use your comment, but I'm going to give you the points as thanks for trying.

The solution to my problem was to set the paramter "1" on function

pdf_set_font();

The error-messages I got about font files, appeared because some font-files were missing on the server.

Batalf
0
 
LVL 4

Expert Comment

by:lokeshv
ID: 6960221
Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now