Friday, January 16, 2015

Create PDF with fixed header and footer in PHP or JavaScript

Solution 1:

TCPDF - one reason is Unicode support in tcpdf, which fpdf utterly lacks. tcpdf is also still actively maintained. and, it's also got a decent OO architecture. word to the wise: fpdf is basically an old thing that got superseded by tcpdf.

http://www.tcpdf.org/index.php

Solution 2:

MPDF is a PHP class which generates PDF files from UTF-8 encoded HTML. It is based on FPDF and HTML2FPDF, with a number of enhancements.

I found mpdf better than tcpdf in terms of html rendering. It can parse css styles much better and create pdf that look very similar to the original html.

mpdf even supports css things like border-radius and gradient etc.

I am surprised to see why mpdf is so less talked about when it comes to html to pdf.

Check out the examples here http://www.mpdf1.com/mpdf/examples

I found it useful for designing invoices, receipts and simple prints etc. However the website itself says that pdfs generated from mpdf tend to be larger in size.

http://www.mpdf1.com/mpdf/index.php

Solution 3:

DOMPdf is the easiest way to make a pdf quickly. Like Mike said, feed it html and it outputs a pdf. Under the hood, it has the option to use either r&ospdf or pdflib as the rendering engine.

https://github.com/dompdf/dompdf
http://www.ros.co.nz/pdf/

Solution 4:

html2ps is the best if you need the css and 3c compatibily.

http://sourceforge.net/projects/html2ps/

Solution 5:

wkhtmltopdf - A great alternative for PDF generation is wkhtmltopdf. It has a smooth integration and the result is awesome. You will have to install the binary into your server, though. If you're on a VPS or dedicated, this will not be a problem.

There's a PHP lib for using it, you can find it here: https://github.com/KnpLabs/snappy

Also, there's a nice solution for the large table problem you have. Maybe it can help you with another lib too, take a look here: https://code.google.com/p/wkhtmltopdf/issues/detail?id=566

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

Solution 6:

Print to the built-in "Microsoft XPS Document Writer" (the *.xps format) and use Apache FOP to covert it to PDF. Not a PHP-native solution, not very efficient either, but it works well even if you need to generate PDF with very complex layouts.

http://xmlgraphics.apache.org/fop/

Solution 7:

The Zend Framework's Zend_Pdf is really good too. It's on par with pdflib in terms of control of output and complexity and is more portable because its a pure php solution. That said, its slower and uses more memory than pdflib. Pecl modules are always more efficient than a php solution.

http://framework.zend.com/manual/en/zend.pdf.html

Solution 8:

Phantomjs now supports pdf output. The problem with various libraries (PDFlib, FPDF etc.) is the rather long learning curve. PhantomJS uses html5+css3+svg+canvas, which have a long history and large user space and php people know html+css usually very well, so the learning curve with PhantomJS is short.

http://phantomjs.org/

Solution 9:

If you include commercial, I've heard good things about Prince.

http://princexml.com/

Solution 10:

PECL pdflib - Creating PDF on the fly with the PDFlib library

http://pecl.php.net/package/pdflib

Solution 11:

PDF.js - A general-purpose, web standards-based platform for parsing and rendering PDFs.

http://mozilla.github.io/pdf.js/

Solution 12:

PHPExcel - Project providing a set of classes for the PHP programming language, which allow you to write to and read from different spreadsheet file formats, like Excel (BIFF) .xls, Excel 2007 (OfficeOpenXML) .xlsx, CSV, Libre/OpenOffice Calc .ods, Gnumeric, PDF, HTML, ... This project is built around Microsoft's OpenXML standard and PHP.

http://phpexcel.codeplex.com/

Reference:

http://stackoverflow.com/questions/1648715/best-pdf-generator-in-php-mpdf-or-fpdf
http://stackoverflow.com/questions/4108361/pdflib-for-php-is-there-an-alternative

No comments: