Hudzilla.org - the homepage of Paul Hudson
Contents > Multimedia > Creating PDF documents Wish List | Report Bug | About Me ]

11.4.2     Getting started: pdf_new(), pdf_open_file(), pdf_findfont(), pdf_begin_page(), pdf_setfont(), pdf_show_xy(), pdf_end_page(), pdf_close(), and pdf_delete()

This is NOT the latest copy of this book; click here for the latest version.

resource pdf_new ( void )

bool pdf_open_file ( resource pdfdoc [, resource pdfdoc])

int pdf_findfont ( resource pdfdoc, string fontname, string encoding [, int embed])

bool pdf_begin_page ( resource pdfdoc, float width, float height)

bool pdf_setfont ( resource pdfdoc, string font, float size)

bool pdf_show_xy ( resource pdfdoc, string text, float x, float y)

bool pdf_end_page ( resource pdfdoc)

bool pdf_close ( resource pdfdoc)

bool pdf_delete ( resource pdfdoc)

Creating a PDF document is very much like creating a Flash movie or a picture in that to get the desired end result you state the list of drawing actions required to get there - drawing lines, text, adding fonts, etc. As in the prior two articles, you need to track the PDF document you are working with at all times as each function uses it. Let's get started in the usual manner - with a big chunk of PHP!

As you might have guessed by the long chapter title, creating even a simple PDF takes quite a few functions, so I suggest you make yourself a fresh cup of coffee before continuing.

<?php
    $pdf
= pdf_new();
    
pdf_open_file($pdf, "/path/to/your.pdf");
    
$font = pdf_findfont($pdf, "Times-Roman", "host");

    
pdf_begin_page($pdf, 595, 842);
    
pdf_setfont($pdf, $font, 30);
    
pdf_show_xy($pdf, "Printing text is easy", 50, 750);
    
pdf_end_page($pdf);

    
pdf_close($pdf);
    
pdf_delete($pdf);
?>

Starting at line one, we use pdf_new() to create a new PDF document and store it in $pdf. This value will be used in all the subsequent functions, so it is important to keep.

pdf_open_file() is used to open a file for writing. Note that the free version of PDFlib does not allow alteration of existing PDFs; this function merely creates a new PDF of the given filename. Naturally it will need to be somewhere your web server is able to write to, otherwise you will receive an error along the lines of "Fatal error: PDFlib error: function 'PDF_set_info' must not be called in 'object' scope in script.php on line XYZ"

The next line uses pdf_findfont() to find and load a font for use inside the generated PDF file. In the example, pdf_findfont() takes three parameters - the PDF document to work with, the name of the font to use, and which encoding to use. In the example above, $pdf is specified as the first parameter (as always). "Times-Roman" is specified as the font to use, which is one of the fourteen standard internal PDFlib fonts. The next parameter can be set to either "winansi" (Windows), "macroman" (Macintosh), "ebcdic" (EBCDIC code page 1047 machines), "builtin" (for symbol fonts), or "host" (winansi for Windows, macroman for Macintosh, etc). Generally it is smart to use "host", and leave PDFlib to do the hard work, so this is what is used in the code above.

pdf_findfont(), when successful, returns a font resource which is stored in $font. You may wish to add error-checking in your own scripts to add extra reliability.

At this point, we're ready to start on the main part of PDF generation. The first three lines merely set things up for the document - the next four, lines four to seven, are the page itself.

Reading the source, it is fairly easy to see that line four and line seven encapsulate one page in the generated PDF file. Objects and text outputted between a pdf_begin_page() and pdf_end_page() will affect that page, and multiple begin/end blocks are used to created multiple pages.

Note that pdf_begin_page() takes a second and third parameter which specify the X and Y point size of this page. The PDF format allows you to make your pages different point sizes from page to page - commonly used sizes are listed later on.

pdf_setfont() takes three parameters - the first is as per usual, the second parameter is the return value from pdf_findfont for the font you wish to use, and the final parameter is the size to use, in points. Immediately afterwards, we call pdf_show_xy() to place text into our page. Parameter two of pdf_show_xy() is the string to use, and parameters three and four are the X and Y co-ordinates at which to print the text.

