如何使用 jspdf 和 html2canvas 打印全屏
How to print full screen using jspdf and html2canvas
您好,我在我的应用程序中使用 Angular8。在这里,我使用 jspdf 和 html2canvas 将 html 转换为 pdf。但我只能打印半页而不是整页。
任何人都可以帮助我哪里出错了。
我附上了一个演示,当我 select 下拉列表中的任何值时,都会打开另一个 div,所以我需要获取 div 中所有内容的完整值部分。请帮忙。
我得到这样的输出,但它不包含预期的完整值:
如果有任何其他方法可以输出我的要求也被接受。
TS:
public downloadPdf() {
var data = document.getElementById('pdfDownload');
html2canvas(data).then(canvas => {
// Few necessary setting options
var imgWidth = 208;
var imgHeight = canvas.height * imgWidth / canvas.width;
alert(imgHeight)
const contentDataURL = canvas.toDataURL('image/png')
let pdf = new jspdf('p', 'mm', 'a4'); // A4 size page of PDF
var position = 0;
pdf.addImage(contentDataURL, 'PNG', 0, position, imgWidth, imgHeight);
// pdf.save('new-file.pdf');
window.open(pdf.output('bloburl', { filename: 'new-file.pdf' }), '_blank');
});
}
为此,您必须在 html2canvas 方法 scrollY 中添加选项
{scrollY: -window.scrollY} 它将获取完全渲染页面的屏幕截图。
html2canvas(数据, {scrollY: -window.scrollY, 比例: 1}
除此之外,我们知道滚动条中有数据。为此,您必须暂时删除 scroll 并需要在 pdf 生成后添加。
你可以通过简单地添加 id 来做到这一点
<ul class="list-group list-group-flush vert-scrollable-150" id="alldata">
元素。
// 为禁用滚动编码
document.getElementById('alldata').style.overflow = 'inherit';
document.getElementById('alldata').style.maxHeight = 'inherit';
async downloadPdf() {
var data = document.getElementById('pdfDownload');
$('pdfOpenHide').attr('hidden', true);
// disable the scroll
document.getElementById('alldata').style.overflow = 'inherit';
document.getElementById('alldata').style.maxHeight = 'inherit';
await html2canvas(data, {scrollY: -window.scrollY,
scale: 1}).then(canvas => {
// Few necessary setting options
var imgWidth = 150;
var imgHeight = canvas.height * imgWidth / canvas.width;
const contentDataURL = canvas.toDataURL('image/png', 1.0)
// enabling the scroll
document.getElementById('alldata').style.overflow = 'scroll';
document.getElementById('alldata').style.maxHeight = '150px';
let pdf = new jspdf('l', 'mm','a4'); // A4 size page of PDF
var position = 0;
// add tghis width height according to your requirement
const divHeight = data.clientHeight
const divWidth = data.clientWidth
const ratio = divHeight / divWidth;
const width = pdf.internal.pageSize.getWidth();
let height = pdf.internal.pageSize.getHeight();
height = ratio * width;
pdf.addImage(contentDataURL, 'PNG', 0, position, width, height);
window.open(pdf.output('bloburl', { filename: 'new-file.pdf' }), '_blank');
});
}
如果滚动条中的数据较多,您也可以使用jspdf添加一个页面。
更多参考,你可以查看这个
HTML2Canvas does not render full div, only what is visible on screen?
另一个解决方案:如果数据更多
async downloadPdf() {
var data = document.getElementById("pdfDownload");
$("pdfOpenHide").attr("hidden", true);
// To disable the scroll
document.getElementById("alldata").style.overflow = "inherit";
document.getElementById("alldata").style.maxHeight = "inherit";
await html2canvas(data, { scrollY: -window.scrollY, scale: 1 }).then(
canvas => {
const contentDataURL = canvas.toDataURL("image/png", 1.0);
// enabling the scroll
document.getElementById("alldata").style.overflow = "scroll";
document.getElementById("alldata").style.maxHeight = "150px";
let pdf = new jspdf("l", "mm", "a4"); // A4 size page of PDF
let imgWidth = 300;
let pageHeight = pdf.internal.pageSize.height;
let imgHeight = (canvas.height * imgWidth) / canvas.width;
let heightLeft = imgHeight;
let position = 0;
pdf.addImage(contentDataURL, "PNG", 0, position, imgWidth, imgHeight);
heightLeft -= pageHeight;
while (heightLeft >= 0) {
position = heightLeft - imgHeight;
pdf.addPage();
pdf.addImage(contentDataURL, "PNG", 0, position, imgWidth, imgHeight);
heightLeft -= pageHeight;
}
window.open(
pdf.output("bloburl", { filename: "new-file.pdf" }),
"_blank"
);
}
);
}
不是从图像创建 pdf,而是使用 jspdf 的 addHtml 方法直接从 html 创建 pdf。这已解决您的问题。我创建了以下代码使用它。
您必须在索引页的页眉部分添加此脚本
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>
public downloadPdf() {
$("pdfOpenHide").attr("hidden", true);
var removeClass = document.getElementsByClassName("vert-scrollable-150");
removeClass[0].classList.add("RemoveThisClass");
removeClass[0].classList.remove("vert-scrollable-150");
var data = document.getElementById("pdfDownload");
const pdf = new jspdf("p", "pt", "a4");
pdf.addHTML(data, () => {
window.open(
pdf.output("bloburl", { filename: "new-file.pdf" }),
"_blank"
);
});
var addClass = document.getElementsByClassName("RemoveThisClass");
addClass[0].classList.add("vert-scrollable-150");
addClass[0].classList.remove("RemoveThisClass");
}
它会为你工作。你也可以参考
试试这个:
public downloadPdf() {
var data = document.getElementById('pdfDownload');
html2canvas(data, { useCORS:true}) // useCORS is optional if your images are externally hosted somewhere like s3
.then(canvas => {
const contentDataURL = canvas.toDataURL('image/png')
let pdf = new jspdf('p', 'pt', [canvas.width, canvas.height]);
var pdfWidth = pdf.internal.pageSize.getWidth();
var pdfHeight = pdf.internal.pageSize.getHeight();
pdf.addImage(contentDataURL, 'PNG', 0, pdfWidth, pdfHeight);
// pdf.save('new-file.pdf');
window.open(pdf.output('bloburl', { filename: 'new-file.pdf' }), '_blank');
});
}
您好,我在我的应用程序中使用 Angular8。在这里,我使用 jspdf 和 html2canvas 将 html 转换为 pdf。但我只能打印半页而不是整页。 任何人都可以帮助我哪里出错了。
我附上了一个演示,当我 select 下拉列表中的任何值时,都会打开另一个 div,所以我需要获取 div 中所有内容的完整值部分。请帮忙。 我得到这样的输出,但它不包含预期的完整值:
如果有任何其他方法可以输出我的要求也被接受。
TS:
public downloadPdf() {
var data = document.getElementById('pdfDownload');
html2canvas(data).then(canvas => {
// Few necessary setting options
var imgWidth = 208;
var imgHeight = canvas.height * imgWidth / canvas.width;
alert(imgHeight)
const contentDataURL = canvas.toDataURL('image/png')
let pdf = new jspdf('p', 'mm', 'a4'); // A4 size page of PDF
var position = 0;
pdf.addImage(contentDataURL, 'PNG', 0, position, imgWidth, imgHeight);
// pdf.save('new-file.pdf');
window.open(pdf.output('bloburl', { filename: 'new-file.pdf' }), '_blank');
});
}
为此,您必须在 html2canvas 方法 scrollY 中添加选项 {scrollY: -window.scrollY} 它将获取完全渲染页面的屏幕截图。
html2canvas(数据, {scrollY: -window.scrollY, 比例: 1}
除此之外,我们知道滚动条中有数据。为此,您必须暂时删除 scroll 并需要在 pdf 生成后添加。
你可以通过简单地添加 id 来做到这一点
<ul class="list-group list-group-flush vert-scrollable-150" id="alldata">
元素。
// 为禁用滚动编码
document.getElementById('alldata').style.overflow = 'inherit'; document.getElementById('alldata').style.maxHeight = 'inherit';
async downloadPdf() {
var data = document.getElementById('pdfDownload');
$('pdfOpenHide').attr('hidden', true);
// disable the scroll
document.getElementById('alldata').style.overflow = 'inherit';
document.getElementById('alldata').style.maxHeight = 'inherit';
await html2canvas(data, {scrollY: -window.scrollY,
scale: 1}).then(canvas => {
// Few necessary setting options
var imgWidth = 150;
var imgHeight = canvas.height * imgWidth / canvas.width;
const contentDataURL = canvas.toDataURL('image/png', 1.0)
// enabling the scroll
document.getElementById('alldata').style.overflow = 'scroll';
document.getElementById('alldata').style.maxHeight = '150px';
let pdf = new jspdf('l', 'mm','a4'); // A4 size page of PDF
var position = 0;
// add tghis width height according to your requirement
const divHeight = data.clientHeight
const divWidth = data.clientWidth
const ratio = divHeight / divWidth;
const width = pdf.internal.pageSize.getWidth();
let height = pdf.internal.pageSize.getHeight();
height = ratio * width;
pdf.addImage(contentDataURL, 'PNG', 0, position, width, height);
window.open(pdf.output('bloburl', { filename: 'new-file.pdf' }), '_blank');
});
}
如果滚动条中的数据较多,您也可以使用jspdf添加一个页面。
更多参考,你可以查看这个 HTML2Canvas does not render full div, only what is visible on screen?
另一个解决方案:如果数据更多
async downloadPdf() {
var data = document.getElementById("pdfDownload");
$("pdfOpenHide").attr("hidden", true);
// To disable the scroll
document.getElementById("alldata").style.overflow = "inherit";
document.getElementById("alldata").style.maxHeight = "inherit";
await html2canvas(data, { scrollY: -window.scrollY, scale: 1 }).then(
canvas => {
const contentDataURL = canvas.toDataURL("image/png", 1.0);
// enabling the scroll
document.getElementById("alldata").style.overflow = "scroll";
document.getElementById("alldata").style.maxHeight = "150px";
let pdf = new jspdf("l", "mm", "a4"); // A4 size page of PDF
let imgWidth = 300;
let pageHeight = pdf.internal.pageSize.height;
let imgHeight = (canvas.height * imgWidth) / canvas.width;
let heightLeft = imgHeight;
let position = 0;
pdf.addImage(contentDataURL, "PNG", 0, position, imgWidth, imgHeight);
heightLeft -= pageHeight;
while (heightLeft >= 0) {
position = heightLeft - imgHeight;
pdf.addPage();
pdf.addImage(contentDataURL, "PNG", 0, position, imgWidth, imgHeight);
heightLeft -= pageHeight;
}
window.open(
pdf.output("bloburl", { filename: "new-file.pdf" }),
"_blank"
);
}
);
}
不是从图像创建 pdf,而是使用 jspdf 的 addHtml 方法直接从 html 创建 pdf。这已解决您的问题。我创建了以下代码使用它。
您必须在索引页的页眉部分添加此脚本
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>
public downloadPdf() {
$("pdfOpenHide").attr("hidden", true);
var removeClass = document.getElementsByClassName("vert-scrollable-150");
removeClass[0].classList.add("RemoveThisClass");
removeClass[0].classList.remove("vert-scrollable-150");
var data = document.getElementById("pdfDownload");
const pdf = new jspdf("p", "pt", "a4");
pdf.addHTML(data, () => {
window.open(
pdf.output("bloburl", { filename: "new-file.pdf" }),
"_blank"
);
});
var addClass = document.getElementsByClassName("RemoveThisClass");
addClass[0].classList.add("vert-scrollable-150");
addClass[0].classList.remove("RemoveThisClass");
}
它会为你工作。你也可以参考
试试这个:
public downloadPdf() {
var data = document.getElementById('pdfDownload');
html2canvas(data, { useCORS:true}) // useCORS is optional if your images are externally hosted somewhere like s3
.then(canvas => {
const contentDataURL = canvas.toDataURL('image/png')
let pdf = new jspdf('p', 'pt', [canvas.width, canvas.height]);
var pdfWidth = pdf.internal.pageSize.getWidth();
var pdfHeight = pdf.internal.pageSize.getHeight();
pdf.addImage(contentDataURL, 'PNG', 0, pdfWidth, pdfHeight);
// pdf.save('new-file.pdf');
window.open(pdf.output('bloburl', { filename: 'new-file.pdf' }), '_blank');
});
}