'Extract individual PDF from a portfolio of multiple PDF documents
I have a web application built with PHP and JS. In this app I make different operations with certain files and i encounter the next problem:
The user needs to upload in my app a PDF file which is a portfolio/package of multiple PDF documents (AcroForm). So the problem is that I don't know how to individually extract each PDF document from the portfolio (I want to read each PDF file separate, otherwise i can't). This is where I get stuck and I don't know how de-structure this collection of PDF's. If you have any idea how to do this please let me know.
Here is the code that I write to extract the data from a single PDF, it is working for a single PDF file, that's why I need to de-structure the collection of PDF's (I use pdflib to extract the data from a single PDF doc)
// get the values from the uploaded PDF form (AcroForm) when the form is submited and send them to the server
document.getElementById('form')?.addEventListener('submit', async e => {
e.preventDefault();
const { PDFDocument, PDFTextField, PDFDropdown, PDFRadioGroup, PDFCheckBox, PDFOptionList } = PDFLib;
const file = document.getElementById('file').files[0];
const data = await readData(file);
const PDFDoc = await PDFDocument.load(data);
const form = PDFDoc.getForm();
const fields = form.getFields();
// get the values from the fields and send them to the server...
});
// get the file data
function readData(file) {
return new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.onload = e => resolve(e.target.result);
fileReader.onerror = reject;
fileReader.readAsArrayBuffer(file);
})
}
Example with the PDF portfolio/collection (in the left side are the PDF documents that I need to extract them separately in my app): PDF portfolio example
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|