简体   繁体   中英

Use this php code to open a pdf in a new tab

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

要在其他浏览器选项卡中打开它,您应该在引用它的 html 中执行此操作: <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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM