-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
pdf-images.js
58 lines (47 loc) · 1.09 KB
/
pdf-images.js
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
const fs = require('fs')
const { Image, createCanvas } = require('..')
const canvas = createCanvas(500, 500, 'pdf')
const ctx = canvas.getContext('2d')
let x, y
function reset () {
x = 50
y = 80
}
function h1 (str) {
ctx.font = '22px Helvetica'
ctx.fillText(str, x, y)
}
function p (str) {
ctx.font = '10px Arial'
ctx.fillText(str, x, (y += 20))
}
function img (src) {
const img = new Image()
img.src = src
ctx.drawImage(img, x, (y += 20))
y += img.height
}
reset()
h1('PDF image demo')
p('This is an image embedded in a PDF')
img('examples/images/squid.png')
p('Figure 1.0 - Some squid thing')
ctx.addPage()
reset()
h1('Lime cat')
p('This is a pretty sweet cat')
img('examples/images/lime-cat.jpg')
p('Figure 1.1 - Lime cat is awesome')
ctx.addPage()
const buff = canvas.toBuffer('application/pdf', {
title: 'Squid and Cat!',
author: 'Octocat',
subject: 'An example PDF made with node-canvas',
keywords: 'node.js squid cat lime',
creator: 'my app',
modDate: new Date()
})
fs.writeFile('out.pdf', buff, function (err) {
if (err) throw err
console.log('created out.pdf')
})