【发布时间】:2016-10-01 08:14:01
【问题描述】:
我在服务器上托管了 pdf 文件,并且希望当用户单击链接以打印 pdf 文件时,应该打开打印对话框,用户可以从那里打印该 PDF 文件。它应该可以在 Chrome、Firefox 和 Safari 浏览器中运行。
请让我知道如何在 PHP/jQuery 或 html/jQuery 中做到这一点?
【问题讨论】:
标签: javascript php jquery html printing
我在服务器上托管了 pdf 文件,并且希望当用户单击链接以打印 pdf 文件时,应该打开打印对话框,用户可以从那里打印该 PDF 文件。它应该可以在 Chrome、Firefox 和 Safari 浏览器中运行。
请让我知道如何在 PHP/jQuery 或 html/jQuery 中做到这一点?
【问题讨论】:
标签: javascript php jquery html printing
尝试以下代码一次
$filepath = "path/to/file.pdf";
$filename = "file.pdf";
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($filepath)); // File size
header('Content-Encoding: none');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename=' . $filename); // Make the browser display the Save As dialog
readfile($filepath); // This is necessary in order to get it to actually download the file, otherwise it will be 0Kb
注意:这只是对 HTTP 协议的扩展;无论如何,一些浏览器可能会忽略它。
【讨论】: