Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/docs/examples/pdf-create.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 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") |
