mirror of
https://github.com/alam00000/bentopdf.git
synced 2026-11-13 17:54:19 +01:00
Updated the form-filler logic to bypass pdf-lib for XFA forms, utilizing PDF.js for rendering. Added UI elements for form-filler options and improved the process button functionality. Enhanced the download process by implementing checks for the download button in the viewer, providing better user feedback and error handling.
107 lines
3.1 KiB
TypeScript
107 lines
3.1 KiB
TypeScript
import { showLoader, hideLoader, showAlert } from '../ui.js';
|
|
import { readFileAsArrayBuffer } from '../utils/helpers.js';
|
|
import { state } from '../state.js';
|
|
|
|
let viewerIframe: HTMLIFrameElement | null = null;
|
|
let viewerReady = false;
|
|
|
|
|
|
export async function setupFormFiller() {
|
|
if (!state.files || !state.files[0]) return;
|
|
|
|
showLoader('Loading PDF form...');
|
|
const pdfViewerContainer = document.getElementById('pdf-viewer-container');
|
|
|
|
if (!pdfViewerContainer) {
|
|
console.error('PDF viewer container not found');
|
|
hideLoader();
|
|
return;
|
|
}
|
|
|
|
try {
|
|
pdfViewerContainer.innerHTML = '';
|
|
|
|
const file = state.files[0];
|
|
const arrayBuffer = await readFileAsArrayBuffer(file);
|
|
const blob = new Blob([arrayBuffer as ArrayBuffer], { type: 'application/pdf' });
|
|
const blobUrl = URL.createObjectURL(blob);
|
|
|
|
viewerIframe = document.createElement('iframe');
|
|
viewerIframe.src = `/pdfjs-viewer/viewer.html?file=${encodeURIComponent(blobUrl)}`;
|
|
viewerIframe.style.width = '100%';
|
|
viewerIframe.style.height = '100%';
|
|
viewerIframe.style.border = 'none';
|
|
|
|
viewerIframe.onload = () => {
|
|
viewerReady = true;
|
|
hideLoader();
|
|
};
|
|
|
|
pdfViewerContainer.appendChild(viewerIframe);
|
|
|
|
const formFillerOptions = document.getElementById('form-filler-options');
|
|
if (formFillerOptions) formFillerOptions.classList.remove('hidden');
|
|
} catch (e) {
|
|
console.error('Critical error setting up form filler:', e);
|
|
showAlert(
|
|
'Error',
|
|
'Failed to load PDF form viewer.'
|
|
);
|
|
hideLoader();
|
|
}
|
|
}
|
|
|
|
export async function processAndDownloadForm() {
|
|
if (!viewerIframe || !viewerReady) {
|
|
showAlert('Viewer not ready', 'Please wait for the form to finish loading.');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const viewerWindow = viewerIframe.contentWindow;
|
|
if (!viewerWindow) {
|
|
console.error('Cannot access iframe window');
|
|
showAlert(
|
|
'Download',
|
|
'Please use the Download button in the PDF viewer toolbar above.'
|
|
);
|
|
return;
|
|
}
|
|
|
|
const viewerDoc = viewerWindow.document;
|
|
if (!viewerDoc) {
|
|
console.error('Cannot access iframe document');
|
|
showAlert(
|
|
'Download',
|
|
'Please use the Download button in the PDF viewer toolbar above.'
|
|
);
|
|
return;
|
|
}
|
|
|
|
const downloadBtn = viewerDoc.getElementById('downloadButton') as HTMLButtonElement | null;
|
|
|
|
if (downloadBtn) {
|
|
console.log('Clicking download button...');
|
|
downloadBtn.click();
|
|
} else {
|
|
console.error('Download button not found in viewer');
|
|
const secondaryDownload = viewerDoc.getElementById('secondaryDownload') as HTMLButtonElement | null;
|
|
if (secondaryDownload) {
|
|
console.log('Clicking secondary download button...');
|
|
secondaryDownload.click();
|
|
} else {
|
|
showAlert(
|
|
'Download',
|
|
'Please use the Download button in the PDF viewer toolbar above.'
|
|
);
|
|
}
|
|
}
|
|
} catch (e) {
|
|
console.error('Failed to trigger download:', e);
|
|
showAlert(
|
|
'Download',
|
|
'Cannot access viewer controls. Please use the Download button in the PDF viewer toolbar above.'
|
|
);
|
|
}
|
|
}
|