Generating pdf using mPDF library in Symfony

This article has moved to this location.


In my previous article, i’ve shown you how to generate pdf using DomPDF library. In this article, i’ll show you how to generate pdf in symfony using mPDF library. mPDF has the same concept with DomPDF, generate pdf from the html content.
mPDF offers a lot of features. Here are some features offered by mPDF :

  1. Right-to-left languages
  2. Nested block-level elements (e.g. P, DIV) are supported, with enhanced CSS support e.g. margins, borders, padding, line-height, background colours etc.
  3. CSS style attributes now fully support font, font-size, color, and background color (for highlighting) plus many more.
  4. Table cell padding and borders are supported.
  5. List indenting can be defined.
  6. Custom tags added – PAGEBREAK, COLUMNBREAK, INDEXENTRY


Ok.. first, download mPDF library, extract and put in the lib folder.
In this article, i’ll take example05_tables.php as an example. You can find more examples in the mPDF examples folder.
Copy the content from example05_tables.php file to your template and mpdfstyletables.css file to the css directory (web/css). Here is your template look like ( I only show the beginning and the end of the content, sorry.. coz it’s too long šŸ˜‰ )

//test_mpdfSuccess.php
<h1>mPDF</h1>
<h2>Tables</h2>
<h3>CSS Styles</h3>
<p>The CSS properties for tables and cells is increased over that in html2fpdf. It includes recognition of THEAD, TFOOT and TH.<br />See below for other facilities such as autosizing, and rotation.</p>
<table border="1">
<tbody><tr><td>Row 1</td><td>This is data</td><td>This is data</td></tr>
....
....
</tbody></table>
<p>&nbsp;</p>

And in the actions class :

public function executeTest_mpdf(sfWebRequest $request)
{
   $html = myUtils::getHtmlContent($this);
   $mpdf = new mPDF('en-GB','A4','','',32,25,27,25,16,13);
   $mpdf->useOnlyCoreFonts = true;

  // LOAD a stylesheet
  $stylesheet = file_get_contents(sfConfig::get('sf_web_dir').'/css/mpdfstyletables.css');
  $mpdf->WriteHTML($stylesheet,1);	// The parameter 1 tells that this is css/style only and no body/html/text
  $mpdf->WriteHTML($html, 2);
  $mpdf->Output('mpdf.pdf','D');
}

Update :
You can use getPresentationFor() function of controller to get the html content. Here is the modified version of action above:

public function executeTest_mpdf(sfWebRequest $request)
{
}

public function executeAsPdf(sfWebRequest $request)
{
   $html = $this->getController()->getPresentationFor($this->getModuleName(), 'test_mpdf');
   $mpdf = new mPDF('en-GB','A4','','',32,25,27,25,16,13);
   $mpdf->useOnlyCoreFonts = true;

   // LOAD a stylesheet
   $stylesheet = file_get_contents(sfConfig::get('sf_web_dir').'/css/mpdfstyletables.css');
   $mpdf->WriteHTML($stylesheet,1);	// The parameter 1 tells that this is css/style only and no body/html/text
   $mpdf->WriteHTML($html, 2);
   $mpdf->Output('mpdf.pdf','D');
}

Here are the screenshots of generated pdf :

Hope it useful.. thanks you…

Advertisements

22 responses to “Generating pdf using mPDF library in Symfony

  1. petersaints August 23, 2010 at 10:21 pm

    Yeah I know is depecrated but that seems not to be the problem. As I said it works well on production environment. On development I get those nasty header already sent error and the message that the PDF cannot be sent.

    However I tried another library and it works fine. I used TCPDF trough sfTCPDFPlugin. It seems a little bit heavier (it takes 2 or 3 seconds to display the PDF) but works like a charm on both dev and prod environments and it seems less prone to error.

    Also I discovered that if I only want the output from a single action without the layout around it all you have to do is:

    $this->setLayout(false);

    I hope all this info may help others.

  2. Giles Birch August 25, 2011 at 11:35 am

    Thanks for this! In case it may help someone else, in Symfony 1.4 I had to modify this approach a bit:
    1. At the end of the executeAsPdf function I had to add “return sfView::NONE;” to suppress error messages.
    2. I wanted a specific portion of a page turned into a PDF, without the layout, web development toolbar, etc. Using a partial or a component seems to fit the bill: E.g. $html = $this->getPartial(‘order’,array(‘var’=>$var,….));

  3. DrAkE September 29, 2011 at 3:21 am

    Hey there! excelent (and smooth šŸ™‚ ) use of mPDF! im using it right now… its just perfect!

    Thank a lot and greetings from Venezuela!!!

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: