Development

sfDomPDFPlugin

You must first sign up to be able to contribute.

sfDomPDFPlugin

Overview

This plugin allows you to convert HTML into a PDF. This is great for converting things like invoices and customized documents into non-editable PDF format without having to recreate the wheel. Please note, that DomPDF does support CSS 2.1, however it is very picky with the formatting. Please avoid using shorthand CSS sytles or combining styles.

Maintainer

Please report all bugs to Ian Ricketson <ian.ricketson[at]symfony-project[dot]com> (_Slick_Rick on IRC)

Prerequisites

  • Make sure you are using the latest version of symfony (at least version 1.0).

Install the Plugin

To install the plugin from the symfony command line, run the following command:

symfony plugin-install http://plugins.symfony-project.com/sfDomPDFPlugin

You can also choose to manually install the plugin by downloading the attached file.

Basic Usage

// Get some HTML to pass into the class
// For this example we are going to convert an HTML invoice into a PDF
public function executePdf()
{
  $invoice = $this->getPresentationFor('invoices', 'print');
  
  // Generate PDF from invoice
  $q = new sfDomPDFPlugin($invoice);
  $q->setProtocol('https://');
  $q->setHost('yourdomain.com');
  
  // The $pdf variable will contain our binary PDF document.  
  // As an example, we are going to send this pdf to the browser as a downloadable file
  if ($pdf = $q->execute())
  {
    $response = $this->getContext()->getResponse();
    $response->setHttpHeader('Pragma', '');
    $response->setHttpHeader('Cache-Control', '');					
    $response->setHttpHeader('Content-Type', 'application/pdf');
    $response->setHttpHeader('Content-Disposition', 'attachment; filename="invoice-2342.pdf"');
    $response->setContent($pdf);
  }
  
  return sfView::NONE;
}	

Note: Due to the fact that the entire HTML document is included, you may experience problems with PDF generation in dev mode. Make sure the output of your HTML document is EXACTLY how you want it to appear in the PDF document. This includes removing excess JS, CSS, and layouts.

Additional Information

There are many more options inside the sfDomPDFPlugin class that you may want to investigate, such as changing the paper size, or orientation. Please examine the source code for a further look at the plugin.

For more information about the DomPDF library, please see the DomPDF website

View the sfDomPDFPlugin Source Code

Please note that version 1.0.4 of this plugin may have an issue with the fonts directory on some systems. Please see this forum thread for more information: http://www.symfony-project.org/forum/index.php/m/50715/

More information about version 1.0.4 may be found in this forum post: http://www.symfony-project.org/forum/index.php/m/49293/

Attachments