使用 javascript 在 IFrame 中打印 PDF 文件,仅获取一页
Print PDF File in IFrame using javascript getting one page only
这是我打印PDF文件的代码。 在这里,而打印时间我只得到一页,我需要解决方案。
function printPdf(){
var ifr = document.getElementById("frame1");
//PDF is completely loaded. (.load() wasn't working properly with PDFs)
ifr.onreadystatechange = function () {
if (ifr.readyState == 'complete') {
ifr.contentWindow.focus();
ifr.contentWindow.print();
}
}
}
我怀疑这是因为整个窗口都被打印出来了(它具有iframe
的当前视图,并渲染了PDF的第一页)。请改用<object>
:
<!DOCTYPE html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<script>
function PrintPdf() {
idPrint.disabled = 0;
idPdf.Print();
}
function idPdf_onreadystatechange() {
if (idPdf.readyState === 4)
setTimeout(PrintPdf, 1000);
}
</script>
</head>
<body>
<button id="idPrint" disabled=1 onclick="PrintPdf()">Print</button>
<br>
<object id="idPdf" onreadystatechange="idPdf_onreadystatechange()"
width="300" height="400" type="application/pdf"
data="test.pdf?#view=Fit&scrollbar=0&toolbar=0&navpanes=0">
<span>PDF plugin is not available.</span>
</object>
</body>
此代码使用 IE 进行验证。其他浏览器仍会呈现 PDF,但可能无法打印。
[更新] 如果您需要动态加载和打印,对上述代码的更改很小:
<!DOCTYPE html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<script>
function PrintPdf() {
idPdf.Print();
}
function idPdf_onreadystatechange() {
if (idPdf.readyState === 4)
setTimeout(PrintPdf, 1000);
}
function LoadAndPrint(url)
{
idContainer.innerHTML =
'<object id="idPdf" onreadystatechange="idPdf_onreadystatechange()"'+
'width="300" height="400" type="application/pdf"' +
'data="' + url + '?#view=Fit&scrollbar=0&toolbar=0&navpanes=0">' +
'<span>PDF plugin is not available.</span>'+
'</object>';
}
</script>
</head>
<body>
<button id="idPrint" onclick="LoadAndPrint('http://localhost/example.pdf')">Load and Print</button>
<br>
<div id="idContainer"></div>
</body>
<iframe src="teste.pdf" id="meupdf" width="800" height="600" />
function printPdf) {
var PDF = document.getElementById("meupdf");
PDF.focus();
PDF.contentWindow.print();
}
相关文章:
- 通过ajax到php文件获取单选按钮值
- jquery自动完成从本地文件获取数据
- 从外部js文件获取变量
- 应用程序脚本-自动从谷歌驱动器删除超过3天的文件-获取文件列表
- Ajax请求从php文件获取数据
- 从Javascript文件获取值
- 如何从外部JavaScript文件获取函数参数
- AngularJS从JSON本地文件获取数据
- 如何从另一个文件获取 HTML 元素值
- 从外部 json 文件获取数据
- 从HTML页面执行外部.js文件,通过发送输入.xml并从外部文件获取输出.xml.js文件
- 将数据从单独的 JavaScript 文件获取到数组中
- jQuery Ajax 文件获取问题
- AJAX/Jquery - 从 php 文件获取响应
- 根据 JSON 文件获取对象的架构
- 如何从输入类型=文件获取数据,修改它,然后将其插入到此输入JS中
- HTML和Javascript:使用输入文件获取完整的目录路径名
- 从本地文件获取JSON的角度服务不起作用
- 尝试从我的节点服务器加载脚本文件 - 获取 404
- 需要从外部文件获取JSON