-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate-html-to-pdf.ts
79 lines (71 loc) · 2.96 KB
/
generate-html-to-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
// Example: Generate a PDF from HTML and CSS via the Anvil API
//
// * PDF generation API docs: https://www.useanvil.com/docs/api/generate-pdf
// * Anvil Node.js client: https://github.com/anvilco/node-anvil
// * See our invoice HTML template for a more complex HTML to PDF example:
// https://github.com/anvilco/html-pdf-invoice-template
//
// 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.
//
// yarn ts-node examples/generate-html-to-pdf.ts
//
// The filled PDF will be saved to `output/generate-html-output.pdf`. You can
// open the filled PDF immediately after saving the file on OSX machines with
// the `open` command:
//
// yarn ts-node examples/generate-html-to-pdf.ts && open output/generate-html-output.pdf
import fs from 'fs'
import path from 'path'
import Anvil from '@anvilco/anvil'
import run from '../lib/run'
// 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', 'generate-html-output.pdf')
async function generateHTMLPDF () {
const anvilClient = new Anvil({ apiKey })
const exampleData = getExampleHTMLToPDFData()
const { statusCode, data, errors } = await anvilClient.generatePDF(exampleData)
console.log('Making HTML PDF generation request...')
console.log('Finished! Status code:', statusCode) // => 200, 400, 404, etc
if (statusCode === 200) {
// `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('Generated PDF saved to:', outputFilepath)
} else {
console.log('There were errors!')
console.log(JSON.stringify(errors, null, 2))
}
}
function getExampleHTMLToPDFData () {
return {
title: 'Example HTML to PDF',
type: 'html',
data: {
html: `
<h1 class='header-one'>What is Lorem Ipsum?</h1>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy text
ever since the <strong>1500s</strong>, when an unknown printer took
a galley of type and scrambled it to make a type specimen book.
</p>
<h3 class='header-two'>Where does it come from?</h3>
<p>
Contrary to popular belief, Lorem Ipsum is not simply random text.
It has roots in a piece of classical Latin literature from
<i>45 BC</i>, making it over <strong>2000</strong> years old.
</p>
`,
css: `
body { font-size: 14px; color: #171717; }
.header-one { text-decoration: underline; }
.header-two { font-style: underline; }
`,
},
}
}
run(generateHTMLPDF)