Using symfony and sfDomPdfPlugin to generate pdf

This article has moved to this location.

One of the nice things from DOMPDF is the way it to generate PDF file from HTML content. In symfony, DOMPDF bundled as plugin. You can download the file here. If you want to know more about DOMPDF, please visit the site here, and if you found any troubles, you can visit this site.

Alright.. let’s get started.
Say, for an example, you have a template to create monthly report, and you want to reuse it to generate PDF. The main question is, how to get the HTML content in symfony? and passing it to DOMPDF?

Create myUtils.class.php or whatever you like, and put this code in your class

public static function getHtmlContent(sfActions $action, $layout = false)
    sfConfig::set('sf_web_debug', false);
    $context = $action->getContext();
    //clear http headers

    /*get the view, execute n render with variables*/
    $view = $context->getController()->getView($action->getModuleName(), $action->getActionName(), 'Success');
    $content = $view->render();
    return $content;

and in your action class,

public function executeDownload_as_pdf(sfWebRequest $request)
    /*your actions code goes here*/
    //get data from database, setting variables.. etc
    /*end your actions*/
    //code to generate pdf
    $html = myUtils::getHtmlContent($this, 'layout_popup_dompdf');
    $dom_pdf = new sfDomPDFPlugin($html);

Don’t forget to make folder fonts in sfDomPDFPlugin/lib/dompdf/lib/fonts writeable.

This is far from perfect.. so, please share your idea with me.. ty

7 responses to “Using symfony and sfDomPdfPlugin to generate pdf

  1. Pingback: Generating pdf using mPDF library in Symfony « Just Blog, No More…

  2. Gregori August 12, 2010 at 1:41 am

    Hi, the “myUtils.class.php” could be in any folder of my symfony project? Or this got a be a specific folder?
    I mean, if i have “reports” module (for example) with the action folder and the template folder, my “myUtils.class.php” should be placed in there or not?
    How the right place to put this classes?

    Just for information, i’m knew at symfony 🙂

    Thanks in advance

    • nibsirahsieu August 12, 2010 at 4:00 am

      You have to put the class in the ones of autoloading directories. So symfony can find it.

      • myproject/lib/
      • myproject/lib/model
      • myproject/apps/myapp/lib/
      • myproject/apps/myapp/modules/mymodule/lib
  3. mikael September 21, 2010 at 1:20 am

    Thanks for your good posts.
    Which others good functions do you have in your myUtils class?

    • nibsirahsieu September 21, 2010 at 6:09 am

      You are welcome
      Here are the functions that i always used in my application:
      This function is to create an indexed array of object by one of it functions

      public static function indexObjectsByFunction($arrayOfObject, $function)
          $result = array();
          foreach($arrayOfObject as $object) {    
            $result[$object->$function()] = $object;
          return $result;

      Grouping array of objects

      public static function indexArrayObjectsByFunction($array, $function)
          $result = array();
          foreach($array as $object) {    
            $result[$object->$function()][] = $object;
          return $result;

      I hope it useful..

  4. Mouhammed Diop December 7, 2010 at 6:23 pm

    I have this error when execute “executeDownload_as_pdf”
    Unable to find a suitable font replacement for: ‘serif’

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: