Solved

Embedding fonts with PDF-lib

Posted on 2002-04-11
6
3,126 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

770 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