comparison mupdf-source/docs/examples/pdf-create-lowlevel.js @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 import * as mupdf from "mupdf"
2
3 // Create a PDF from scratch.
4
5 // This example creates a new PDF file from scratch, using only the low level APIs.
6 // This assumes a basic working knowledge of the PDF file format.
7
8 // Create a new empty document with no pages.
9 var pdf = new mupdf.PDFDocument()
10
11 // Create and add a font resource.
12 var font = pdf.addObject({
13 Type: "Font",
14 Subtype: "Type1",
15 Encoding: "WinAnsiEncoding",
16 BaseFont: "Times-Roman",
17 })
18
19 // Create and add an image resource:
20 var image = pdf.addRawStream(
21 // The raw stream contents, hex encoded to match the Filter entry:
22 "004488CCEEBB7733>",
23 // The image object dictionary:
24 {
25 Type: "XObject",
26 Subtype: "Image",
27 Width: 4,
28 Height: 2,
29 BitsPerComponent: 8,
30 ColorSpace: "DeviceGray",
31 Filter: "ASCIIHexDecode",
32 }
33 );
34
35 // Create resource dictionary.
36 var resources = pdf.addObject({
37 Font: { Tm: font },
38 XObject: { Im0: image },
39 })
40
41 // Create content stream.
42 var buffer = new Buffer()
43 buffer.writeLine("10 10 280 330 re s")
44 buffer.writeLine("q 200 0 0 200 50 100 cm /Im0 Do Q")
45 buffer.writeLine("BT /Tm 16 Tf 50 50 TD (Hello, world!) Tj ET")
46 var contents = pdf.addStream(buffer)
47
48 // Create page object.
49 var page = pdf.addObject({
50 Type: "Page",
51 MediaBox: [0,0,300,350],
52 Contents: contents,
53 Resources: resources,
54 })
55
56 // Insert page object into page tree.
57 var pagetree = pdf.getTrailer().Root.Pages
58 pagetree.Count = 1
59 pagetree.Kids = [ page ]
60 page.Parent = pagetree
61
62 // Save the document.
63 pdf.save("out.pdf")