Jazerix
Jazerix

Reputation: 4801

Force download file that's not placed on the server

I'm trying to force download a pdf file that I'm generating. I don't need the pdf file to be actually saved on the server.

So when I generate my pdf file, I get the file content. I then encode it with base64. Now the problem is that I need to force download it. I've looked all over the web, but I haven't found any search results that tells me how to do this without the file actually being placed on the site.

I've tried the following code:

header("Content-type: application/pdf");
header("Content-disposition: attachment; filename=\"invoice.pdf\""); 
header("Content-Length: ".filesize($pdffile));
readfile(base64_decode($pdffile));

But, it's giving me a corrupt pdf file, (1 kb). The actual file should be around 50kb.

Any ideas, as to what I can try?

Upvotes: 0

Views: 62

Answers (1)

V G
V G

Reputation: 1225

readfile trying to output content from file, but you have only data string. Try this instead:

header("Content-type: application/pdf");
header("Content-disposition: attachment; filename=\"invoice.pdf\""); 
echo base64_decode($pdffile);

I also suggest rename $pdffile to $pdfcontent for even better clarification.

Upvotes: 2

Related Questions