summaryrefslogtreecommitdiff
path: root/docs/examples/pdf-create-lowlevel.js
diff options
context:
space:
mode:
Diffstat (limited to 'docs/examples/pdf-create-lowlevel.js')
-rw-r--r--docs/examples/pdf-create-lowlevel.js63
1 files changed, 63 insertions, 0 deletions
diff --git a/docs/examples/pdf-create-lowlevel.js b/docs/examples/pdf-create-lowlevel.js
new file mode 100644
index 00000000..658adfb5
--- /dev/null
+++ b/docs/examples/pdf-create-lowlevel.js
@@ -0,0 +1,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")