I am trying to generate a pdf file with php, but it generates a corrupted file

Asked

Viewed 282 times

1

Follow the file code to display the pdf. This pdf is saved in the database. On an old php machine it works. But on the online server it generates the corrupted pdf file.

<?php

if (!empty($_GET['id'])) {
    $file = $banco = $banco->GetRow("SELECT * FROM up_danf WHERE id = '" . $_GET['id'] . "'");
}

if (!empty($file['nome'])) {
    header("Content-Type: application/" . $file["tipo"] . "");
    header("Content-Length: " . $file["tamanho"] . "");
    header("Content-Disposition: attachment; filename=" . $file["nome"] . "");
    header("Content-Description: PHP Generated Data");
    header('Cache-Control: private');
    header('Pragma: private');

    header("Expires: 0");
    session_cache_limiter('public');

    print $file["conteudo_arquivo"];
}
?>

I put the remote header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); and commented header("Expires: 0");

continued to function.

maybe help the version of php I’m using is: PHP Version 5.3.17

  • Sometimes this happens due to html or other text output that goes along, check it out. Related: What is the difference between readfile and file_get_contents?

  • Do a test by adding this header with date in the past header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); If it works out it returns a feedback here to us, I’ve been through this problem and it has to do with browser cache control. I used the header with expires in the past and printed the PDF.

  • I put the header command ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); and commented header("Expires: 0"); continued to not work. may help the version of php I’m using: PHP Version 5.3.17

  • Comment on the line header("Content-Type: application/" . $file["tipo"] . "");, is likely to be some problem when printing (print $) and not us headers.

  • I commented the line, and it didn’t work either.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.