Use this php code to open a pdf in a new tab
Asked Answered
A

6

7

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);
Algia answered 4/10, 2012 at 15:22 Comment(3)
I'm intrigued why you think the code doesn't work for early versions of IEMagee
good question, although im sorry to dissapoint, its just me assuming it would not work in earlier versions of I.E. please excuse me i will edit the quesion and remove that part.Algia
I don't know enough about early versions of IE to know if they had problems with PDF docs; in general though, don't forget that because PHP is serverside, it is generally browser-independentMagee
L
8

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.

Latimore answered 4/10, 2012 at 15:26 Comment(0)
H
6

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 <a href="http://www.example.com/pdfdocument.php" target="_blank"> click here to show pdf preview </a>

Halftruth answered 8/7, 2015 at 23:54 Comment(0)
P
1

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.

Pompea answered 4/10, 2012 at 15:24 Comment(3)
Hi Merca. I just added your suggestion and unfortunately it behaves in exactly the same fashion. it opens perfectly, in the same browser tab, and I would like it to open in a new tab instead. thanks for the suggestion!Algia
I don't think you can trigger a new tab opening with PHP. Unless you will add target=_blank on the link that downloads the PDFPompea
This helped me thank you I was trying to download a file on the same page without opening another tab.Urbai
B
1

to open it in other browser tab, you should do it in the html that reference it: <a href="..." target="_blank">Download pdf

Beckett answered 4/10, 2012 at 15:26 Comment(2)
Hi. Thanks for the suggestion. Unfortunately this happens during an action. the pdf is not available to present as a link at the time. instead I need to perform a number of operations to get the system in a state to present the pdf. So basically after a number of other actions, I would like to open a pdf in a seperate tab, and cannot do so via an <a> tag. thanks for the suggestion!Algia
ok, thanks, but you wont be able to do it from server side, unless you generate a page, with a redirect to the original page (to keep the original one) and open a tab in the browser (all this with javascript)Beckett
S
-1

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..

Soper answered 4/10, 2012 at 15:34 Comment(1)
Be careful, target="_blank" doesn't follow the w3c recommendations and inline javascript doesn't follow the modern javascript best practices.Alesandrini
C
-1

Replace "Content-Disposition: inline" with "Content-Disposition: attachment" and keep "filename" definition. That will force browser to open PDF in the new tab.

Cinquain answered 1/8, 2023 at 13:26 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Carnauba

© 2022 - 2025 — McMap. All rights reserved.