Ionic Cordova Angular HTML2canvas 和 pdf 在手机上以 pdf 文件的形式下载屏幕截



我已经坚持了3 天了。

在安卓和IOS手机上。当我单击按钮时,我需要对页面的离子内容进行场景拍摄并将其下载到手机上的功能。

我找到了如何使用html2canvas做pdf:

html2canvas(document.getElementById("exportthis"), {
onrendered: function (canvas) {
var contentWidth = canvas.width;
var contentHeight = canvas.height;
//The height of the canvas which one pdf page can show;
var pageHeight = contentWidth / 592.28 * 841.89;
//the height of canvas that haven't render to pdf
var leftHeight = contentHeight;
//addImage y-axial offset
var position = 0;
//a4 format [595.28,841.89]       
var imgWidth = 595.28;
var imgHeight = 592.28 / contentWidth * contentHeight;
var pageData = canvas.toDataURL('image/jpeg', 1.0);
console.log(pageData);
var pdf = new jsPDF('', 'pt', 'a4');
console.log(pdf);
if (leftHeight < pageHeight) {
pdf.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight);
} else {
while (leftHeight > 0) {
pdf.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)
leftHeight -= pageHeight;
position -= 841.89;
//avoid blank page
if (leftHeight > 0) {
pdf.addPage();
console.log('>0 : ', pdf);
}
}
}
pdf.save('aircraft.pdf');
}

})

以及如何使用pdfMake(pdfMake链接(在手机上下载pdf。

但是我找不到如何将其放在一起以在设备中工作。

这是我现在放在一起时所拥有的,但它不能像 this.pdfObj 为空一样工作:

createPdf() {
html2canvas(document.getElementById("exportthis"), {
onrendered: function (canvas) {
var contentWidth = canvas.width;
var contentHeight = canvas.height;
//The height of the canvas which one pdf page can show;
var pageHeight = contentWidth / 592.28 * 841.89;
//the height of canvas that haven't render to pdf
var leftHeight = contentHeight;
//addImage y-axial offset
var position = 0;
//a4 format [595.28,841.89]       
var imgWidth = 595.28;
var imgHeight = 592.28 / contentWidth * contentHeight;
var data = canvas.toDataURL('image/jpeg', 1.0);
var docDefinition = {
content: [{
image: data,
width: imgWidth,
height: imgHeight,
position: position
}]
};
this.pdfObj = pdfMake.createPdf(docDefinition);
console.log("obj : ", this.pdfObj);
if (this.pdfObj) {
console.log("it works");
if (this.plt.is('cordova')) {
this.pdfObj.getBuffer((buffer) => {
var blob = new Blob([buffer], { type: 'application/pdf' });
// Save the PDF to the data Directory of our App
this.file.writeFile(this.file.dataDirectory, 'aircraft.pdf', blob, { replace: true }).then(fileEntry => {
// Open the PDf with the correct OS tools
this.fileOpener.open(this.file.dataDirectory + 'aircraft.pdf', 'application/pdf');
})
});
} else {
// On a browser simply use download!
this.pdfObj.download('aircraft.pdf');
}
}
else{
console.log("fail");
}
}
});

}

有人可以帮我吗?

我在帮助下发现了这个问题。 对于感兴趣的人,有解决方案:

createPdf() {
html2canvas(document.getElementById("exportthis"), {
onrendered: function (canvas) {
var contentWidth = canvas.width;
var contentHeight = canvas.height;
//The height of the canvas which one pdf page can show;
var pageHeight = contentWidth / 592.28 * 841.89;
//the height of canvas that haven't render to pdf
var leftHeight = contentHeight;
//addImage y-axial offset
var position = 0;
//a4 format [595.28,841.89]       
var imgWidth = 595.28;
var imgHeight = 592.28 / contentWidth * contentHeight;
var data = canvas.toDataURL('image/jpeg', 1.0);
var docDefinition = {
content: [{
image: data,
width: imgWidth,
height: imgHeight,
position: position
}]
};
this.pdfObj = pdfMake.createPdf(docDefinition);
console.log("obj : ", this.pdfObj);
if (this.pdfObj) {
console.log("it works");
if (this.plt.is('cordova')) {
this.pdfObj.getBuffer((buffer) => {
var blob = new Blob([buffer], { type: 'application/pdf' });
// Save the PDF to the data Directory of our App
this.file.writeFile(this.file.dataDirectory, 'aircraft.pdf', blob, { replace: true }).then(fileEntry => {
// Open the PDf with the correct OS tools
this.fileOpener.open(this.file.dataDirectory + 'aircraft.pdf', 'application/pdf');
})
});
} else {
// On a browser simply use download!
this.pdfObj.download('aircraft.pdf');
}
}
else {
console.log("fail");
}
}.bind(this) <-------- Solution
});

}

最新更新