Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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?



66.8k
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);


1.9k
edited 17d ago

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

>
> requireonce(LIBPATH.'/vendor/dompdf/dompdf%5Fconfig.inc.php');
> $dompdf = new \DOMPDF();
> $dompdf->loadhtml($htmlcode);
> $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.