web: get rid of print-js lib for printing

This commit is contained in:
Abdullah Atta
2023-07-24 15:45:14 +05:00
committed by Abdullah Atta
parent 36bf7c5cc6
commit 69dee3be78
3 changed files with 1052 additions and 37 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -51,7 +51,6 @@
"pdfjs-dist": "3.6.172",
"phone": "^3.1.14",
"platform": "^1.3.6",
"print-js": "^1.6.0",
"qclone": "^1.2.0",
"react-dropzone": "^11.4.2",
"react-hot-toast": "^2.2.0",

View File

@@ -30,20 +30,31 @@ export async function exportToPDF(
content: string
): Promise<boolean> {
if (!content) return false;
const { default: printjs } = await import("print-js");
return new Promise((resolve) => {
printjs({
printable: content.replaceAll(/<p [a-z\-="]*><\/p>/gm, "<p>&nbsp;</p>"),
targetStyles: "*",
type: "raw-html",
documentTitle: title,
header: '<h3 class="custom-h3">My custom header</h3>',
onPrintDialogClose: () => {
resolve(false);
},
onError: () => resolve(false)
});
resolve(true);
return new Promise<boolean>((resolve) => {
const iframe = document.createElement("iframe");
iframe.srcdoc = content.replaceAll(/<p(.+?)><\/p>/gm, "<p$1><br/></p>");
iframe.style.position = "fixed";
iframe.style.right = "0";
iframe.style.bottom = "0";
iframe.style.width = "0";
iframe.style.height = "0";
iframe.style.border = "0";
iframe.onload = () => {
if (!iframe.contentWindow) return;
if (iframe.contentDocument) iframe.contentDocument.title = title;
iframe.contentWindow.onbeforeunload = () => closePrint(false);
iframe.contentWindow.onafterprint = () => closePrint(true);
iframe.contentWindow.print();
};
function closePrint(result: boolean) {
document.body.removeChild(iframe);
resolve(result);
}
document.body.appendChild(iframe);
});
}