简体   繁体   中英

pdf (mpdf) file gets corrupted in javascript

I have a web app, where pdfs need to be generated dynamically. Data is gathered in javascript and send with a post request to the server. Here mpdf is used to generate a pdf. If I save the file locally: php $mpdf->Output($filename, \Mpdf\Output\Destination::FILE); it works.

But if I send it to the browser php $mpdf->Output($filename, \Mpdf\Output\Destination::DOWNLOAD); and take the output in the jquery callback to do the following (borrowed from https://nehalist.io/downloading-files-from-post-requests/ ):

jQuery.post(my_axax_url, data, function(data) {
  var blob = new Blob([data], { type: 'application/pdf' });
  var l = document.createElement('a');
  l.href = window.URL.createObjectURL(blob);
  l.download = 'test.pdf';
  document.body.appendChild(l);
  l.click();
});

The downloaded pdf is empty (blank page), and has corrupted author information (that makes it look, like an encoding problem). I ran https://www.datalogics.com/products/pdftools/pdf-checker/ , which only gave me it the javascript generated pdf is a "Damaged document".

I hope, that this is an easy problem. I am used to php and text documents, not pdf.

Thanks!

Try adding the following to the start of your php script, it may be some sort of encoding issue:

ob_clean();
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="test.pdf"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');

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