This documentation provides an example of how to use the FileforgeClient to generate combine multiple PDFs into one.

Prerequisites

Ensure you have the following:

  • An API key for Fileforge as an environment variable: process.env.FILEFORGE_API_KEY
  • Node.js and npm installed.
  • The Fileforge Client installed.

Guide

1

Make sure to set a timeout for the request

If you are merging many / very large files, you may need to increase the timeout as shown below with the timeoutInSeconds parameter.

2

Merge the PDFs

1import { FileforgeClient } from "@fileforge/client";
2import * as fs from "fs";
3
4(async () => {
5 const ff = new FileforgeClient({
6 apiKey: process.env.FILEFORGE_API_KEY,
7 });
8
9 try {
10 const pdfFiles = [
11 fs.createReadStream(__dirname + "/pdf1.pdf"),
12 fs.createReadStream(__dirname + "/pdf2.pdf"),
13 ];
14 const mergedPdfStream = await ff.pdf.merge(
15 pdfFiles,
16 {
17 options: {
18 // Specify merge options if any
19 },
20 },
21 {
22 timeoutInSeconds: 60,
23 },
24 );
25 mergedPdfStream.pipe(fs.createWriteStream("./result_merge.pdf"));
26 console.log("PDF merge successful. Stream ready.");
27 } catch (error) {
28 console.error("Error during PDF merge:", error);
29 throw error;
30 }
31 })();