DEV Community

Visakh Vijayan
Visakh Vijayan

Posted on

Merging PDFs in NodeJs

public static async mergeProposalPDFs(frontURL: string, backURL: string): Promise<Buffer> {
    const pdfDoc = await PDFDocument.create();

    const pdf1Data = await fetch(frontURL).then((res) => res.arrayBuffer());
    const pdf1 = await PDFDocument.load(pdf1Data);
    const copiedPages1 = await pdfDoc.copyPages(pdf1, pdf1.getPageIndices());
    copiedPages1.forEach((page) => pdfDoc.addPage(page));

    const pdf2Data = await fetch(backURL).then((res) => res.arrayBuffer());
    const pdf2 = await PDFDocument.load(pdf2Data);
    const copiedPages2 = await pdfDoc.copyPages(pdf2, pdf2.getPageIndices());
    copiedPages2.forEach((page) => pdfDoc.addPage(page));

    // Save the merged PDF to a new buffer
    const mergedPdfFile = await pdfDoc.save();

    return Buffer.from(mergedPdfFile);
  }
Enter fullscreen mode Exit fullscreen mode

The function takes in two PDF urls and returns a buffer. In our case we wanted it to be uploaded further to s3.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more