This documentation provides an example of how to use the FileforgeClient to fill forms in a PDF document. This endpoint returns modified version of the PDF you submit with the filled form, according to the data you provide.

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

Prepare the form data as a list of object as follows

Note: if you don’t know what the field names are, you should use the mark form fields or detect form fields API endpoints to get the field names.

Here are the most common fields and supported types:

Field TypeOptions
PDFTextFieldvalue: string
PDFOptionListselected: string
PDFDropdownListselected: string
PDFCheckBoxchecked: boolean
PDFRadioGroupselected: string
1[
2 {
3 name: "Producer Name",
4 type: "PDFTextField",
5 value: "Titouan Launay", // fills a text field
6 },
7 {
8 name: "Check Box4",
9 type: "PDFCheckBox",
10 checked: false, // unchecks a box
11 },
12];
2

Fill form fields from the PDF and retrieve a modified PDF

1import { FileforgeClient } from "@fileforge/client";
2import * as fs from "fs";
3import { pipeline } from "stream";
4import { promisify } from "util";
5
6const pipelineAsync = promisify(pipeline);
7
8(async () => {
9 const ff = new FileforgeClient({
10 apiKey: process.env.FILEFORGE_API_KEY,
11 });
12
13 try {
14 const formFillRequest = {
15 options: {
16 fields: [
17 {
18 name: "Producer Name",
19 type: "PDFTextField",
20 value: "Pierre Dorge",
21 },
22 {
23 name: "Check Box4",
24 type: "PDFCheckBox",
25 checked: false,
26 },
27 ],
28 },
29 };
30 const requestOptions = {
31 timeoutInSeconds: 60,
32 maxRetries: 3,
33 };
34 const filledPdfStream = await ff.pdf.form.fill(
35 new File([fs.readFileSync(__dirname + "/form.pdf")], "form.pdf", {
36 type: "application/pdf",
37 }),
38 formFillRequest,
39 requestOptions
40 );
41
42 await pipelineAsync(
43 filledPdfStream,
44 fs.createWriteStream("./result_filled.pdf")
45 ); // ensures the whole file is written before the stream is closed
46
47 console.log("PDF form filling successful. Stream ready.");
48 } catch (error) {
49 console.error("Error during PDF form filling:", error);
50 }
51})();
3

Get a modified PDF

The reponse is stream of the filled PDF document.