我一直试图将页面打印为pdf,但是还没有能够,导致错误:对象不支持属性或方法' setattribute&# 39;
在线; html2canvas($(html)).find('#Content')[0],
如何修复错误?
window.onload = function() {
document.getElementById("cmd").onclick = function fun() {
alert("hello");
exportTwo();
}
}
function exportTwo() {
var canvasToImage = function(canvas){
var img = new Image();
var dataURL = canvas.toDataURL('image/png');
img.src = dataURL;
return img;
};
var canvasShiftImage = function(oldCanvas,shiftAmt){
shiftAmt = parseInt(shiftAmt) || 0;
if(!shiftAmt){ return oldCanvas; }
var newCanvas = document.createElement('canvas');
newCanvas.height = oldCanvas.height - shiftAmt;
newCanvas.width = oldCanvas.width;
var ctx = newCanvas.getContext('2d');
var img = canvasToImage(oldCanvas);
ctx.drawImage(img,0, shiftAmt, img.width, img.height, 0, 0, img.width, img.height);
return newCanvas;
};
var canvasToImageSuccess = function(canvas){
var pdf = new jsPDF('l','px'),
pdfInternals = pdf.internal,
pdfPageSize = pdfInternals.pageSize,
pdfScaleFactor = pdfInternals.scaleFactor,
pdfPageWidth = pdfPageSize.width,
pdfPageHeight = pdfPageSize.height,
totalPdfHeight = 0,
htmlPageHeight = canvas.height,
htmlScaleFactor = canvas.width / (pdfPageWidth * pdfScaleFactor),
safetyNet = 0;
while(totalPdfHeight < htmlPageHeight && safetyNet < 15){
var newCanvas = canvasShiftImage(canvas, totalPdfHeight);
pdf.addImage(newCanvas, 'png', 0, 0, pdfPageWidth, 0, null, 'NONE');
totalPdfHeight += (pdfPageHeight * pdfScaleFactor * htmlScaleFactor);
if(totalPdfHeight < htmlPageHeight){
pdf.addPage();
}
safetyNet++;
}
pdf.save('test.pdf');
};
$.ajax({
url:'https://apps.group.ec/sites/Insight/en-us/Pages/Dom/dm.aspx',
type:'GET',
success: function(data){
//$('#Content').html($(data).find('#content').html());
var html = $.parseHTML(data);
console.log($(html).find( '#Content').html());
html2canvas($(html)).find('#Content')[0], {
onrendered: function(canvas){
canvasToImageSuccess(canvas);
}
};
}
});
}
&#13;
谢谢。