抱歉PDF生成问题被淹没了。我一直致力于一个项目,使用分页符将页面转换为多页pdf(使用jsPDF)。我(终于!)让这个项目在Google Chrome中运行,但是在Firefox上进行测试后,我能够生成第一页,但后两页显示为全黑。这是代码:
$(document).ready(function()
{
$("#runpdf").click(function(event)
{
var partsec = $("main_body page1");
html2canvas(document.body,
{
logging: true,
profile: true,
allowTaint: true,
letterRendering: true,
onrendered: function(canvas)
{
var sectionHeight = $("section").height();
var sectionWidth = $("#width").width();
var doc = new jsPDF();
var image = new Image();
var imageData = canvas.toDataURL("image/jpeg");
image = Canvas2Image.convertToJPEG(canvas);
doc.addImage(imageData,'JPEG', -115, 5, 440, 875);
doc.addPage();
var canvas1 = document.createElement('canvas');
canvas1.setAttribute('height', sectionHeight);
canvas1.setAttribute('width', sectionWidth);
var ctx = canvas1.getContext("2d");
ctx.drawImage(image, 0, 1025, sectionWidth, 1250, 0, 0, 1800, 950);
var image2 = new Image();
image2 = Canvas2Image.convertToJPEG(canvas1);
image2Data = image2.src;
doc.addImage(image2Data, 'JPEG', -105, 5, 440, 325);
doc.addPage();
var canvas2 = document.createElement('canvas');
canvas2.setAttribute('height', sectionHeight);
canvas2.setAttribute('width', sectionWidth);
var ctx1 = canvas2.getContext("2d");
ctx1.drawImage(image, 0, 2050, sectionWidth, 1250, 0, 0, 1800, 1000);
var image3 = new Image();
image3 = Canvas2Image.convertToJPEG(canvas2);
image2Data = image3.src;
doc.addImage(image2Data, 'JPEG', -105, 5, 440, 325);
doc.save('test.pdf');
}
});
});
});
正如您所看到的,每个页面都是单独生成的,因为第一页正在运行,而不是后两页,我假设问题出在getContext或drawImage函数上。我怎么能改变它或添加一些能使它在Firefox中正常工作的东西。 再次感谢。
答案 0 :(得分:1)
您可以尝试将blob保存到pdf中,而不是直接使用doc.save,这里是修改后的代码
$(document).ready(function()
{
$("#runpdf").click(function(event)
{
var partsec = $("main_body page1");
html2canvas(document.body,
{
logging: true,
profile: true,
allowTaint: true,
letterRendering: true,
onrendered: function(canvas)
{
var sectionHeight = $("section").height();
var sectionWidth = $("#width").width();
var doc = new jsPDF();
var image = new Image();
var imageData = canvas.toDataURL("image/jpeg");
image = Canvas2Image.convertToJPEG(canvas);
doc.addImage(imageData,'JPEG', -115, 5, 440, 875);
doc.addPage();
var canvas1 = document.createElement('canvas');
canvas1.setAttribute('height', sectionHeight);
canvas1.setAttribute('width', sectionWidth);
var ctx = canvas1.getContext("2d");
ctx.drawImage(image, 0, 1025, sectionWidth, 1250, 0, 0, 1800, 950);
var image2 = new Image();
image2 = Canvas2Image.convertToJPEG(canvas1);
image2Data = image2.src;
doc.addImage(image2Data, 'JPEG', -105, 5, 440, 325);
doc.addPage();
var canvas2 = document.createElement('canvas');
canvas2.setAttribute('height', sectionHeight);
canvas2.setAttribute('width', sectionWidth);
var ctx1 = canvas2.getContext("2d");
ctx1.drawImage(image, 0, 2050, sectionWidth, 1250, 0, 0, 1800, 1000);
var image3 = new Image();
image3 = Canvas2Image.convertToJPEG(canvas2);
image2Data = image3.src;
doc.addImage(image2Data, 'JPEG', -105, 5, 440, 325);
var data = doc.output();
var buffer = new ArrayBuffer(data.length);
var array = new Uint8Array(buffer);
for (var i = 0; i < data.length; i++) {
array[i] = data.charCodeAt(i);
}
var blob = new Blob(
[array],
{type: 'application/pdf', encoding: 'raw'}
);
saveAs(blob, "test.pdf");
});
});
});
除了jspdf之外,这里使用的javascript是canvas-toBlob.js和FileSaver.js