comparison mupdf-source/docs/examples/pdf-create.js @ 3:2c135c81b16c

MERGE: upstream PyMuPDF 1.26.4 with MuPDF 1.26.7
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:44:09 +0200
parents b50eed0cc0ef
children
comparison
equal deleted inserted replaced
0:6015a75abc2d 3:2c135c81b16c
1 import * as mupdf from "mupdf"
2
3 // Create a PDF from scratch using helper functions.
4
5 // This example creates a new PDF file from scratch, using helper
6 // functions to create resources and page objects.
7 // This assumes a basic working knowledge of the PDF file format.
8
9 import mupdf from "mupdf"
10
11 // Create a new empty document with no pages.
12 var pdf = new mupdf.PDFDocument()
13
14 // Load built-in font and create WinAnsi encoded simple font resource.
15 var font = pdf.addSimpleFont(new mupdf.Font("Times-Roman"))
16
17 // Load PNG file and create image resource.
18 var image = pdf.addImage(new mupdf.Image("example.jpg"))
19
20 // Create resource dictionary.
21 var resources = pdf.addObject({
22 Font: { Tm: font },
23 XObject: { Im0: image },
24 })
25
26 // Create content stream data.
27 var contents =
28 "10 10 280 330 re s\n" +
29 "q 200 0 0 200 50 100 cm /Im0 Do Q\n" +
30 "BT /Tm 16 Tf 50 50 TD (Hello, world!) Tj ET\n"
31
32 // Create a new page object.
33 var page = pdf.addPage([0,0,300,350], 0, resources, contents)
34
35 // Insert page object at the end of the document.
36 pdf.insertPage(-1, page)
37
38 // Save the document to file.
39 pdf.save("out.pdf", "pretty,ascii,compress-images,compress-fonts")