使用此 php 代码在新选项卡中打开 pdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12730581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use this php code to open a pdf in a new tab
提问by activeDev
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.
我已经设法使用下面的代码片段在浏览器中打开一个 pdf。而不是在同一页面中打开,我希望它在新的浏览器选项卡中打开。
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.
我没有使用标签。这段代码调用了许多操作,最后它应该显示 pdf。它工作正常,但我希望它在新选项卡中打开。
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.
我在 php 5.3 上使用 Magento (EE 12.02) 应用程序。
$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);
回答by Niko Sams
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.
但是您可以通过在 a 标签中添加 target="_blank" 来在新的浏览器窗口中打开链接,然后浏览器通常会使用新选项卡。
回答by user28864
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 文档的同时打开一个新标签。示例:如果打开您的 pdf 文档的代码位于名为pdfdocument.php的页面上,则是一些类似这样的代码
<?php
`$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>
并且您有一个指向该页面的链接,例如http://www.example.com/pdfdocument.php,您可以像这样在新选项卡中显示它 <a href="http://www.example.com/pdfdocument.php" target="_blank"> click here to show pdf preview </a>
回答by jodarove
to open it in other browser tab, you should do it in the html that reference it: <a href="..." target="_blank">Download pdf
要在其他浏览器选项卡中打开它,您应该在引用它的 html 中执行此操作:<a href="..." target="_blank">下载 pdf
回答by Efx
To open it in a new tab if you are using <a>tag you can append a target attribute like this:
如果您使用<a>标签,要在新选项卡中打开它,您可以附加一个目标属性,如下所示:
<a href="your_pdf_generator_link" target="_blank" > TEXT </a>
<a href="your_pdf_generator_link" target="_blank" > TEXT </a>
If you have a button you can apply an inline javascript such as
如果您有按钮,则可以应用内联 javascript,例如
<input type="button" onclick="window.open('your_pdf_generator_link','_blank')" />
<input type="button" onclick="window.open('your_pdf_generator_link','_blank')" />
Hope this helps..
希望这可以帮助..
回答by Miroslav
Just add exit() at the end:
只需在最后添加 exit() :
$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.
这应该只是触发文件的下载。

