How do you create a PDF on the fly? And save it.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do you create a PDF on the fly? And save it.

Post by simonmlewis »

Hi

I will soon need to be able to create PDF documents on the fly, via HTML code. Creating the layout with logos and information.

How is this done?
Is there some "bible" where people learnt to do this online?

Once created on the fly, can it be saved on the server, tho it's probably best if the PDF is created again on the fly, for the customer.

Thanks.

UPDATE:

Have tried this:

Code: Select all

<?php
$mypdf = PDF_new();
PDF_open_file($mypdf, "");
PDF_begin_page($mypdf, 595, 842);
$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);
PDF_setfont($mypdf, $myfont, 10);
PDF_show_xy($mypdf, "Sample PDF, constructed by PHP in real-time.", 50, 750);
PDF_show_xy($mypdf, "Made with the PDF libraries for PHP.", 50, 730);
PDF_show_xy($mypdf, "A JPEG image, on 60 % of its original size.", 50, 710);
$myimage = PDF_open_image_file($mypdf, "jpeg", "/images/logo.jpg");
PDF_place_image($mypdf, $myimage, 50, 650, 0.6);
PDF_end_page($mypdf);
PDF_close($mypdf);
$mybuf = PDF_get_buffer($mypdf);
$mylen = strlen($mybuf);
header("Content-type: application/pdf");
header("Content-Length: $mylen");
header("Content-Disposition: inline; filename=gen02.pdf");
print $mybuf;
PDF_delete($mypdf);
?>
But it doesn't work. The image file is correctly entered.

Error is:
Fatal error: Uncaught exception 'PDFlibException' with message 'pdf_open_image_file() expects exactly 5 parameters, 3 given' in C:\xampp\phpMyAdmin\site\createpdf.php:10 Stack trace: #0 C:\xampp\phpMyAdmin\site\createpdf.php(10): pdf_open_image_file(Resource id #2, 'jpeg', '/images/logo.jp...') #1 {main} thrown in C:\xampp\phpMyAdmin\site\createpdf.php on line 10
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do you create a PDF on the fly? And save it.

Post by Christopher »

Maybe give it a valid filename?

Code: Select all

PDF_open_file($mypdf, "");
And if you read the manual you would know that that function is deprecated for PDF_begin_document():
http://www.php.net/manual/en/function.p ... cument.php
(#10850)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you create a PDF on the fly? And save it.

Post by simonmlewis »

Sorry what do you mean?
I am completely new to this function, so flying a bit blind.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: How do you create a PDF on the fly? And save it.

Post by mecha_godzilla »

Hi,

You could also look at using a 3rd party library like FPDF as there is an extension available that will do HTML-to-PDF conversions:

http://www.fpdf.org/

http://www.html2pdf.fr/en/default

I'm just mentioning FPDF as an alternative to the in-built PHP functions because I've used it myself for the past couple of years and there are lots of example scripts available on the site.

Another option if the layouts are particularly complex and/or styled by JavaScript is to use wkhtmltopdf:

http://code.google.com/p/wkhtmltopdf/

This essentially "prints" web pages to PDFs and the results are generally very good, but I'm not sure how viable an option this is for production use.

HTH,

Mecha Godzilla
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you create a PDF on the fly? And save it.

Post by simonmlewis »

There won't be any Javascript, just HTML on-the-fly, and CSS controlling.
I did look at FPDF but could not get to grips with that web site. Hence I wondered if there was a "golden rule" to it all.

Will look more tomorrow as it's late here now. But thanks.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: How do you create a PDF on the fly? And save it.

Post by mecha_godzilla »

Hi,

I think you're probably going to run into issues whichever PDF library you use, because the two mediums are very different and web browsers have advanced page rendering routines in them which a PDF library just cannot hope to emulate in any practical sense - remembering also that web browsers then usually rely on printer drivers provided by the OS to parse *their* output to PDFs.

As a suggestion, if you'd like to post/PM a link to the page design you need to convert then myself and the other forum members could probably tell you how easy or difficult it might be to emulate using a PDF library.

M_G
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do you create a PDF on the fly? And save it.

Post by Christopher »

simonmlewis wrote:Sorry what do you mean?
I am completely new to this function, so flying a bit blind.
I am completely new to this function also. So I looked at the manual and it says the second parameter is a filename. The filename you gave it is "" which is no file name. Then later you call PDF_close() which according to the manual "Closes the generated PDF file, and frees all document-related resources." After that you try to use the resource, which may no longer exist.
(#10850)
Post Reply