Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Embedding fonts with PDF-lib

Posted on 2002-04-11
6
3,136 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

840 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