I’m using PDF.js
Adding references straightforward
<link rel="resource" type="application/l10n" href="/locale/locale.json"> <script src="build/pdf.js" type="module"></script> <link rel="stylesheet" href="/viewer.css">
I wanted to remove a specific page using another plugin name pdf-lib
I do the following
1- Read PDF File as Base64 from PDF Viewer
async function ReadPDFFromViewer() {
var pdfFileBytes = await PDFViewerApplication.pdfDocument.saveDocument();
var b64encoded = _arrayBufferToBase64(pdfFileBytes);
return b64encoded;
}
function _arrayBufferToBase64(buffer) {
var binary = '';
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
}
2- Load and remove targeted page using pdf-lib
var currentFilePDFBase64 = await ReadPDFFromViewer();
let pdfDoc = await PDFLib.PDFDocument.load(currentFilePDFBase64);
pdfDoc.removePage(currentPage)
var pdfBytes = await pdfDoc.save();
3- Reopen the edited file onto PDF Viewer
PDFViewerApplication.open({ data: pdfBytes });
PDFViewerApplication.pdfViewer.refresh(false, "");
Until this code it works correctly
The problem
After reopen the edited PDF, The action Open on editor no longer being loaded new selected file
When I click Open and select new file after that no file appears on Viewer
enter image description here
I Read the events inside viewer.js and reach this lines of codes file
var fileInput = this._openFileInput = document.createElement("input");
fileInput.id = "fileInput";
fileInput.hidden = true;
fileInput.type = "file";
fileInput.value = null;
document.body.append(fileInput);
fileInput.addEventListener("change", function (evt) {
debugger;
const {
files
} = evt.target;
if (!files || files.length === 0) {
return;
}
eventBus.dispatch("fileinputchange", {
source: this,
fileInput: evt.target
});
});
I notice after doing previous actions the listener stop triggered
fileInput.addEventListener(“change”, function (evt)
I tried to add event listener again but the PDF Viewer still no file being loaded
Developer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.