Fill existing PDF templates with TypeScript. Set up a PDF template, send data to Anvil, and get back a completed PDF — no manual entry required.
import fs from 'fs'
import Anvil from '@anvilco/anvil'
// Your API key from your Anvil organization settings
const apiKey = process.env.ANVIL_API_KEY ?? ''
// A sample PDF template available to any account. See
// https://www.useanvil.com/help/tutorials/set-up-a-pdf-template
// to set up your own
const pdfTemplateID = '05xXsZko33JIO6aq5Pnr'
async function fillPDF () {
const anvilClient = new Anvil({ apiKey })
const payload = {
title: 'My PDF Title',
fontSize: 10,
textColor: '#333333',
// IDs here match the fields configured on the PDF template
data: {
shortText: 'Hello World!',
date: '2024-01-15',
name: { firstName: 'Robin', mi: 'W', lastName: 'Smith' },
email: 'testy@example.com',
phone: { num: '5554443333', region: 'US', baseRegion: 'US' },
usAddress: {
street1: '123 Main St #234',
city: 'San Francisco',
state: 'CA',
zip: '94106',
country: 'US',
},
ssn: '456454567',
ein: '897654321',
checkbox: true,
decimalNumber: 12345.67,
dollar: 123.45,
integer: 12345,
percent: 50.3,
longText: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
},
}
const { statusCode, data, errors } =
await anvilClient.fillPDF(pdfTemplateID, payload)
if (statusCode === 200 && data) {
// `data` is the filled PDF binary; save it with no encoding or the
// file will be corrupt
fs.writeFileSync('fill-output.pdf', data, { encoding: null })
console.log('Filled PDF saved to fill-output.pdf')
} else {
console.log(
'Error filling PDF:', statusCode, JSON.stringify(errors, null, 2)
)
}
}
fillPDF().catch((error) => {
console.error(error)
process.exit(1)
})
The @anvilco/anvil package is MIT-licensed and maintained in the open on GitHub.
$ npm install @anvilco/anvil
$ yarn add @anvilco/anvil
$ pnpm add @anvilco/anvil
$ bun add @anvilco/anvilInstall the Anvil Document SDK plugin in Claude Code and Claude will scaffold the SDK into your codebase — discovery, planning, and production-ready code.
/plugin marketplace add anvilco/anvil-plugins
/plugin install anvil-document-sdk@anvil-pluginsNo API key setup needed. The plugin connects to your Anvil account with OAuth and logs you in automatically.
Instantly fill your PDF templates with JSON data. Try an example below and see documentation when ready to fill your own.