简体   繁体   中英

Symfony 4 - DomPdf doesn't work in production on Heroku : weird characters

I use dompdf on symfony 4 to generate pdf. Everything works in local dev. I tried to launch my app in prod on Heroku, but when I go on a link generating a PDF, I get this:

在此处输入图像描述

I told myself that it came from the requirements and that he had to miss things on Heroku.

Here is the list of what it takes to run dompdf:

Requirements

  • PHP version 5.4.0 or higher
  • DOM extension
  • GD extension
  • MBString extension
  • php-font-lib
  • php-svg-lib

I've PHP 7.3

And in my composer.json I've:

"require": {

//...
"ext-mbstring": "*",
"ext-gd": "*",
"dompdf/dompdf": "^0.8.3",
"phenx/php-font-lib": "^0.5.1",
"phenx/php-svg-lib": "^0.3.3",
}

And according the Heroku documentaiton, DOM is enabled by default:

在此处输入图像描述

To generate some pdf, in my php file:

public function generatePdf(OrdreMission $ordre)
    {
        // Configure Dompdf according to your needs
        $pdfOptions = new Options();
        $pdfOptions->setIsRemoteEnabled(true);



        // Instantiate Dompdf with our options
        $dompdf = new Dompdf();
        $dompdf->setOptions($pdfOptions);
        // $dompdf->set_option('isHtml5ParserEnabled', true);

        // Retrieve the HTML generated in our twig file

        $html = "<link type='text/css href='/css/bootstrap.min.css' rel='stylesheet'>" . $this->templating->render('ordre_mission/pdf.html.twig', [
            'ordre' => $ordre,
        ]);


        // Load HTML to Dompdf
        $dompdf->loadHtml($html);

        // (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
        $dompdf->setPaper('A4', 'portrait');

        // Render the HTML as PDF
        $dompdf->render();

        // Output the generated PDF to Browser (inline view)
        $dompdf->stream("mypdf.pdf", [
            "Attachment" => false,
        ]);
    }

It's ok in dev environment but not in prod environment

Can someone help me please?

I had similar issue.

I found solution via logs inside heroku.

The problem is that all controller require:

Symfony\Component\HttpFoundation\Response

for return and you do not provide it.

Controller method return null instead.

Solution is simple, You need to provide return response after stream dompdf.

It wont reach it, because you will stream pdf first but somehow it require inside heroku server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM