0

I'm trying to combine multiple pdfs to make one file. I have found a script online that does this but I don't want to manually click to upload the files, I simply want to just merge them from their existing location and download.

Here's the example I have: jsfiddle

Here are the PDF placeholders I wish to use:

placeholder 1

and

placeholder 2

This is the part that is merging the PDFs from what I can tell but how do I point this towards my files as opposed to using the current method?

async function joinPdf() {
    const mergedPdf = await PDFDocument.create();
    for (let document of window.arrayOfPdf) {
        document = await PDFDocument.load(document.bytes);
        const copiedPages = await mergedPdf.copyPages(document, document.getPageIndices());
        copiedPages.forEach((page) => mergedPdf.addPage(page));
    }
    var pdfBytes = await mergedPdf.save();
    download(pdfBytes, "pdfconbined" + new Date().getTime() + ".pdf", "application/pdf");
}

input.addEventListener('change', openfile, false);

function mostrarDados() {

}
Juan-man
  • 436
  • 4
  • 13
  • What do you mean by "existing location"? The files are on the client, the server, or at some other location? – Yogi Oct 03 '22 at 13:53
  • The files will be located on the server but I'm using placeholders for the moment. – Juan-man Oct 03 '22 at 13:58
  • You could do this using createObjectUrl to download the preselected PDF files from your server. See this [question](https://stackoverflow.com/a/52829183/943435) for an example. And you will need to rework your openfile() function to use the downloaded files rather than the input element. It doesn't seem too difficult to implement. However, this kind of task is usually done on the server rather than with JavaScript on the client side. So you might want to look at that option too. – Yogi Oct 03 '22 at 19:03

0 Answers0