6

I'm using mpdf to create PDF files on the fly, and the files open fine in a browser but Adobe gives me an error:

Adobe Acrobat Reader DC could not open 'example-filename.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).

I looked at other questions about this (another mpdf + adobe error), and checked out the pdf in a text editor. I found that the first part of the file looked like this:

<!DOCTYPE html>
<head>
    <title>
        CapstoneDB
    </title>
    %PDF-1.4
%âãÏÓ

After I removed everything up to %PDF-1.4 (including the tab), the file opened fine in Adobe, which is great, except that I need to be able to get the generated pdfs to open in Adobe without manually fiddling with the code every time.

Here's my wrapper function that calls mpdf with the html and css:

include('../mpdf/mpdf.php');

function user_download_pdf($html, $css_file, $filename) {
    $mpdf = new mPDF();
    $stylesheet = file_get_contents($css_file);
    $mpdf->WriteHTML($stylesheet,1);
    $mpdf->WriteHTML($html,2);
    $mpdf->Output($filename, "D");
}

I never feed mpdf a full html page, usually just an h3 and one or more tables. Maybe I need to be giving mpdf an entire html page, including <head>, <body>, etc? Is there any way to change mpdf configuration or the way I call mpdf in php that would get rid of the html junk at the beginning of the pdf file that's gunking everything up?

Community
  • 1
  • 1
Potato_potato
  • 89
  • 1
  • 5
  • 2
    It seems more likely that some other part of your PHP code is outputting the header before getting to the PDF generator. – Adam B Feb 08 '16 at 18:03
  • I work with mpdf and if it is rendered on an webpage, sure the file is ok and so your code. Maybe the problem lies in Adobe, which is very probable. Why not use an open source reader? – digitai Feb 08 '16 at 18:03
  • Check if the downloaded file opens with other pdf reader like SUMATRA, FOXIT, if you're using linux, there are excellent readers, GNOME brings its own Document Viewer is called. – digitai Feb 08 '16 at 18:07
  • @datelligence Unfortunately moving to a totally different PDF reader isn't feasible in this case, because the pdf files generated are primarily for other users of the website (a student management system). If it was just me, I'd be happy enough that the pdf opens in browsers. I tried Nitro and the pdfs open fine there, so yes, the issue seems to be that Adobe Acrobat Reader DC is picky. – Potato_potato Feb 08 '16 at 18:19
  • Maybe is an issue for what adobe is intolerant, while others not, so I would check @AdamB recommendation. – digitai Feb 08 '16 at 18:21
  • @Potato_potato first of all, save your pdf to the server and download-it in standard way (by direct URL), then open it with acrobat. if they works, the problem is in the sending procedure, otherwise the problem is in the pdf itself. – fusion3k Feb 08 '16 at 18:21

1 Answers1

17

Place

ob_clean(); 

immediately before

$mpdf->Output();

Without this mpdf sometimes includes the website page HTML and not just the HTML that you want in the PDF, probably because headers have already been sent elsewhere in the code. That can mess up your PDF so Adobe won't open it.

halfer
  • 19,824
  • 17
  • 99
  • 186
Markol
  • 236
  • 4
  • 13