【发布时间】:2016-04-27 17:36:28
【问题描述】:
我们正在使用 jsPDF 将 HTML 生成为 PDF。但是,在向我的 PDF 添加 40 个元素并将其渲染到画布时,我的浏览器崩溃了。
我们正在使用以下函数将元素作为图像添加到 pdf。
function addElement(element, doc, opt, newPage, callback) {
var thiscreen = element;
//Get the original background color.
var originalBGColor = thiscreen.style.backgroundColor;
//Change the background color of the element to desired color.
if (opt.bgColor)
thiscreen.style.backgroundColor = opt.bgColor;
var options = options || {};
options.elements = [thiscreen];
//Increment the in-progress counter.
counter++;
console.log('adding' + counter);
console.log(element);
//The complete callback method.
options.complete = setTimeout(function(images) {
//Decrement the in-progress counter since the image is successfully generated..
counter--;
console.log('complete' + counter);
console.log(element);
var queue = html2canvas.Parse(thiscreen, images, options),
canvas = html2canvas.Renderer(queue, options);
//Reset the background color.
thiscreen.style.backgroundColor = originalBGColor;
//Add the generated image to PDF document.
doc.addImage(canvas.toDataURL(), 'png', opt.x, opt.y, opt.width, opt.height);
//Call the callback method if any
if (callback) {
callback();
}
}, 500);
//Conver the html to PNG using html2canvas util.
html2canvas.Preload(thiscreen, options);
}
我们也尝试过使用以下解决方案。
var blob = doc.output("blob");
window.open(URL.createObjectURL(blob));
或者有什么方法可以将低分辨率图像添加到 pdf 中。
【问题讨论】:
标签: jquery image pdf jspdf html2canvas