changes
This commit is contained in:
28
app/utils/exportImage.js
Normal file
28
app/utils/exportImage.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import dayjs from "dayjs";
|
||||
import html2canvas from "html2canvas-pro";
|
||||
|
||||
export default async function exportImage(docid, filename = "file") {
|
||||
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);
|
||||
|
||||
try {
|
||||
await new Promise((r) => requestAnimationFrame(r));
|
||||
|
||||
const canvas = await html2canvas(target);
|
||||
const link = document.createElement("a");
|
||||
link.href = canvas.toDataURL("image/png");
|
||||
link.download = `${filename}-${dayjs().format("YYYYMMDDHHmmss")}.png`;
|
||||
link.click();
|
||||
link.remove();
|
||||
} finally {
|
||||
restoreIcons();
|
||||
ignored.forEach((el) => el.style.removeProperty("display"));
|
||||
}
|
||||
|
||||
return filename;
|
||||
}
|
||||
Reference in New Issue
Block a user