Error downloading pdf with php

2

I am trying to make the user download a pdf file with php, the problem is that when the files are downloaded I get the following error:

My code is as follows:

$file="$file.pdf";
$filename = $this->config->item('RUTA_CFDi').$file;
header("Content-disposition: attachment; filename=".$filename);
header("Content-type: application/pdf");
readfile($filename);

Already verify that the files are good, the strange thing is that some files if you download them well and another does not, they leave with the error of the image.

I do not know if anyone knows that this error is due, and how I can fix it, or some other way to download pdf files, with php.

    
asked by José Gregorio Calderón 07.07.2017 в 19:06
source

2 answers

1

I had a similar problem when creating a pdf file, I obtained all the information from the database and it gave me an error when the image did not go up well so it did not exist. I noticed that until I verified each image if it was inserted.

    
answered by 07.07.2017 в 21:52
-3

Normally it will be enough to put a link to the PDF , inside the HTML code, as if you were linking any other resource If the PDF is in the location to which the link points, it will open or download automatically.

How you handle the loading of PDFs, their creation, etc. is independent of how you will arrange your download. Unless you are generating them at the moment.

Edited.

This answer is to the specific problem:

  

Error downloading pdf with php

     

I'm trying to make the   user download a pdf file with php, the problem is that when   download the files I get the following error:

that is, I indicate the necessary HTML code to be able to download a PDF file , how it is loaded, generated, etc. are other issues that may require the rethinking of this post in whose current content is they mix several concepts.

For a PDF to be downloadable , only an HTML link is required that links the resource and places it in the public folder of a website (I have linked it with an absolute path):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Descarga PDF</title>
</head>
<body>

    <a target="_blank" href="http://www.cambialibros.es/public/test.pdf">pdf ejemplo</a>
</body>
</html>

If you create an HTML file with the above code and open it in your browser, when you click, the PDF file will be opened or downloaded.

    
answered by 07.07.2017 в 19:31