Skip to content

Commit

Permalink
Fixes fill form guide
Browse files Browse the repository at this point in the history
  • Loading branch information
Titou325 committed Aug 22, 2024
1 parent 9e940bb commit ef14738
Showing 1 changed file with 69 additions and 62 deletions.
131 changes: 69 additions & 62 deletions fern/pages/help_center/pdf-forms/fill.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,84 +15,91 @@ Ensure you have the following:

### 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](/help_center/pdf-forms/mark) or [detect form fields](/help_center/pdf-forms/fill) API endpoints to get the field names.

Here are the most common fields and supported types:

| Field Type | Options |
| --------------- | ------------------ |
| PDFTextField | `value: string` |
| PDFOptionList | `selected: string` |
| PDFDropdownList | `selected: string` |
| PDFCheckBox | `checked: boolean` |
| PDFRadioGroup | `selected: string` |

```typescript
[
{
name: "Producer Name",
type: "PDFTextField",
value: "Titouan Launay", // fills a text field
},
{
name: "Check Box4",
type: "PDFCheckBox",
checked: false, // unchecks a box
},
]
{
name: "Producer Name",
type: "PDFTextField",
value: "Titouan Launay", // fills a text field
},
{
name: "Check Box4",
type: "PDFCheckBox",
checked: false, // unchecks a box
},
];
```

### Mark form fields from the PDF and retrieve a modified PDF
### Fill form fields from the PDF and retrieve a modified PDF

```typescript
import { FileforgeClient } from "@fileforge/client";
import * as fs from "fs";
import { pipeline } from 'stream';
import { promisify } from 'util';
import { pipeline } from "stream";
import { promisify } from "util";

const pipelineAsync = promisify(pipeline);

(async () => {
const ff = new FileforgeClient({
apiKey: process.env.FILEFORGE_API_KEY,
});

try {
const formFillRequest = {
options: {
fields: [
{
name: "Producer Name",
type: "PDFTextField",
value: "Pierre Dorge"
},
{
name:"Check Box4",
type:"PDFCheckBox",
checked: false
},
],
},
};
const requestOptions = {
timeoutInSeconds: 60,
maxRetries: 3,
};
const filledPdfStream = await ff.pdf.form.fill(
new File(
[fs.readFileSync(__dirname + "/form.pdf")],
"form.pdf",
const ff = new FileforgeClient({
apiKey: process.env.FILEFORGE_API_KEY,
});

try {
const formFillRequest = {
options: {
fields: [
{
type: "application/pdf",
name: "Producer Name",
type: "PDFTextField",
value: "Pierre Dorge",
},
),
formFillRequest,
requestOptions,
);


await pipelineAsync(filledPdfStream, fs.createWriteStream("./result_filled.pdf")); // ensures the whole file is written before the stream is closed

console.log("PDF form filling successful. Stream ready.");
} catch (error) {
console.error("Error during PDF form filling:", error);
}
})();
{
name: "Check Box4",
type: "PDFCheckBox",
checked: false,
},
],
},
};
const requestOptions = {
timeoutInSeconds: 60,
maxRetries: 3,
};
const filledPdfStream = await ff.pdf.form.fill(
new File([fs.readFileSync(__dirname + "/form.pdf")], "form.pdf", {
type: "application/pdf",
}),
formFillRequest,
requestOptions
);

await pipelineAsync(
filledPdfStream,
fs.createWriteStream("./result_filled.pdf")
); // ensures the whole file is written before the stream is closed

console.log("PDF form filling successful. Stream ready.");
} catch (error) {
console.error("Error during PDF form filling:", error);
}
})();
```

### 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](../../../assets/form-pdf-marked.png)


The reponse is stream of the filled PDF document.

</Steps>
</Steps>

0 comments on commit ef14738

Please sign in to comment.