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

How to load the domPDF library in phalcon 3.2.3?

I am working on Phalcon 3.2 and I try to use the DomPDF library, someone who has implemented it to tell me how I add it?



85.5k
Accepted
answer

hmm intresting, i am using mpdf, didnt know about this one.

Anyways, implementing "standalone" libraries has nothing to do with Phalcon, you have to implement them in your own way. Here are some code samples of my workers which are sending emails


$di->set('simpleView', function ($dir) use ($config) {

        $view = new \Phalcon\Mvc\View\Simple();
        $view->setViewsDir($dir);
        $eventsManager = new EventsManager();

        $view->registerEngines([
            '.phtml' => function ($view, $di) {

                $phtml = new \Phalcon\Mvc\View\Engine\Php($view, $di);

                return $phtml;
            },
            '.volt'  => function ($view, $di) use ($config) {

                $volt = new  \Phalcon\Mvc\View\Engine\Volt($view, $di);
                $volt->setOptions([
                    'compileAlways' => true,
                    'compiledPath'  => $config->voltCompileDir
                ]);

                $compiler = $volt->getCompiler();
                $compiler->addFunction("money_format", "money_format");
                $compiler->addFunction("round", "round");
                $compiler->addFunction("hashIds", function ($resolvedArgs, $exprArgs) {

                    return $this->getShared("hashIds")->encode($resolvedArgs);
                });

                return $volt;
            }
        ]);

        $eventsManager->attach("view:afterRender", function ($event, $view) {

            $search = [
                '/\>[^\S ]+/s',  // strip whitespaces after tags, except space
                '/[^\S ]+\</s',  // strip whitespaces before tags, except space
                '/(\s)+/s'       // shorten multiple whitespace sequences
            ];

            $replace = [
                '>',
                '<',
                '\\1'
            ];

            $view->setContent((string)preg_replace($search, $replace, $view->getContent()));
        });

        return $view;
    });

    //in the controller 
    $html = $this->simpleView->render("somevoltfile.volt", ['my-data' => "somevar" ... ]);

    //create the pdf should be something like this
    $dompdf = new Dompdf\Dompdf();
    $dompdf->loadHtml($html);
    $dompdf->render();

    $output = $dompdf->output();
    file_put_contents('/var/www/mysite/filename.pdf', $output);


12.1k
edited Nov '17

The easy way: download dompdf inside a dir like lib/vendor/dompdf and define your LIB_PATH. Then include this lines wherever you need:

    require_once(LIB_PATH.'/vendor/dompdf/dompdf_config.inc.php');  
    $dompdf = new \DOMPDF();  
    $dompdf->load_html($html_code);  
    $dompdf->render();  

                  // debug pdf  
     $dompdf->stream("pdf_".date('dmYHm').".pdf");  

you should wrap it inside a phalcon pdf class created ad hoc for generating pdf.