gpt4 book ai didi

javascript - Phonegap/jsPDF 将 html 保存为 pdf 的问题

转载 作者:行者123 更新时间:2023-12-03 12:45:19 25 4
gpt4 key购买 nike

我不是一个大量的 javascript/jquery 用户,但我已经开始更多地了解它在移动应用程序中的使用...我一直在寻找答案来解决我在尝试时出现空白页面的问题使用 jspdf 将 html 输出为 pdf,我发现的每篇文章都与 blob 有关。

这是我的代码,我有导出一个空白的pdf,我留下了pdf.save,所以我在我的电脑上导出了一个它应该是什么样子的样本,但在我的ipad和nexus 7上它保存了一个空白pdf.

var pdf = new jsPDF('p', 'pt', 'letter'), source = $('#home')[0], specialElementHandlers = {
'#bypassme': function(element, renderer){
return true
}
}
margins = {top: 80,bottom: 60,left: 40,width: 522};
pdf.fromHTML(source, margins.left, margins.top, {
'width': margins.width // max width of content on PDF
, 'elementHandlers': specialElementHandlers
},
function (dispose) {
pdf.save('home.pdf');
console.log( pdfOutput );

var pdfOutput = doc.output();
console.log("file system - ");
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {

console.log(fileSystem.name);
console.log(fileSystem.root.name);
console.log(fileSystem.root.fullPath);

fileSystem.root.getFile("test.pdf", {create: true}, function(entry) {
var fileEntry = entry;
console.log(entry);

entry.createWriter(function(writer) {
writer.onwrite = function(evt) {
alert("write success");
};

console.log("writing to file");
writer.write( pdfOutput );
}, function(error) {
console.log(error);
});

}, function(error){
console.log(error);
});
},
function(event){
console.log( evt.target.error.code );
});
},
margins
)

有人可以给我提示或指出正确的方向,告诉我如何将您的解决方案合并到我的编码中,以便我可以保存 html 格式和图像吗?

最佳答案

我刚刚遇到了同样的问题。这就是我为解决这个问题所做的事情。

更换线路

writer.write( pdfOutput );

这样:

var data = pdfOutput;
var buffer = new ArrayBuffer(data.length);
var array = new Uint8Array(buffer);
for (var i = 0; i < data.length; i++) {
array[i] = data.charCodeAt(i);
}
writer.write(buffer);

不能 100% 确定我理解发生了什么,但根据我一直在研究的内容,您需要在写入文件之前将输出从 jsPDF 转换为 ArrayBuffer。

关于javascript - Phonegap/jsPDF 将 html 保存为 pdf 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23368033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com