I have managed to use the below snippet of code, to open a pdf in a browser.Instead of opening in the same page, I would like it to open in a new browser tab.
I am not using an tag. This piece of code invokes a number of actions and at the end it is supposed to display the pdf. It works fine, but i would like it to open in a new tab.
Is this possible? and if so could you please explain to me how to do so.
Im using a Magento (EE 12.02) application and its on php 5.3.
$file = $pdf_file_path;
$filename = $file_name;
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);
You can't do that from that request.
But you can open the link in a new browser window by adding target="_blank" to your a tag, then browsers usually will use a new tab.
You can open a new tab while opening your pdf document. Example: if the code that opens your pdf document is on a page called pdfdocument.php,
that is some code like so
$pdf = file_get_contents($PDFfilename);
header('Content-Type: application/pdf');
header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Content-Length: '.strlen($pdf));
header('Content-Disposition: inline; filename="'.basename($PDFfilename).'";');
ob_clean();
flush();
echo $pdf;
and you have a link to the page such as http://www.example.com/pdfdocument.php, you can display it in a new tab like so click here to show pdf preview
to open it in other browser tab, you should do it in the html that reference it: <a href="..." target="_blank">Download pdf
Just add exit() at the end:
$file = $pdf_file_path;
$filename = $file_name;
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);
exit();
this should just trigger the download of the file.
To open it in a new tab if you are using <a> tag you can append a target attribute like this:
<a href="your_pdf_generator_link" target="_blank" > TEXT </a>
If you have a button you can apply an inline javascript such as
<input type="button" onclick="window.open('your_pdf_generator_link','_blank')" />
Hope this helps..
Related
Am trying to figure out how does one make myimage.jpg part of the trail end point of an hyperlink?
So far I have got the image to download but not sure how to make the hyperlink look like:
http://192.168.0.11/myimage.jpg
My code so far:
<?php
$file = "./my_image.jpg";
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;
}
?>
You can use echo to print your $file variable at the end of your link.
Like this,
<a href="//192.168.0.11/<?php echo $file; ?>">
This will create the link look this in HTML,
<a href="//192.168.0.11/myimage.jpg">
A friend of mine configured h2ml2canvas for me as I don't understand javascript. When saving using h2ml2canvas it generates a random filename e.g.
df0e604b2962492165eb8f2b31578171
Is there a way to specify a filename prefix? e.g. soccer then generate a random 3-4 digit number? Alternatively is there a way to open a save as dialogue instead of downloading an image on click? My download.php file.
<?php
$file = trim($_GET['path']);
// force user to download the image
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: image/png');
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);
unlink($file);
exit;
}
else {
echo "error not found";
}
?>
The filename in your case is actually generated (or not) by the PHP server-side, not the JavaScript you've quoted. When it returns the data to send back, it's including a Content-Disposition header, probably one that looks like this:
Content-Disposition: attachment
It's possible to suggest a filename to the browser by adding to that header:
Content-Disposition: attachment; filename=soccer123.xyz
In the PHP somewhere, you should find:
header("Content-Disposition", "attachment");
or similar. You can change it to:
header("Content-Disposition", "attachment; filename=soccer-" . rand(100,999) . ".xyz");
(Probably best to make the .xyz an appropriate extension for the type of image, e.g. .png or .jpg...)
Re your edit, you can replace:
header('Content-Disposition: attachment; filename='.basename($file));
with
header('Content-Disposition: attachment; filename=soccer-'.rand(100,999).'.xyz');
again you'll want a correct extension instead of .xyz.
I'm really struggling to get my application to open a pdf when the user clicks on a link.
So far the anchor tag redirects to a page which sends headers that are:
$filename='./pdf/jobs/pdffile.pdf;
$url_download = BASE_URL . RELATIVE_PATH . $filename;
header("Content-type:application/pdf");
header("Content-Disposition:inline;filename='$filename");
readfile("downloaded.pdf");
this doesn't seem to work, has anybody successfully sorted this problem in the past?
Example 2 on w3schools shows what you are trying to achieve.
<?php
header("Content-type:application/pdf");
// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename=\"downloaded.pdf\"");
// The PDF source is in original.pdf
readfile("original.pdf");
?>
Also remember that,
It is important to notice that header() must be called before any
actual output is sent (In PHP 4 and later, you can use output
buffering to solve this problem)
$name = 'file.pdf';
//file_get_contents is standard function
$content = file_get_contents($name);
header('Content-Type: application/pdf');
header('Content-Length: '.strlen( $content ));
header('Content-disposition: inline; filename="' . $name . '"');
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
echo $content;
There are some things to be considered in your code.
First, write those headers correctly. You will never see any server sending Content-type:application/pdf, the header is Content-Type: application/pdf, spaced, with capitalized first letters etc.
The file name in Content-Disposition is the file name only, not the full path to it, and altrough I don't know if its mandatory or not, this name comes wrapped in " not '. Also, your last ' is missing.
Content-Disposition: inline implies the file should be displayed, not downloaded. Use attachment instead.
In addition, make the file extension in upper case to make it compatible with some mobile devices. (Update: Pretty sure only Blackberries had this problem, but the world moved on from those so this may be no longer a concern)
All that being said, your code should look more like this:
<?php
$filename = './pdf/jobs/pdffile.pdf';
$fileinfo = pathinfo($filename);
$sendname = $fileinfo['filename'] . '.' . strtoupper($fileinfo['extension']);
header('Content-Type: application/pdf');
header("Content-Disposition: attachment; filename=\"$sendname\"");
header('Content-Length: ' . filesize($filename));
readfile($filename);
Technically Content-Length is optional but it is important if you want the user to be able to keep track of the download progress, and detect if the download was interrupted before the end. When using it you have to make sure you won't be send anything along with the file data. Make sure there is absolutely nothing before <?php or after ?>, not even an empty line.
I had the same problem recently and this helped me:
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="FILENAME"');
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("PATH/TO/FILE"));
ob_clean();
flush();
readfile(PATH/TO/FILE);
exit();
I found this answer here
Can you try this, readfile need the full file path.
$filename='/pdf/jobs/pdffile.pdf';
$url_download = BASE_URL . RELATIVE_PATH . $filename;
//header("Content-type:application/pdf");
header("Content-type: application/octet-stream");
header("Content-Disposition:inline;filename='".basename($filename)."'");
header('Content-Length: ' . filesize($filename));
header("Cache-control: private"); //use this to open files directly
readfile($filename);
You need to define the size of file...
header('Content-Length: ' . filesize($file));
And this line is wrong:
header("Content-Disposition:inline;filename='$filename");
You messed up quotas.
header("Content-type:application/pdf");
// It will be called downloaded.pdf thats mean define file name would be show
header("Content-Disposition:attachment;filename= $fileName ");
// The PDF source is in original.pdf
readfile($file_url);
<img src="images/android/android01.jpg"/>Android
links to:
php code:
$file = 'myfile.apk';
$filename ="MYAPP";
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.android.package-archive');
header("Content-Disposition:attachment; filename=\"$filename\"");
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);
exit;
}
I get an undefined output on Firefox desktop browser and phone. When I do a browser refresh, the download dialog starts and downloads the apk file.
Any ideas on how I can get around the undefined output in a blank page and go to download dialog immediatly?
Try this instead:
<a href="#" onClick="window.location='download.php'">
<img src="images/android/android01.jpg"/>Android
</a>
I'm trying to make a file that send a pdf file to the visitor.
I have file: download.php
and this is his content:
header('Content-disposition: attachment; filename='.$_GET['file']);
header('Content-type: application/pdf');
readfile($_GET['file']);
for some reason, the file sending an empty pdf file 183 bytes.
any advice?
thanks.
You should provide the full path to the file for readfile() not just a filename.
First off, you really should first check if the file exists with file_exists
Second ... this seems pretty insecure since you allow specifying the filename through a global $_GET parameter. What if I would try to download your config file like download.php?file=../application/settings/config.ini? You should first filter the $_GET parameter and make sure the file specified is allowed for being downloaded.
Try this:
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: application/pdf');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($filepath)) . ' GMT');
header('Content-disposition: attachment; filename=' . $pathinfo['filename'] . '.pdf');
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($filepath)); // provide file size
header('Connection: close');
readfile($filepath);
exit();