We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Generating PDF file wih Phalcon

hi,

I saw this topic

https://forum.phalcon.io/discussion/985/generating-pdf-file-in-phalcon-framework

but, I need more informations for how to generate pdf with phalcon.

how should I proceed?

for sample, in the action "pdf_report", what should I put?

where can I get examples?

sorry for my english

Thanks



39.4k
Accepted
answer

You will first need to have the mpdf library available to you (usually by composer) - https://github.com/mpdf/mpdf

In an action (that you will choose) say pdf_reportAction you gather all the data you need and inject it in a view. That will return a string which should be well formed XML. You pass that string to the mpdf library and your file is there :)

Something like this:

public function pdf_reportAction()
{
    $this->view->disable();

    // Get all the data from the database
    $data = Posts::find();   

    // Get the view data
    $html = $this->view->getRender('reports', 'pdf_report', $data);

    $pdf = new mPDF();
    $pdf->WriteHTML($html, 2);
    // .....
}

Your view will get the $data and do something with it like any view that shows data on screen. You can present things in a table or whatever you like.