你可以尝试响应式方式,例如:
if (screen.width < 1024) {
document
.querySelector("meta[name=viewport]")
.setAttribute("content", "width=1200px");
}
let generatedPDFB64;
const printElement = document.querySelector(`div[print-id=${selector}]`);
const convertedCanvas = await html2canvas(printElement, {
allowTaint: true,
removeContainer: true,
backgroundColor: null,
imageTimeout: 15000,
logging: true,
scale: 2,
scrollY: window.scrollTo({
top: 0,
behavior: 'smooth'
}),
useCORS: true
});
const contentDataURL = convertedCanvas.toDataURL("image/png");
const imgWidth = 205;
const pageHeight = 300;
const imgHeight = (convertedCanvas.height * imgWidth) / convertedCanvas.width;
let heightLeft = imgHeight;
const shouldCompress = true
let pdf = new jsPDF("p", "mm", "a4", shouldCompress);
let position = 0;
pdf.addImage(
contentDataURL,
"PNG",
0,
position,
imgWidth,
imgHeight,
"print",
"FAST"
);
heightLeft -= pageHeight;
while (heightLeft >= 0) {
position = heightLeft - imgHeight;
pdf.addPage();
pdf.addImage(
convertedCanvas,
0,
position,
imgWidth,
imgHeight,
"print",
"FAST"
);
heightLeft -= pageHeight;
}
pdf.save("resume.pdf");
if (screen.width < 1024) {
document
.querySelector("meta[name=viewport]")
.setAttribute("content", "width=device-width, initial-scale=1");
}