-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfill-pdf.ts
98 lines (89 loc) · 3.16 KB
/
fill-pdf.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Example: Fill a PDF via the Anvil API
//
// * PDF filling API docs: https://www.useanvil.com/docs/api/fill-pdf
// * Anvil Node.js client: https://github.com/anvilco/node-anvil
//
// This script is runnable as is, all you need to do is supply your own API key
// in the ANVIL_API_KEY environment variable in the .env file at the root of the
// typescript directory. By default this script fills a global sample template.
//
// yarn ts-node examples/fill-pdf.ts
//
// The filled PDF will be saved to `output/fill-output.pdf`. You can open the
// filled PDF immediately after saving the file on OSX machines with the
// `open` command:
//
// yarn ts-node examples/fill-pdf.ts && open output/fill-output.pdf
import fs from 'fs'
import path from 'path'
import Anvil from '@anvilco/anvil'
import run from '../lib/run'
// The PDF template ID to fill. This PDF template ID is a sample template
// available to anyone.
//
// See https://www.useanvil.com/help/tutorials/set-up-a-pdf-template for details
// on setting up your own template
const pdfTemplateID = '05xXsZko33JIO6aq5Pnr'
// Get your API key from your Anvil organization settings.
// See https://www.useanvil.com/docs/api/getting-started#api-key for more details.
const apiKey = process.env['ANVIL_API_KEY'] ?? ''
const outputFilepath = path.join(__dirname, '..', 'output', 'fill-output.pdf')
async function fillPDF () {
const anvilClient = new Anvil({ apiKey })
const exampleData = getExampleFillData()
console.log('Making fill request...')
const { statusCode, data, errors } = await anvilClient.fillPDF(pdfTemplateID, exampleData)
console.log('Finished! Status code:', statusCode) // => 200, 400, 404, etc
if (statusCode === 200 && data) {
// `data` will be the filled PDF binary data. It is important that the
// data is saved with no encoding! Otherwise the PDF file will be corrupt.
fs.writeFileSync(outputFilepath, data, { encoding: null })
console.log('Filled PDF saved to:', outputFilepath)
} else {
console.log('There were errors!')
console.log(JSON.stringify(errors, null, 2))
}
}
function getExampleFillData () {
// JSON data to fill the PDF
return {
title: 'My PDF Title',
fontSize: 10,
textColor: '#333333',
data: {
shortText: 'HELLOOW',
date: '2022-07-08',
name: {
firstName: 'Robin',
mi: 'W',
lastName: 'Smith'
},
email: '[email protected]',
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,
radioGroup: 'cast68d7e540afba11ecaf289fa5a354293a',
decimalNumber: 12345.67,
dollar: 123.45,
integer: 12345,
percent: 50.3,
longText: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.',
textPerLine: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.',
textPerLetter: 'taH9QGigei6G5BtTUA4',
image: 'https://placekitten.com/800/495'
}
}
}
run(fillPDF)