Add PDF printing functionality to editor components
Enabled PDF printing in `SenderPdfImageEditor` and `SenderPdfImageEditorToolbar`. Added `CanPrintPdf` property to determine print availability and a new `OnPrintPdfAsync` method for handling print logic via JavaScript interop. Updated the toolbar with a "Print" button that triggers the print functionality. Implemented the `printDataUrlPdf` function in `envelope-editor.js` to handle printing from a data URL. This function creates an iframe, loads the PDF, and invokes the browser's print functionality with proper cleanup of resources.
This commit is contained in:
@@ -243,5 +243,82 @@ window.envelopeEditor = {
|
||||
host.appendChild(link);
|
||||
link.click();
|
||||
host.removeChild(link);
|
||||
},
|
||||
|
||||
printDataUrlPdf: async function (dataUrl, mimeType) {
|
||||
if (!dataUrl || typeof dataUrl !== 'string') {
|
||||
return;
|
||||
}
|
||||
|
||||
let objectUrl = null;
|
||||
let printSrc = dataUrl;
|
||||
|
||||
if (dataUrl.startsWith('data:')) {
|
||||
try {
|
||||
const response = await fetch(dataUrl);
|
||||
const blob = await response.blob();
|
||||
objectUrl = URL.createObjectURL(blob);
|
||||
printSrc = objectUrl;
|
||||
} catch {
|
||||
printSrc = dataUrl;
|
||||
}
|
||||
}
|
||||
|
||||
const frame = document.createElement('iframe');
|
||||
frame.style.position = 'fixed';
|
||||
frame.style.right = '0';
|
||||
frame.style.bottom = '0';
|
||||
frame.style.width = '0';
|
||||
frame.style.height = '0';
|
||||
frame.style.border = '0';
|
||||
frame.setAttribute('aria-hidden', 'true');
|
||||
|
||||
const host = document.body || document.documentElement;
|
||||
host.appendChild(frame);
|
||||
|
||||
let cleaned = false;
|
||||
const cleanup = function () {
|
||||
if (cleaned) {
|
||||
return;
|
||||
}
|
||||
|
||||
cleaned = true;
|
||||
|
||||
if (frame.parentNode) {
|
||||
frame.parentNode.removeChild(frame);
|
||||
}
|
||||
|
||||
if (objectUrl) {
|
||||
URL.revokeObjectURL(objectUrl);
|
||||
objectUrl = null;
|
||||
}
|
||||
};
|
||||
|
||||
frame.onload = function () {
|
||||
try {
|
||||
const frameWindow = frame.contentWindow;
|
||||
if (!frameWindow) {
|
||||
cleanup();
|
||||
return;
|
||||
}
|
||||
|
||||
frameWindow.addEventListener('afterprint', function () {
|
||||
window.setTimeout(cleanup, 250);
|
||||
}, { once: true });
|
||||
|
||||
window.addEventListener('focus', function () {
|
||||
window.setTimeout(cleanup, 1500);
|
||||
}, { once: true });
|
||||
|
||||
frameWindow.focus();
|
||||
frameWindow.print();
|
||||
} catch {
|
||||
cleanup();
|
||||
}
|
||||
};
|
||||
|
||||
frame.src = printSrc;
|
||||
|
||||
window.setTimeout(cleanup, 120000);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user