我需要捕获网页的图像并将其另存为图像/ PDF。我正在尝试实现canvas2pdf canvas2pdf。我使用html2canvas在JavaScript中获取画布,然后尝试生成PDF。有谁知道如何在canvas2pdf中将画布分配/添加到上下文。还有其他我可以使用的脚本库(尝试过jsPDF jsPDF,但会引发错误-Blob不是构造函数)。我的代码如下:
html2canvas(document.getElementById(obj), {
logging:true,
proxy:'app/qBlob/proxy.ashx',
useCORS: true,
onrendered: function(canvas) {
var ctx = new canvas2pdf.PDFContext(blobStream());
//draw your canvas like you would normally
// how to assign canvas to ctx??
//convert your PDF to a Blob and save to file
ctx.stream.on('finish', function () {
var blob = ctx.stream.toBlob('application/pdf');
saveAs(blob, 'example.pdf', true);
});
ctx.end();
// jsPDF code
// var imgData = canvas.toDataURL('image/png');
// console.log(imgData);
// var pdf = new jsPDF();
// var marginLeft=20;
// var marginRight=20;
// pdf.addHTML(document.body);
// pdf.save('example.pdf');
}
});
答案 0 :(得分:0)
Blob is not a constructor
与以下行一起抛出:
var ctx = new canvas2pdf.PDFContext(blobStream());
更改为该错误以避免发生错误:
var stream = blobStream();
var ctx = new canvas2pdf.PdfContext(stream);
该代码摘自canvas2pdf演示页面的源代码: