GuidePDF Form Operations

Fill fields

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

Prepare the form data as a list of object as follows

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]

Mark 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(
36 [fs.readFileSync(__dirname + "/form.pdf")],
37 "form.pdf",
38 {
39 type: "application/pdf",
40 },
41 ),
42 formFillRequest,
43 requestOptions,
44 );
45
46
47 await pipelineAsync(filledPdfStream, fs.createWriteStream("./result_filled.pdf")); // ensures the whole file is written before the stream is closed
48
49 console.log("PDF form filling successful. Stream ready.");
50 } catch (error) {
51 console.error("Error during PDF form filling:", error);
52 }
53 })();

Get a modified PDF

The reponse is stream of the modified PDF with form fields marked with a green border, and hover text showing the field name.

sample marked pdf file