Author's Note: Confusingly, there is a pdf_set_font() function that is deprecated - try not to get mixed up!

It is important to note that the Y parameter, the last parameter passed to pdf_show_xy(), is distance the text should appear above the page baseline, in points. That is, setting this parameter to 0 will have the bottom of a lowercase "a" at the very bottom of the page, and the bottom of a lowercase "y" outside the margins of the page.

With pdf_end_page called, the first and only page is completed, and all that is left to do is clean things up.

Cleaning up is done through the help of two functions - pdf_close() and pdf_delete(). They may sound somewhat similar, but you do need to call them both - pdf_close() cleans up the PDFlib memory and document-related resources, whereas pdf_delete() cleans up PHP's reference to $pdf and any other internal resources. Be sure to call them in the order shown above.

Save the code as pdf1.php and, after placing inside your public HTML directory, run it using your web browser. Note that there is no "Success!" message being printed out, however, you should find your PDF file has been created and is viewable in your PDF reader of choice.





<< 11.4.1 There\'s more than one way to do it   11.4.3 Adding more pages and more style: pdf_setcolor() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
shivarules@gmail.com - 07 Sep 2008

I may not check this again because while searching I got this site pls reply me to my email ID.

shivarules@gmail.com - 07 Sep 2008

HI

For me its not creating the new object $pdf = PDF_new();

I m using Linux I think I have to include php_pdf.dll file, now how to include php_pdf.dll in linux.

Thanks in advance.

Cheers

shivarules@gmail.com - 07 Sep 2008

HI

For me its not creating the new object $pdf = PDF_new();

I m using Linux I think I have to include php_pdf.dll file, now how to include php_pdf.dll in linux.

Thanks in advance.

Cheers

sai - 07 Sep 2008

where is the pdf file created.. it is not created in the folder where the pdf1.php is located. where can i find the file which is created. I do have Acrobat reader for reading the pdf created, but not able to find where the pdf file has been created.

A PHP User - 07 Sep 2008

run

<?php
phpinfo();
?>

and check in the middle of the page of PDF has been
activated.

good luck

A PHP User - 07 Sep 2008

run

<?php
phpinfo();
?>

and check in the middle of the page of PDF has been
activated.

good luck

A PHP User - 07 Sep 2008

I have the same problem but with PDF_begin_page(). the preceding statements work fine (PDF_set_info etc). i have correctly altered the ini file and have the correct version of php_pdf.dll(as i already had this problem) but now i get the same error as above, its really pissing me off. any help would b greatly appreciated.
Cheers

A PHP User - 07 Sep 2008

Good advice here. So-called optional parameters, do the empty string thing.
http://www.webdotdev.com/nvd/server-side/php/php-extensions-pdflib.html
kaller at hug dot co dot nz

A PHP User - 07 Sep 2008

Hi,

I'm having a problem with pdf_open_file:
- if I give it a parameter, then it does not create the file, and the execution stops
- if I don't provide a file name, and try to output the bugger, it eats up all my memory and CPU, without results.

What I'm trying to do: in a smarty templates environment, I want to have one image posted a few times in a pdf file, and then generate the file and open it.

Any help would be appreciated!

A PHP User - 07 Sep 2008

Hi,

I'm having a problem with pdf_open_file:
- if I give it a parameter, then it does not create the file, and the execution stops
- if I don't provide a file name, and try to output the bugger, it eats up all my memory and CPU, without results.

What I'm trying to do: in a smarty templates environment, I want to have one image posted a few times in a pdf file, and then generate the file and open it.

Any help would be appreciated!

A PHP User - 07 Sep 2008

You have to enable PDF_lib in your php extensions (f. e. uncomment the correct extension line in php.ini).

CDa3230771@aol.com - 07 Sep 2008

I am a student using WinXPPro, Apachce 1.3.2 and php 4.XX, I have tried the code for creating a PDF but get an error as you describe above:
Fatal error: PDFlib error: function 'PDF_set_info' must not be called in 'object' scope

Apparently PHP for Windows has PDF support, I have edited my PHp.ini file (uncommented the PDF.dll statements) but still can't get this to work.
Any Pointers anyone?



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is ten plus eight?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow