I am trying to replicate the official example for merging 2 pdf files, but instead of hardcode the name of the file, I'd like to have the user upload two files. The code works well when the filename is hardcoded (see url2) but doesn't work when trying to retrieve the filename from the input tag. What am I doing wrong?
async function copyPages() {
// Fetch first existing PDF document
const url1 = document.getElementById('file1').file[0].name
//const url1 = 'Patient_Card.pdf'
const doc1 = await fetch(url1).then(res => res.arrayBuffer())
// Fetch second existing PDF document
const url2 = 'Patient_Card.pdf'
const doc2 = await fetch(url2).then(res => res.arrayBuffer())
// Load a PDFDocument from each of the existing PDFs
const pdf1 = await PDFDocument.load(doc1)
const pdf2 = await PDFDocument.load(doc2)
// Create a new PDFDocument
const mergedPdf = await PDFDocument.create();
const copiedPagesA = await mergedPdf.copyPages(pdf1, pdf1.getPageIndices());
copiedPagesA.forEach((page) => mergedPdf.addPage(page));
const copiedPagesB = await mergedPdf.copyPages(pdf2, pdf2.getPageIndices());
copiedPagesB.forEach((page) => mergedPdf.addPage(page));
const mergedPdfFile = await mergedPdf.save();
// Trigger the browser to download the PDF document
download(mergedPdfFile, "pdf-lib_page_copying_example.pdf", "application/pdf");
}