在移动设备上使用 jsPDF 下载 [英] Download using jsPDF on a mobile devices
问题描述
我有一个包含使用 jsPDF 的下载按钮的页面.在台式机上,它会按原样下载页面.但是,pdf.save()
在我的平板电脑或手机上不起作用.
I have a page that includes a download button using jsPDF. On desktop machines it downloads the page as it should. However, pdf.save()
does not work on my tablet or phone.
我尝试为移动设备添加一个特殊情况,以便在新窗口中打开 PDF,因为移动设备下载的内容与台式机不同,其想法是一旦 PDF 在新窗口中打开用户可以选择手动保存.
I tried to add a special case for mobile devices to open the PDF in a new window, since mobile devices don't download things the same as desktops, with the idea being that once the PDF is open in a new window the user can choose to save it manually.
var pdf = new jsPDF('p', 'pt', 'letter');
var specialElementHandlers = {
'#editor': function (element, renderer) {
return true;
}
};
html2canvas($("#pdf-area"), {
onrendered: function (canvas) {
$("#pdf-canvas").append(canvas);
$("#pdf-canvas canvas").css("padding", "20px");
}
});
var options = {
pagesplit: true
};
function download(doctitle) {
pdf.addHTML($("#pdf-area")[0], options, function () {
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
pdf.output('dataurlnewwindow');
} else {
pdf.save(doctitle);
}
});
}
但下载功能在我的平板电脑/手机上仍然没有任何作用.我用这个测试它以确保 pdf.output() 函数正常工作:
But the download function still does nothing on my tablet/phone. I tested it with this to make sure the pdf.output() function was working:
pdf.addHTML($("#pdf-area")[0], options, function () {
pdf.output('dataurlnewwindow');
});
它仍然可以在桌面上运行,但在移动设备上没有任何作用.
and it does still work on desktop, but does nothing on mobile.
推荐答案
适用于桌面和移动设备的最简单方法是使用:
The easiest way which works on both Desktop and Mobile is to use:
window.open(doc.output(bloburl"), _blank");
这篇关于在移动设备上使用 jsPDF 下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!