<p>Not a full answer, but a possible workaround.</p>
<p>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.</p>
<p><a href="https://github.com/amici-infotech/craft-super-pdf" rel="nofollow noreferrer">https://github.com/amici-infotech/craft-super-pdf</a></p>

CC BY-SA 4.0
2023-01-23T14:25:49.120Z
I'll try this later this week. Huge thanks for your possible answer!!!
wesley
CC BY-SA 4.0
2023-02-03T10:10:30.437Z
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
CC BY-SA 4.0
2023-02-06T21:15:43.650Z
dompdf is harder to work with than MPDF, for sure. SVGs are likely a no-go, fonts _should_ work though.
Brian Norris
CC BY-SA 4.0
2023-01-20T18:48:59.550Z
2023-01-20T18:48:59.550Z
Brian Norris
<p>I've finally fixed my issue.</p>
<p>My initial function to generate the PDF's was:</p>
<pre><code>class PdfVariable
{
    public function generate($html, $name, $toBrowser = true)
    {
        try
        {
        
            $defaultConfig = (new \Mpdf\Config\ConfigVariables())-&gt;getDefaults();
  

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

            $_mpdf-&gt;shrink_tables_to_fit = 1;

            $_mpdf-&gt;WriteHTML($html);

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

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

        if($toBrowser)
        {
            exit(0);
        }
    }
}
</code></pre>
<p>Somehow this piece of code</p>
<pre><code>if($toBrowser)
{
    exit(0);
}
</code></pre>
<p>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.</p>
<p>Still find it weird it gave no errors whatsoever in the Craft CMS logs.</p>
<p>Hope I can help someone out with this.</p>

CC BY-SA 4.0
2023-02-06T15:51:55.687Z
2023-02-06T15:51:55.687Z
wesley
<p>I can also confirm that using a template hook instead of a twig variable doesn't help.</p>
<p>I also downgraded Craft to 4.0.6 and that also has no affect.</p>

CC BY-SA 4.0
2023-01-19T14:26:41.310Z
2023-01-19T14:26:41.310Z
Brian Norris
<p>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...</p>
<ul>
<li>I can reproduce locally, so I don't think it's a server issue.</li>
<li>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.</li>
<li>MPDFs WriteText method also doesn't work, so I don't think it's an HTML parsing issue.</li>
<li>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.</li>
</ul>
<p>Similarly no errors, the PDF is created, it's just empty/doesn't render.</p>
<p>There is nothing documented about Craft's Dev Mode that seems like it would have any affect on this.</p>
<p>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.</p>
<p>Craft CMS Version: 4.3.6.1</p>
<p>MPDF: 8.1.2</p>

CC BY-SA 4.0
2023-01-18T18:13:12.957Z
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
CC BY-SA 4.0
2023-01-18T15:46:17.220Z
2023-03-01T10:01:44.223Z
2023-03-01T10:01:44.223Z
Brian Norris
<p>I have an issue with generation a pdf with MPDF.</p>
<p>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.</p>
<p>Anyone have a idea what could be the issue?</p>
<p>Edit:
I'm using Craft CMS 3, it's happening aswell on my local environment when I set the .env file to production.</p>

CC BY-SA 4.0
2023-01-17T15:05:18.167Z
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
CC BY-SA 4.0
2023-01-17T15:20:25.233Z
@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
CC BY-SA 4.0
2023-01-18T00:14:26.110Z
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
CC BY-SA 4.0
2023-01-18T18:24:33.747Z
@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
CC BY-SA 4.0
2023-01-18T23:26:50.477Z
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.
Brian Norris
<devmode><pdf><dompdf>
MPDF not working when devmode is not enabled
# MPDF not working when devmode is not enabled

(Asked by: username_0 on 2023-01-17)

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.

**Tags:** devmode, pdf, dompdf
> **Comments:**
> **username_1**: 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.  
> **username_0**: @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  
> **username_2**: 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?  
> **username_0**: @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.  
> **username_3**: 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.  

---

## 4 answers

### Answer by username_3 on 2023-01-20. Score: 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>

> **Comments:**
> **username_0**: I'll try this later this week. Huge thanks for your possible answer!!!  
> **username_0**: 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  
> **username_3**: dompdf is harder to work with than MPDF, for sure. SVGs are likely a no-go, fonts _should_ work though.  

---

### Answer by username_0 on 2023-02-06. Score: 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.


---

### Answer by username_3 on 2023-01-19. Score: 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.


---

### Answer by username_3 on 2023-01-18. Score: 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

> **Comments:**
> **username_0**: 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?  

---

