logo
Ask your Symfony questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

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

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:

http://nibsirahsieu.wordpress.com/2009/11/02/using-symfony-and-sfdompdfplugin-to-generate-pdf/

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).

This question has been answered.

fxsymfony | 10/17/10 at 11:25pm Edit


The experts have suggested, on average, a prize of $4 for this question.

(6) Responses

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    10/18/10
    9:16pm
    Nate Flink says:

    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("http://symfonyexperts.com/question/show/id/141#answer-section"));
    }


    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://").
    $_SERVER['SERVER_NAME'].'/your-url.php?p='.$your_params)
    );
    }



    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.

    ADDENDUM

    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://"
    ).$_SERVER['SERVER_NAME']."/default/index?p=".$params;


    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.

    Previous versions of this answer: 10/18/10 at 12:11am | 10/18/10 at 10:55am | 10/18/10 at 12:12pm | 10/18/10 at 12:12pm | 10/18/10 at 12:13pm | 10/18/10 at 12:14pm

  • avatar
    Last edited:
    10/18/10
    12:19am
    Sanjay Rathod says:

    Hi
    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('your_partial_name',array('parameter you want to pass'));



    $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



    $omPdf->WriteHTML($ssHtml,2);



    $omPdf->Output($ssPdfName,'D');

    exit;

    }

    Hope this will help you.
    Regards

    Previous versions of this answer: 10/18/10 at 12:19am

  • avatar
    Last edited:
    10/18/10
    3:16am
    Alex Zgorzhelsky says:

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

  • avatar
    Last edited:
    10/18/10
    9:16pm
    David PĂ©rez says:

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

  • avatar
    Last edited:
    10/18/10
    3:35am
    fxsymfony says:

    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?

  • avatar
    Last edited:
    10/18/10
    9:12pm
    fxsymfony says:

    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:

    http://code.google.com/p/wkhtmltopdf/

    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.

    Cheers

This question has expired.





Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.