Solved

Embedding fonts with PDF-lib

Posted on 2002-04-11
6
3,175 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
[X]
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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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.
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…

615 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