Annotate,Merge, and Print without WebViewer

WebViewer Version: 8

I’m trying to get multiple PDFs then while mapping though them and add a barcode annotation to each individual PDF. Finally merging them all together so it would print all documents selected without utilizing the web-viewer UI.

function printForm() {
            let selectedDocs = [];
            const form = document.querySelector('#print-forms-form');
            const checkboxes = form.querySelectorAll('.form-list__cell.form-list__checkbox-cell');
            for (let i = 0; i < checkboxes.length; i++) {
                if (checkboxes[i].checked) {
                const docId = parseInt(checkboxes[i].value);
                const docUrl = `/api/documents/${docId}/file`;
                selectedDocs.push(docUrl);
                console.log(`Adding document ${docId} to print queue`);
                }
            }

            // Wait for all documents to load
            Promise.all(selectedDocs.map(getDoc))
                .then(docs => {
                console.log('All documents loaded');
                const mergedPdf = mergePdfs(docs);
                printPdf(mergedPdf);
                })
                .catch(error => console.error(error));
            }

            async function getDoc(docUrl) {
            const response = await fetch(docUrl);
            if (response.ok) {
                return response.blob();
            } else {
                throw new Error(`Failed to fetch document from ${docUrl}`);
            }
            }

            async function mergePdfs(pdfs) {
                try {
                    const doc = await PDFNet.PDFDoc.create();
                    for (const pdf of pdfs) {
                    const pdfDoc = await PDFNet.PDFDoc.createFromBuffer(new Uint8Array(await pdf.arrayBuffer()));
                    await doc.insertPages(doc.getPageCount(), pdfDoc, 1, pdfDoc.getPageCount(), PDFNet.PDFDoc.InsertFlag.e_none);
                    }
                    const options = await PDFNet.SDFDoc.SaveOptions.create(PDFNet.SDFDoc.SaveOptions.e_linearized);
                    const pdfBlob = await doc.saveMemoryBuffer(options);
                    return new Blob([pdfBlob.buffer], { type: 'application/pdf' });
                } catch (error) {
                    console.error(error);
                }
                }

            function printPdf(pdfBlob) {
            const objectUrl = URL.createObjectURL(pdfBlob);
            const iframe = document.createElement('iframe');
            iframe.style.display = 'none';
            iframe.src = objectUrl;
            document.body.appendChild(iframe);
            iframe.contentWindow.print();
        }

Trying to utilize PDF net but unsure if this best course of action.

https://docs.apryse.com/documentation/web/guides/manipulation/merge/

Hi Simon,

From what we can see, we can confirm your method is the correct way to do it.

Cheers!
Will