This is how I created my dompf plugin for codeigniter used on most of my projects.

First lets start off with the files, get a copy of dompdf and extract the files needed.

Here’s what you’ll need.
on your CI System/plugins Directory make a folder and call it something, I named mine “dompdf” extract the following folders and files from dompdf.

system/plugins/dompdf/

  • include (entire directory)
  • lib (entire directory)
  • dompdf.php
  • dompdf_config.inc.php
  • LICENSE.LGPL (for legality sake :P )
  • load_font.php

Now lets move on and create your plugin under the system/plugin folder and call it something, I baptized mine as “to_pdf.php“.

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Try increasing memory available, mostly for PDF generation
 */
ini_set("memory_limit","32M");

function pdf_create($html, $filename, $stream=TRUE)
{
	require_once(BASEPATH."plugins/dompdf/dompdf_config.inc.php");

	$dompdf = new DOMPDF();
	$dompdf->set_paper("a4", "portrait");
	$dompdf->load_html($html);
	$dompdf->render();
	if ($stream) { //open only
		$dompdf->stream($filename.".pdf");
	}else{ // save to file only, your going to load the file helper for this one
		write_file("pdf/$filename.pdf", $dompdf->output());
	}
}
?>

Usually I render from HTML to PDF so we’re gonna need an HTML file for this one. Here’s my structure and i called it sample.html:

<html>
	<head>
		<title>PDF Sample</title>
		<script type="text/php">
		if ( isset($pdf) ) {
			//This goes to the header
			$font = Font_Metrics::get_font("Helvetica", "bold");
			$pdf->page_text(72, 18, "Page {PAGE_NUM} of {PAGE_COUNT}", $font, 8, array(0,0,0));

			//get started with the footer
			// Open the object: all drawing commands will
			// go to the object instead of the current page
			$footer = $pdf->open_object();
			$text_height = 7;

			$w = $pdf->get_width();
			$h = $pdf->get_height();
			$y = $h - 2 * $text_height - 24;
			$size = 7;
			$color = array(255,255,255);

			$font = Font_Metrics::get_font("Helvetica", "italic");
			$text = "http://www.ikawka.com/ - ".date("F m, Y - h:i:s a");
			$width = Font_Metrics::get_text_width($text, $font, $size);

			// Add a logo
			$img_w = 540; // 2 inches, in points
			$img_h = 20; // 1 inch, in points -- change these as required

			$pdf->image("images/logo.png", "png", ($w - $img_w) / 2.0, $y - $img_h, $img_w, $img_h );
			$pdf->text(38, $y-16, $text, $font, $size, $color);
			// Close the object (stop capture)
			$pdf->close_object();

			// Add the object to every page. You can
			// also specify "odd" or "even"
			$pdf->add_object($footer, "all");
		}
		</script>
	</head>
	<body>
		<h1>Hello Gallaxy!!!</h1>
	</body>
</html>

I’ve added some extra feat onto this one, the script on the HTML file will have a header and a footer image on each and every page of the generated pdf.

Now let’s make our controller. Let’s name it “htmltopdf.php“.

<?php
class htmltopdf extends Controller{
	function htmltopdf(){
		parent::Controller();
	}

	function index(){
		$this->load->library( 'parser' );
		$this->load->plugin( 'to_pdf' );

		//render your html first, I'm using parser on this example
		//you can always use any method you like
		$html = $this->parser->parse('sample.html', $data, true);

		pdf_create( $html, 'filename', TRUE ); //this will stream only
	}
}
?>

And there you have it, your dompdf magic! ^_^

Extras: (download at your own risk)
dompdf – minor bug fixes.