36 lines
1.2 KiB
JavaScript
36 lines
1.2 KiB
JavaScript
import dayjs from "dayjs";
|
|
import html2pdf from "html2pdf.js";
|
|
|
|
export default async function exportPdf(docid, { filename = "file", format = "a4", orientation = "portrait" } = {}) {
|
|
const target = document.getElementById(docid);
|
|
|
|
const ignored = target.querySelectorAll(".ignore");
|
|
ignored.forEach((el) => el.style.setProperty("display", "none", "important"));
|
|
// Xóa cache ảnh để lần export sau không bị lỗi
|
|
target.querySelectorAll("img").forEach((img) => (img.src = `${img.src}`));
|
|
const restoreIcons = swapIconsForExport(target);
|
|
|
|
const opt = {
|
|
margin: 3,
|
|
filename: `${filename}-${dayjs().format("YYYYMMDDHHmmss")}.pdf`,
|
|
jsPDF: { format, orientation, unit: "mm" },
|
|
html2canvas: { scale: 3, useCORS: true },
|
|
image: { type: "jpeg", quality: 1 },
|
|
pagebreak: {
|
|
mode: ["avoid-all", "css", "legacy"],
|
|
before: ".page-break-before",
|
|
after: ".page-break-after",
|
|
avoid: ".avoid-page-break",
|
|
},
|
|
};
|
|
|
|
try {
|
|
await new Promise((r) => requestAnimationFrame(r));
|
|
await html2pdf().set(opt).from(target).save();
|
|
} finally {
|
|
restoreIcons();
|
|
ignored.forEach((el) => el.style.removeProperty("display"));
|
|
}
|
|
return opt.filename;
|
|
}
|