我正在构建HTML格式的PDF列表.在列表中,我想要包含下载链接和打印按钮/链接.有没有办法在没有用户看到PDF或打开PDF查看器的情况下直接打开PDF的"打印"对话框?
将PDF下载到隐藏的iframe并触发使用JavaScript打印的一些变体?
nul*_*ity 54
此问题演示了一种可能对您有所帮助的方法:静默打印嵌入式PDF
它使用<embed>
标记将PDF嵌入到文档中:
<embed
type="application/pdf"
src="path_to_pdf_document.pdf"
id="pdfDocument"
width="100%"
height="100%" />
Run Code Online (Sandbox Code Playgroud)
然后.print()
在加载PDF时在Javascript中调用元素上的方法:
function printDocument(documentId) {
var doc = document.getElementById(documentId);
//Wait until PDF is ready to print
if (typeof doc.print === 'undefined') {
setTimeout(function(){printDocument(documentId);}, 1000);
} else {
doc.print();
}
}
Run Code Online (Sandbox Code Playgroud)
您可以将嵌入放置在隐藏的iframe中并从那里打印出来,为您提供无缝体验.
Nic*_*DIA 33
这是一个从iframe打印PDF的功能.
您只需将PDF的URL传递给该函数即可.一旦加载PDF,它将创建一个iframe并触发打印.
请注意,该函数不会破坏iframe.相反,它每次调用函数时都会重用它.破坏iframe很难,因为在打印完成之前需要它,并且打印方法没有回调支持(据我所知).
printPdf = function (url) {
var iframe = this._printIframe;
if (!this._printIframe) {
iframe = this._printIframe = document.createElement('iframe');
document.body.appendChild(iframe);
iframe.style.display = 'none';
iframe.onload = function() {
setTimeout(function() {
iframe.focus();
iframe.contentWindow.print();
}, 1);
};
}
iframe.src = url;
}
Run Code Online (Sandbox Code Playgroud)
use*_*203 17
从http://printjs.crabbly.com/下载Print.js
$http({
url: "",
method: "GET",
headers: {
"Content-type": "application/pdf"
},
responseType: "arraybuffer"
}).success(function (data, status, headers, config) {
var pdfFile = new Blob([data], {
type: "application/pdf"
});
var pdfUrl = URL.createObjectURL(pdfFile);
//window.open(pdfUrl);
printJS(pdfUrl);
//var printwWindow = $window.open(pdfUrl);
//printwWindow.print();
}).error(function (data, status, headers, config) {
alert("Sorry, something went wrong")
});
Run Code Online (Sandbox Code Playgroud)
小智 12
https://github.com/mozilla/pdf.js/
现场演示http://mozilla.github.io/pdf.js/
它可能就是你想要的,但我看不出这一点,因为现代浏览器包含这样的功能,而且它在移动设备等低功耗设备上的运行速度非常慢,顺便提一下,它们有自己的优化插件和应用程序.
我使用此功能从服务器下载 pdf 流。
function printPdf(url) {
var iframe = document.createElement('iframe');
// iframe.id = 'pdfIframe'
iframe.className='pdfIframe'
document.body.appendChild(iframe);
iframe.style.display = 'none';
iframe.onload = function () {
setTimeout(function () {
iframe.focus();
iframe.contentWindow.print();
URL.revokeObjectURL(url)
// document.body.removeChild(iframe)
}, 1);
};
iframe.src = url;
// URL.revokeObjectURL(url)
}
Run Code Online (Sandbox Code Playgroud)
从 base64 字符串打印 pdf 的跨浏览器解决方案:
.
const blobPdfFromBase64String = base64String => {
const byteArray = Uint8Array.from(
atob(base64String)
.split('')
.map(char => char.charCodeAt(0))
);
return new Blob([byteArray], { type: 'application/pdf' });
};
const isIE11 = !!(window.navigator && window.navigator.msSaveOrOpenBlob); // or however you want to check it
const printPDF = blob => {
try {
isIE11
? window.navigator.msSaveOrOpenBlob(blob, 'documents.pdf')
: printJS(URL.createObjectURL(blob)); // http://printjs.crabbly.com/
} catch (e) {
throw PDFError;
}
};
printPDF(blobPdfFromBase64String(base64String))
Run Code Online (Sandbox Code Playgroud)
奖励 - 在 IE11 的新选项卡中打开 blob 文件
如果您能够在服务器上对 base64 字符串进行一些预处理,则可以在某个 url 下公开它并使用printJS
:) 中的链接
归档时间: |
|
查看次数: |
232658 次 |
最近记录: |