Solved

Embedding fonts with PDF-lib

Posted on 2002-04-11
6
3,109 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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 …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

759 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

21 Experts available now in Live!

Get 1:1 Help Now