CodeIgniter Forums
force pdf download - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: force pdf download (/showthread.php?tid=2455)



force pdf download - El Forum - 08-07-2007

[eluser]webdude[/eluser]
when i click on a pdf link (the value is help in the db, $data['content_pdf'] = $row->content_pdfWink it tries to open the link as a web page rather than getting a "save as" pop-up.

any ideas?


force pdf download - El Forum - 08-07-2007

[eluser]Crimp[/eluser]
http://ellislab.com/codeigniter/user-guide/helpers/file_helper.html


force pdf download - El Forum - 08-07-2007

[eluser]LuckyFella73[/eluser]
Did I get it right that the sourcecode of the "PDF file"
is an entry in your DB?
Then maybe you'll have to use the download helper additionally
(after creating the pdf file from your DB). So you could first
write the file, use the download-helper and delete the file
after the download is complete.
The CI download helper is great but in case your file
is too large it won't work anymore - I think it's because
of the PHP memory limit.
Just wanted to mention it because I had the same problem
a while ago.

Cheers,
Luckyfella

http://ellislab.com/codeigniter/user-guide/helpers/download_helper.html


force pdf download - El Forum - 10-10-2007

[eluser]easymind[/eluser]
...


force pdf download - El Forum - 10-18-2007

[eluser]Bacteria Man[/eluser]
When Adobe Acrobat (Reader) is installed this is usually the desired and expected behavior. An easy workaround is to zip the PDF.


force pdf download - El Forum - 10-18-2007

[eluser]easymind[/eluser]
In my previous post here I deleted this because I didn't know the force_download() helper function from CI. But CI wants you to put the data in a variable first. So maybe this still is interesting:
Code:
$filename = 'somefile.pdf';
$filepath = '/some/path/';
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // some day in the past to avoid cached files
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename={$filename}");
header("Content-Transfer-Encoding: binary");
readfile($filepath.$filename);

Readfile directly dumps the data. So maybe you won't get the problem of the memory limit in PHP. Otherwise you can read and dump a file part by part yourself with fopen wrappers...

Then again....
You have the file data in the db, so you probably have to put the data in a variable. If you want it that way for security I can advice you to put files in a folder outside your webroot and use the above code in a function to let people download the files and maybe do a user check first. This way you do not have direct urls to your files but still can have files that are bigger than 8mb (standard PHP memory limit size? or 16mb...don't know)


force pdf download - El Forum - 10-18-2007

[eluser]Bacteria Man[/eluser]
You know, that's pretty cool. I haven't ever needed to do that before, but it's bound to come in handy. Thanks very much for bringing it to my (our) attention.


force pdf download - El Forum - 12-15-2007

[eluser]jabbett[/eluser]
easymind--
Thanks for this bit of code... using force_download was appending a whole bunch of empty lines at the beginning of my PDF, and Foxit/Acrobat couldn't parse it. Your code did the trick!