Is there a solution for pdf template in PHP?

I need to generate quotes in pdf format by PHP, with a template and dynamic values retrieved from database. is it viable ?

asked May 17, 2010 at 12:47 user198729 user198729 63.3k 109 109 gold badges 254 254 silver badges 352 352 bronze badges

Just a heads-up: creating PDFs with PHP is very fiddly. Adjusting dimensions, positioning. Just allow yourself time for tweaking ;)

Commented May 17, 2010 at 12:58

3 Answers 3

UPDATE: i see people upvoting this recently and wanted to add that wkhtmltopdf might be a better option for most applications.

yes, fpdf + fpdi worked for me

basically, you create your pdf template first, then load it into the new FPDI object and use FPDF functions to draw "over" the template - much like photoshop layers work.

$pdf = new FPDI(); $pdf->setSourceFile('template.pdf'); $tpl = $pdf->importPage(1); $pdf->addPage(); $pdf->useTemplate($tpl); setXY(10, 20); $pdf->write(100, "Hi there"); output('newpdf.pdf', 'D');  
answered May 17, 2010 at 12:50 user187291 user187291 53.8k 19 19 gold badges 96 96 silver badges 127 127 bronze badges My team is using FPDF to some success, though I haven't coded against it yet. Commented May 17, 2010 at 12:50 Do you mean I need these two libs to create a customized pdf,or . can you demonstrate ? Commented May 17, 2010 at 13:07 Last but no least,is it free?Seems not . Commented May 17, 2010 at 13:47

Good ! But I want the dynamic text be added like this way: print("this is your product:%s",$name) .Seems it's not the case in the demo.

Commented May 17, 2010 at 14:25

No,in a photoshop manner,I don't think it can handle the overflow issue,I need it to wrap around instead of go out of view.

Commented May 17, 2010 at 23:23

Try the DOMPDF. It's very good & easy to use, and has got some very useful APIs. I have used it twice, and it really amazes me as to what not you can do.

Really, you can set multiple pages in the on-the-fly created PDF, with resized images acting as catalog products.

It has a very good Documentation, with examples shown. If you have any problems integrating it, feel free to post your question here, and the users will be proactive in answering your questions.

DOMPDF is an awesome cool stuff. I think that it has also won some awards, for its sheer coding standard. Check out yourself, for more.

answered May 17, 2010 at 13:04 Knowledge Craving Knowledge Craving 7,975 13 13 gold badges 51 51 silver badges 92 92 bronze badges

If you can afford the licensing costs, PDFLIB has a nice templating system they call "blocks". You draw the blocks onto the source template in Acrobat, assign it a name, and then in the PHP code it's a simple matter of issuing a "fill this block with the following text/image/eps".

Once all the housekeeping code of opening the template/loading fonts/etc. is completed, the fill-in-the-blanks codes boils down to:

PDF_fill_textblock($pdf, $pagehandle, $blockname, "Text to insert", 'fitting method arguments'); PDF_fill_imageblock($pdf, $pagehandle, $blockname, $imagehandle, 'fitting method arguments'); PDF_fill_pdfblock($pdf, $pagehandle, $blockname, $pdfhandle, 'arguments here');