This commit is contained in:
Viet An
2026-06-25 17:25:42 +07:00
parent 31ac60a282
commit f759ca49d5
13 changed files with 669 additions and 1458 deletions

35
app/utils/exportPdf.js Normal file
View File

@@ -0,0 +1,35 @@
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;
}