Skip to content

PDF generation using dompdf

World Wide Web Server edited this page Jul 4, 2012 · 29 revisions

Well... its not so much of a plugin as a "how-to". I've needed to be able to generate PDFs on the fly for the application I'm building. I've played with [url=http://www.fpdf.org/]FPDF[/url] but found it the syntax to onerous to be practical (I want users to be able to control the look of their PDFs, and with FPDF they'd essentially need to learn a new language). So then I tried [url=http://html2fpdf.sourceforge.net/]html2fpdf[/url] to create them on the fly from HTML pages. I was very, very disappointed with its lack of support for simple HTML. I then found [url=http://xhtml2pdf.mandragor.org/fr/index]xhtml2pdf[/url] and [url=http://user.it.uu.se/~jan/html2ps.html]html2ps[/url]... same thing. Finally, I hit upon [url=http://www.digitaljunkies.ca/dompdf/]dompdf[/url]. EXCELLENT! Decent support for what I needed to do, and has decent (not great) support for many CSS styles and XHTML.

So here's how to implement it in your own project.

  1. Get a copy from http://www.digitaljunkies.ca/dompdf/
  2. Uncompress it. You'll get a folder (I renamed it "dompdf"), and put the whole thing into your plugins folder, ie: /system/plugins/dompdf
  3. Create the plugin. I've named it "to_pdf_pi.php"[code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); function pdf_create($html, $filename) { require_once("dompdf/dompdf_config.inc.php");

$dompdf = new DOMPDF(); $dompdf->load_html($html); $dompdf->render(); $dompdf->stream($filename.".pdf"); } ?> [/code] 4) use it in your controllers like this[code] function pdf() { $this->load->plugin('to_pdf'); // page info here, db calls, etc.
$html = $this->load->view('controller/viewfile', $data, true); pdf_create($html, 'filename'); } [/code] [i]A few notes:[/i]

  • It is PHP 5 only. Sorry, I know that goes against what CI is doing here... but the level of support for CSS in the other projects wasn't acceptable for my app
  • You might find that there is limited or no support for a specific CSS style you want (ie: floating) and you'll need to work around with old-school tables. Ugh ..... Sorry for that...
  • I in no way can claim credit for writing this fine code. dompdf was released under an lgpl license, and you'll obviously need to respect that in your apps if you choose to use it.

Derek

Clone this wiki locally