我已经设法使用下面的代码片段在浏览器中打开一个 pdf。而不是在同一页面中打开,我希望它在新的浏览器选项卡中打开。
我没有使用标签。这段代码调用了许多操作,最后它应该显示 pdf。它工作正常,但我希望它在新选项卡中打开。
这可能吗?如果可以,请向我解释如何操作。
我在 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);
Run Code Online (Sandbox Code Playgroud)
您可以在打开 pdf 文档的同时打开一个新标签。示例:如果打开您的 pdf 文档的代码位于名为pdfdocument.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;
Run Code Online (Sandbox Code Playgroud)
并且您有一个指向该页面的链接,例如http://www.example.com/pdfdocument.php,您可以像这样在新选项卡中显示它 <a href="http://www.example.com/pdfdocument.php" target="_blank"> click here to show pdf preview </a>