summaryrefslogtreecommitdiff
path: root/docs/mutool/examples/pdf-create-lowlevel.js
blob: b55e22cf45fa8302551639d923f058ea3b7707b8 (plain)
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
// Create a PDF from scratch.

// This example creates a new PDF file from scratch, using only the low level APIs.
// This assumes a basic working knowledge of the PDF file format.

// Create a new empty document with no pages.
var pdf = new PDFDocument()

// Create and add a font resource.
var font = pdf.addObject({
	Type: "Font",
	Subtype: "Type1",
	Encoding: "WinAnsiEncoding",
	BaseFont: "Times-Roman",
})

// Create and add an image resource:
// Allocate a slot for a new object and get a reference to it.
var image = pdf.createObject()
// Write a dictionary object into the slot.
image.writeObject({
	Type: "XObject",
	Subtype: "Image",
	Width: 4,
	Height: 2,
	BitsPerComponent: 8,
	ColorSpace: "DeviceGray",
	// The compression filter to be used:
	Filter: "ASCIIHexDecode",
})
// Write raw stream data into the slot; hex encoded
// to match the Filter entry in the dictionary.
image.writeRawStream("004488CCEEBB7733>")

// Create resource dictionary.
var resources = pdf.addObject({
	Font: { Tm: font },
	XObject: { Im0: image },
})

// Create content stream.
var buffer = new Buffer()
buffer.writeLine("10 10 280 330 re s")
buffer.writeLine("q 200 0 0 200 50 100 cm /Im0 Do Q")
buffer.writeLine("BT /Tm 16 Tf 50 50 TD (Hello, world!) Tj ET")
var contents = pdf.addStream(buffer)

// Create page object.
var page = pdf.addObject({
	Type: "Page",
	MediaBox: [0,0,300,350],
	Contents: contents,
	Resources: resources,
})

// Insert page object into page tree.
var pagetree = pdf.getTrailer().Root.Pages
pagetree.Count = 1
pagetree.Kids = [ page ]
page.Parent = pagetree

// Save the document.
pdf.save("out.pdf")