1

I have an issue with generation a pdf with MPDF.

Everything is working fine when devmode is on. When I try it on production it can't load the pdf. When I set devmode true on production it's working again. Normally it should be the opposite since we are developing with devmode on.

Anyone have a idea what could be the issue?

Edit: I'm using Craft CMS 3, it's happening aswell on my local environment when I set the .env file to production.

5
  • Is there an error message and what do the logs show? There's far too little information here to be able to provide a helpful answer.
    – Ben Croker
    Commented Jan 17, 2023 at 15:05
  • @BenCroker I'm not getting any errors at all. Just "Can't load this pdf". When I look at the server logs I don't see any. When I try to do dd($html); it returns me a 500 error. If I turn devmode on it shows me the HTML correctly
    – wesley
    Commented Jan 17, 2023 at 15:20
  • The underlying 500 internal server error message will either be in Craft's logs or your web server's logs (depending on how it originated). Since you've already checked the web server's logs, anything in Craft's?
    – Brad Bell
    Commented Jan 18, 2023 at 0:14
  • @BradBell Well, actually it isn't coming up in the phperrors.log. In the web.log I see clearly that I'm requesting the page at 2023-01-18 19:21:11 when it gives a 500 error. The phperrors.log hasn't been updated in 2 days.
    – wesley
    Commented Jan 18, 2023 at 18:24
  • I don't have enough reputation to comment, so I guess I'll keep adding answers here that aren't actually answers. Craft CMS Version: 4.3.6.1 I spent some time today transitioning to dompdf instead and found I still have the same issue, with dev mode off, they don't work. So it may very well be something non-specific to MPDF. Commented Jan 18, 2023 at 23:26

4 Answers 4

2

Not a full answer, but a possible workaround.

I was able to get the Super PDF plugin to work regardless of Dev Mode. It uses dompdf under the hood, but something in the way it is implemented bypasses whatever the underlying issue is.

https://github.com/amici-infotech/craft-super-pdf

3
  • I'll try this later this week. Huge thanks for your possible answer!!!
    – wesley
    Commented Jan 23, 2023 at 14:25
  • Really don't like this plugin, not getting my custom fonts and svg's to work. Only basic css is working. Need a fix for the problem we have
    – wesley
    Commented Feb 3, 2023 at 10:10
  • dompdf is harder to work with than MPDF, for sure. SVGs are likely a no-go, fonts should work though. Commented Feb 6, 2023 at 21:15
2

I've finally fixed my issue.

My initial function to generate the PDF's was:

class PdfVariable
{
    public function generate($html, $name, $toBrowser = true)
    {
        try
        {
        
            $defaultConfig = (new \Mpdf\Config\ConfigVariables())->getDefaults();
  

            $_mpdf = new \Mpdf\Mpdf([
                'mode' => 'utf-8',
                'format' => 'A4-P',
    
            ]);

            $_mpdf->shrink_tables_to_fit = 1;

            $_mpdf->WriteHTML($html);

            if($toBrowser)
            {
                // Shows pdf in browser (for development)
                //$_mpdf->Output($name . '.pdf', \Mpdf\Output\Destination::DOWNLOAD);
                $_mpdf->Output();
            }
            else
            {
                // Downloads
                $_mpdf->Output($name . '.pdf', \Mpdf\Output\Destination::DOWNLOAD);

            }
        }
        catch(Exception $e)
        {
            echo $e->getMessage();
        }

        if($toBrowser)
        {
            exit(0);
        }
    }
}

Somehow this piece of code

if($toBrowser)
{
    exit(0);
}

Causes it to crash when the environment is set to staging/production. If set to 'dev' it will still function normally with this piece of code.

Still find it weird it gave no errors whatsoever in the Craft CMS logs.

Hope I can help someone out with this.

0

I can also confirm that using a template hook instead of a twig variable doesn't help.

I also downgraded Craft to 4.0.6 and that also has no affect.

0

Wow, I just had the exact same problem pop up and have been debugging for hours and hours and this is the first time I've found any indication that it's not just me. Here's what I know, but I have no solutions so far...

  • I can reproduce locally, so I don't think it's a server issue.
  • I also don't think it's a file permission issue, getting the same result if I'm saving files locally or just sending them inline to the browser.
  • MPDFs WriteText method also doesn't work, so I don't think it's an HTML parsing issue.
  • Similarly, I've reduced what I'm trying to do to just a Hello World string of text, so I don't think it's anything about the document itself.

Similarly no errors, the PDF is created, it's just empty/doesn't render.

There is nothing documented about Craft's Dev Mode that seems like it would have any affect on this.

BTW, my implementation uses a custom module that really just comes down to a twig variable function. I feel like that's the only thing that even kind of related to dev mode. The function runs when that route is hit. In my reduced test case it passes no parameters. It obviously runs regardless of the mode.

Craft CMS Version: 4.3.6.1

MPDF: 8.1.2

1
  • Maybe we should make an issue on the Craft CMS github? I'm happy it isn't just me aswell. What Craft CMS version are you using?
    – wesley
    Commented Jan 18, 2023 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.