I cannot open pdf in chrome created by Mpdf - CodeIgniter . - Failed to load PDF document I cannot open pdf in chrome created by Mpdf - CodeIgniter . - Failed to load PDF document codeigniter codeigniter

I cannot open pdf in chrome created by Mpdf - CodeIgniter . - Failed to load PDF document


Your PDF file contains some direct PHP output from your script which makes it corrupt.

It can be a whitespace from after the ?> PHP closing tag, it can be PHP notice printed during course of generating the file, etc.

Be sure that you disable all layouting your framework provides as it produces the exact same output that could cause this.


To troubleshoot further, save your PDF file to disc ($mpdf->Output('<file path>', "D");) and open it in a text editor. You will see a bunch of weird characters.

If the file does not start with %PDF-1.4, look for the reason of the output before calling $mpdf->Output();.

If there is some readable text at the end of the document, look after calling the Output method.


I'd suggest you to remove your ?> PHP closing tag anyway for a good measure.

See also https://mpdf.github.io/troubleshooting/error-messages.html


The reason this happens is that there are some PHP errors/warnings that show up before the PDF gets written, these make it a non-valid PDF therefore not readable by the Browser.

You can see it by downloading and opening the PDF file with a TextEditor and you'll see PHP errors instead of the standard %PDF-1.4 value (which should be the first value of the file).

If you suppress warnings error_reporting(E_ERROR | E_PARSE); you should be good to go.


Try this

$mpdf->Output('', "I"); 

so then file should be displayed inline in browser instead of downloading it.

if you want to download use "D" insted of "I"