I have been using codeigniter framework. It was going very with everything except that I can't read my pdf files. When I do try reading, It downloads or saves it instead. I have tried a lot of solutions but didn't work.
Controller
public function read($name)
{
$url = base_url().'uploads/'.$name;
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="'.$name.'"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
#readfile($url);
}
How can you read a pdf file? If by read you mean to read characters in the pdf file then you need an OCR library to do the same. You can find different libraries like:
http://www.fpdf.org/
checkout:
How to force files to open in browser instead of download (pdf)?
also if this doesn't help you try using a library like mpdf or dompdf they have good documentation on how to export files to browser or to save it etc
https://github.com/mpdf/mpdf
Related
im trying to make pdf files downloadable in my website but all im getting is the current page source(html).
the file name is correctly given but the file itself is not downloading.
ive tried various fixes found on stackoverflow but its not helping.
ive tried AddType application/octet-stream .pdf in htaccess , also ForceType.
Tried the php fix here:
How to make PDF file downloadable in HTML link?
and going through php with this:
header("Content-disposition: attachment; filename=filename.pdf");
header("Content-type: application/pdf");
readfile("filename.pdf");
and then linking to the php file, still the same.
what am i doing wrong and what information do you require to make better sense of this?
You can have serveral mistakes to check (and debug) try this
<?php
$file = ABSOLUTE_PATH_WHERE_PDF_IS_STORED.'/my.pdf'; //replace *ABSOLUTE_PATH_WHERE_PDF_IS_STORED* with your path
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
} else {
die("FILE [".$file."]" don't exists!");
}
?>
So i found the problem, which is of course quite obvious, i linked to the files wrongly, and since im using a cms it sent the front page source (default behaviour).
Peculiarly the html5 download attribute doesn’t work anyway.
Thank you Donald123 and PKa for answering.
You can use a tag with attribute download
<a href="path/to/file/*.pdf" download>Download this pdf</a>
Its a Quick way to do that.
<?php
header('Content-disposition: attachment; filename=Booking.pdf');
header('Content-type: application/pdf');
readfile('http://mysite.com/Booking.pdf');
?>
why is the Booking.pdf file downloaded empty!??
mac and windows both say:
The file “Booking.pdf” could not be opened because it is empty.
checked google and stackoverflow, can't find relative info... has anyone experienced this before?
ps: I only found this forum post:'The online issue is a bit off topic I think, but is generally due to loading the PDF to a server in the ASCII mode of FTP rather than binary. That creates a corrupt file. Be sure to turn on binary transmission', but this is not true in this case as i can display the same pdf file in an iframe and it is not blank/empty.
You need to change
readfile('http://mysite.com/Booking.pdf');
To
readfile(__DIR__ . '/Booking.pdf');
Example
$file = __DIR__ . '/test.pdf' ;
header('Content-disposition: attachment; filename=Booking.pdf');
header('Content-type: application/pdf');
header("Content-length: ".filesize($file));
readfile($file);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Forcing to download a file using PHP
When we need to force user to download a file, we use header with several parameters/options. What if I use
header("location:test.xlsx");
This is working :) Is there any drawback of using this shortcut ?
This approach should solve the problems mentioned here
download.php?filename=test.xlsx
if isset ($_GET['filename']){
$filename = $_GET['filename']
}
else{
die();
}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
And of course don't forget to secure this so users can't download other files
There are a few disadvantages to this method:
If the file is one the browser can read, it won't be downloaded (like .txt, .pdf, .html, .jpg, .png, .gif and more), but simply be shown within the browser
Users get the direct link to the file. Quite often, you don't want this because they can give this link to others, so...
it will cost you more bandwidth
it can't be used for private files
if it's an image, they can hotlink to it
All you're doing is redirecting to a file. This is no different than if they went to it directly.
If you are trying to force a download, you need to set your Content-Disposition header appropriately.
header('Content-Disposition: attachment');
Note that you can't use this header when redirecting... this header must be sent with the file contents. See also: https://stackoverflow.com/a/3719029/362536
Not every file is forced to download.
If you were to use that header() on a .jpg the browser won't open the download dialog but will just show the image.
I want to show my users PDF files. The reason why I use CGI to show the PDF is I want to track the clicks for the PDF, and cloak the real location of the saved PDF.
I've been searching on the Internet and only found how to show save dialog to the users and creating a PDF, not show the files to the users.
What I wanted for is show the users my PDF files, not creating or download the PDF.
Here is what I got form the official PHP documentation:
<?php
header('Content-type: application/pdf');
readfile('the.pdf');
?>
Also my google-search-result perl code:
open(PDF, "the.pdf") or die "could not open PDF [$!]";
binmode PDF;
my $output = do { local $/; <PDF> };
close (PDF);
print "Content-Type: application/pdf\n";
print "Content-Length: " .length($output) . "\n\n";
print $output
if you do it on ruby, please say it to me. But I'm not sure if my server support rails.
Sorry if my code is too far away from the method to show the pdf, since I don't know anything about pdf processing and how to implement this problem.
Lets assume that the users have the Adobe Reader plug-in. So, how to fix my problem?
edit : I want to show plain PDF file. My primary purpose: track my pdf files and use some fancy urls.
edit : Here's my main php code:
<?php
$file='/files/the.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="the.pdf"');
#readfile($file);
?>
edit : Now the code is working. But the loading progress bar (on Adobe Reader X plugin) doesn't shows up. Why? Anyone can help me? Here's my main code:
<?php
$file='./files/the.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="the.pdf"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
#readfile($file);
?>
edit : All my problems solved. Here's the final code:
<?php
$file = './path/to/the.pdf';
$filename = 'Custom file name for the.pdf'; /* Note: Always use .pdf at the end. */
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
#readfile($file);
?>
Thanks! :)
I assume you want the PDF to display in the browser, rather than forcing a download. If that is the case, try setting the Content-Disposition header with a value of inline.
Also remember that this will also be affected by browser settings - some browsers may be configured to always download PDF files or open them in a different application (e.g. Adobe Reader)
$url ="https://yourFile.pdf";
$content = file_get_contents($url);
header('Content-Type: application/pdf');
header('Content-Length: ' . strlen($content));
header('Content-Disposition: inline; filename="YourFileName.pdf"');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
ini_set('zlib.output_compression','0');
die($content);
Tested and works fine. If you want the file to download instead, replace
Content-Disposition: inline
with
Content-Disposition: attachment
You could modify a PDF renderer such as xpdf or evince to render into a graphics image on your server, and then deliver the image to the user. This is how Google's quick view of PDF files works, they render it locally, then deliver images to the user. No downloaded PDF file, and the source is pretty well obscured. :)
The safest way to have a PDF display instead of download seems to be embedding it using an object or iframe element. There are also 3rd party solutions like Google's PDF viewer.
See Best Way to Embed PDF in HTML for an overview.
There's also DoPDF, a Java based In-browser PDF viewer. I can't speak to its quality but it looks interesting.
You can also use fpdf class available at: http://www.fpdf.org.
It gives options for both outputting to a file and displaying on browser.
There is a simple solution using the embed tag:
<span class="fileShow">
<a href="aa.pdf" onclick="event.stopPropagation();" target="_blank">
<embed style="width:450px; height:300px; max-width:450px; max-height:300px" src="aa.pdf">
</a>
</span>
I've created a custom solution in WordPress that will generate a CSV file to be downloaded by clicking a simple hyperlink, linked directly to this file. Instead of being prompted to download the file to the computer; the CSV opens in the the browser window instead.
FWIW I'm on Media Temple using a vanilla install of WordPress.
Send the proper mime type
header('Content-type: text/csv');
And use the Content-Disposition header to tell it to download: http://www.jtricks.com/bits/content_disposition.html
header('Content-Disposition: attachment; filename="mycssfile.csv"');
You always want to send the proper mime type, otherwise firewalls, anti-virus software and some browsers may have issues with it...
You can use PHP's header() function to change Content-type
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="myFile.csv"');
The above code will force a prompt to the user for download. where myFile.csv should be replaced with the path to the file you want downloaded.
This works:
$filename = 'export.csv';
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
Also, I personally do not like links on my sites, I like buttons. If you want a button to do for the export function you can use the code below. I just thought I would post it because it took me a bit to figure out the first time :)
<input type="button" value="Export to CSV" onClick="window.location.href='something.php?action=your_action';"/>
You need to send the browser a MIME type of application/csv so it will offload the responsibility of handling the file to whatever the OS recommends (or user chooses).
In PHP (before any output is sent to the client):
header('Content-type: application/csv');