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>
Related
I have a library website to provide digital document to user, but i need change show direct document link by third party provider ( as https://cdn.xxx.xxx/abc.pdf ) to link through by php code ( for authen user).
I user file_get_contents to get link and show browser but have problem with big pdf file ( more than 200M ), it process and view take a lot of time and it not make good user exp.
Do anyone have solution for this case ?
My code:
filepath = "https:/cdn.xxx.xxx/a.pdf";
header("Content-type: application/pdf");
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
#readfile($filepath);
But pdf more than 200M, it display very slow, I need it can display as stream not only show blank page.
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
In my program I want to add a download option to download the currently straming video. I tried this code:
$psp = "Tom_20_amp__20Jerry_20race-1.flv";
header("Content-type:application/octet-stream");
header("Content-Disposition:attachment;filename=$psp");
But I get this error:
"C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\Tom_20_amp__20Jerry_20race-1-5.flv" is not a valid FLV file.
the video streaming properly. please guide me
Change you're header from :
header("Content-type:application/octet-stream");
To :
header("Content-type: video/flv");
Then you can do :
header("Content-Disposition:attachment;filename=\"$psp\"");
//allways a good idea to let the browser know how much data to expect
header("Content-length: " . filesize($psp) . "\n\n");
echo file_get_contents($psp); //$psp should contain the full path to the video
If you are looking to allow files to be downloaded instead of streamed then something like this should work. You will obviously need to change the paths.
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="download.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
EDIT
In your case it will be something like this.
// We'll be outputting a video
header('Content-type: video/flv');
// It will be called video.flv
header('Content-Disposition: attachment; filename="video.flv"');
// The PDF source is in original.flv
readfile('original.flv');
I am working on a website where the visitor should be able to download a pdf file.
(There are three links to choose from but that is irrelevant)
I wanted to know how to make it so that the visitor can simply click the link and not have to
right click > Save (target) As...
I am open to PHP and or Javascript solutions. Thanks.
EDIT: Can I use javascript to call the PHP and save the file via AJAX?
EDIT2: I used Nirmal's solution in the end, since it was the simplest to change for all three files. I didn't need to make 3 files for the three PDF's and I didn't need to hand code the switch. BalusC gets the check though since his/her code was up first and does the trick too.
All you basically need to do is to set the Content-Disposition header to attachment to get a 'Save As' dialogue. Here's a kickoff PHP example:
<?php
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="foo.pdf"');
readfile('/path/to/foo.pdf');
?>
You can't and don't want to do this with Javascript.
Important note: due to a poor feature, in MSIE the default filename in 'Save As' dialogue won't be derived from the content-disposition header, it will instead be the last part of the pathinfo in the request URL. To workaround this, append the PDF filename to the link, e.g. http://example.com/pdf/foo.pdf. You can even make use of it in PHP to read the in the pathinfo specified PDF file. Here's a basic example of pdf.php:
<?php
$file_name = $_SERVER['PATH_INFO'];
$file = '/path/to/pdf/files' . $file_name;
if (file_exists($file)) {
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="' . basename($file_name) . '"');
header('Content-Length: ' . filesize($file));
readfile($file);
} else {
header('HTTP/1.1 404 Not Found');
}
?>
This however assumes that you've MultiViews on so that /pdf/ will go through this PHP file, or at least a RewriteRule from /pdf/ to /pdf.php/.
The major advantage of this approach is that you don't need to change the code whenever you want to add a new PDF file or change the PDF file name.
You can even make it more generic by automatically determining and setting the correct content type:
<?php
$file_name = $_SERVER['PATH_INFO'];
$file = '/path/to/all/files' . $file_name;
if (file_exists($file)) {
header('Content-Type: ' . mime_content_type($file_name));
header('Content-Disposition: attachment;filename="' . basename($file_name) . '"');
header('Content-Length: ' . filesize($file));
readfile($file);
} else {
header('HTTP/1.1 404 Not Found');
}
?>
Name it files.php or so and then you have a generic PHP downloader which you can access by for example http://example.com/files/foo.pdf, http://example.com/files/bar.zip, etcetera.
Hope this helps.
Rather than having to write PHP wrapper scripts, if you are using Apache, you can do this with a .htaccess file in the folder containing the PDFs:
<Files *.pdf>
Header set Content-Disposition attachment
</Files>
Apparently some versions of IE / Adobe Reader don't respect the Content-Disposition header. You can work around these with ForceType application/octet-stream
<Files *.pdf>
ForceType application/octet-stream
Header set Content-Disposition attachment
</Files>
If you don't want to mess with server side code and if this is a low priority thing for you, you can use HTML5 download attribute.
Example:
<a href="myfile.pdf" download>Download</a>
However this is not supported by all browser. As per w3schools, following browsers are supported:
Chrome (14+), Firefox(20+), Opera (15+).
You can also specify filename by giving value to download attr:
Download
Try using PHP to serve the file while first sending a header of Content-type: application/octet-stream.
You can add an HTTP header to do that, there is an example in the PHP Docs (See example one)
The following code may help you:
<?php
if(isset($_GET['docid'])){
switch($_GET['docid']){
case '1':
$file = 'complete/path/to/pdf/file1';
break;
case '2':
$file = 'complete/path/to/pdf/file2';
break;
case '3':
$file = 'complete/path/to/pdf/file3';
break;
default:
exit;
}
if(file_exists($file)){
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, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
}
Save this code as a php file (say, download.php) and call it from the link. What the script does is that it reads the pdf file and outputs it to the buffer. The headers will force the disposition as download.
To call the first pdf, href to '/path/to/download.php?docid=1'
To call the second pdf, href to '/path/to/download.php?docid=2'
To call the third pdf, href to '/path/to/download.php?docid=3'
So, you don't need AJAX to do the work.
In a web application I am working on, the user can click on a link to a CSV file. There is no header set for the mime-type, so the browser just renders it as text. I would like for this file to be sent as a .csv file, so the user can directly open it with calc, excel, gnumeric, etc.
header('Content-Type: text/csv');
echo "cell 1, cell 2";
This code works as expected on my computer (Isn't that how it always is?) but does not work on another computer.
My browser is a nightly build of FF 3.0.1 (on linux). The browsers it did not work in were IE 7 and FF 3.0 (on windows)
Are there any quirks I am unaware of?
You could try to force the browser to open a "Save As..." dialog by doing something like:
header('Content-type: text/csv');
header('Content-disposition: attachment;filename=MyVerySpecial.csv');
echo "cell 1, cell 2";
Which should work across most major browsers.
You are not specifying a language or framework, but the following header is used for file downloads:
"Content-Disposition: attachment; filename=abc.csv"
With Internet Explorer you often have to specify the Pragma: public header as well for the download to function properly..
header('Pragma: public');
Just my 2 cents..
This code can be used to export any file, including csv
// application/octet-stream tells the browser not to try to interpret the file
header('Content-type: application/octet-stream');
header('Content-Length: ' . filesize($data));
header('Content-Disposition: attachment; filename="export.csv"');
I tried to use text/csv but it did not work for me after that tried different stuff and I figured out that if we use this text/plain. Now the file upload is completed. as expected. This problem was in the Yii2 file upload widget.
Example response after success.
{"files":[{"name":"unit_ids list - Sheet1.csv","type":"text/plain","size":30,"base_url":"https://s3-eu-west-1.amazonaws.com/cdn.abc.co","path":"1/g2qVy3JtyZBLaRUd8c5gMOtSyrTEwdzR.csv","url":"https://s3-eu-west-1.amazonaws.com/cdn.abc.co/1/g2qVy3JtyZBLaRUd8c5gMOtSyrTEwdzR.csv","delete_url":"/coupons/default/sheet-delete?path=1%2Fg2qVy3JtyZBLaRUd8c5gMOtSyrTEwdzR.csv"}]}