在移动设备上使用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在新窗口中打开,用户可以选择手动保存。 / p>
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.
推荐答案
如果您使用Cordova平台进行开发,这里是一个例子:
Here is the example if you're using the Cordova platform for your development:
< a href =https://github.com/saharcasm/Cordova-jsPDF-Email =nofollow noreferrer> https://github.com/saharcasm/Cordova-jsPDF-Email
未在设备中下载的PDF的解决方法是使用cordova-plugin-file。
The workaround of the pdf not being downloaded in the devices is to use cordova-plugin-file.
使用输出法在文件上,将给你需要编写的原始pdf保存在一个文件中。
Use the output method on the doc that will give you the raw pdf which needs to be written & saved in a file.
例如,
var doc = new JsPDF();
//... some work with the object
var pdfOutput = doc.output("blob"); //returns the raw object of the pdf file
然后通过使用pdfOutput将其写入实际文件文件插件。
The pdfOutput is then written on an actual file by using the file plugin.
这篇关于在移动设备上使用jsPDF下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!