Ask your Symfony questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

PDF file (using TCPDF) of a dynamically generated view? Symfony


I will explain this with an example.

I have a module "reports", that has an action called "monthlyReport", this action receives "month_id" as an parameter, and generates a view with the monthly report for the specified month. What I need is to be able to generate a PDF out of this dynamically generated view.

If the view for "monthlyReport" was static and didn't have parameters, I could use:

But my "monthlyReport" view is based on the action's business logic. So basically I need a way to get the HTML content out of an action execution (action A calls action B and obtains its HTML content without showing the view in the browser).

Answers (4)


Nate Flink answers:

I could be wrong but it sounds like what your looking for is a way to wrap your http request inside your app. This is accomplished through something like this example:

public function executeSpitOutHtml(sfWebRequest $request)
$this->getResponse()->setContentType('Content-Type: text/html');//handy to set to whatever mime type is needed here
return $this->renderText(file_get_contents(""));

I've used this to return an http call to another php script before, like what you are describing with a little bit more:

public function executeSpitOutHtml(sfWebRequest $request)
$this->getResponse()->setContentType('Content-Type: text/html');
return $this->renderText(file_get_contents(
(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? "https://" : "http://").

I would note that it shouldn't really be necessary to wrap these calls, as there are plenty of good PDF tools for php out there.


I also like the 4th guy's answer about using an external pdf tool. You should check into that as well.

To answer the question, using the idea above you could either load the url_helper into the action (pain in the butt) and use the sf function "url_for" or use the php junk:

$url =
isset( $_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? "https://" : "http://"

where "default" is the module and "index" is the action. This assumes your using the default app, and no app name appears in the url.

Good luck, may the force be with you.

fxsymfony comments:

and how can I use this executeSpitOutHtml function, for example if I'm in action1 and want to obtain the html of action2, what would the code be like?

fxsymfony comments:

thanks Nate that works,

I ran into a different problem afterwards which is that my reports have javascript generated charts. Those get printed with David's idea of using these external url to pdf conversion tools.

Yet again, I can't use those solutions as my reports are secure modules.

After a lot of browsing I found the solution to all of my problems:

This tool implements Webkit in the server to generate PDF's in the same way that a browser would, including the JS charts, I'm pretty sure David's external tool uses the same thing. I got it running in my server and will work on getting it to work with Symfony. Thank you for the help.



David PĂ©rez answers:

The fastest and easier sollution could be an external PDF API such as PDFCROWD. You can get more information in


Sanjay Rathod answers:

Following are steps you should follow to generate a PDF report as HTML.

first of all separate the your business logic in a partial.
second on click on PDF icon you should perform following code.

* note here i have used a mPDF. You can also use TCPDF also

if($this->getRequest()->getMethod() == sfRequest::POST )

The magic begins here. when ever you use get_partial it will give you a whole html content.
$ssHtml = get_partial(<strong>'your_partial_name</strong>',array('<strong>parameter you want to pass</strong>'));

$omPdf = new mPDF('en-GB','A4','','',32,25,27,25,16,13); // Create a mPDF object
$omPdf->useOnlyCoreFonts = true;

// LOAD a stylesheet

$stylesheet = file_get_contents(sfConfig::get('sf_lib_dir').'/mpdf/mpdf.css');

$omPdf->WriteHTML($stylesheet,1); // The parameter 1 tells that this is css/style only and no body/html/text





Hope this will help you.


Alex Zgorzhelsky answers:

Answer #1 is correct, answer #2 is against all symfony concepts