summaryrefslogtreecommitdiff
path: root/docs/examples/jpx-to-pdf.js
blob: ee95ab305988380e6a3669e69e792fcb056cb7ab (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
// Script to create a PDF from JPEG2000 images.
// Each image be put on its own page.
// This script can be used to create files to test JPEG2000 support in PDF viewers.

var doc = new PDFDocument();

function addJPXImage(filename, w, h) {
	return doc.addRawStream(
		readFile(filename),
		{
			Type: "XObject",
			Subtype: "Image",
			Width: w,
			Height: h,
			Filter: "JPXDecode"
		}
	);
}

function addJPXPage(filename) {
	var image = new Image(filename);
	var w = image.getWidth();
	var h = image.getHeight();
	var mediabox = [0, 0, w, h];
	var resources = { XObject: { I: addJPXImage(filename, w, h) } };
	var contents = "q " + w + " 0 0 " + h + " 0 0 cm /I Do Q";
	doc.insertPage(-1, doc.addPage(mediabox, 0, resources, contents));
}

var i, n = argv.length;
if (n < 2) {
	print("usage: mutool run jpx-to-pdf.js file.jpx ...");
	quit();
}
for (i = 1; i < n; ++i) {
	addJPXPage(argv[i]);
}

doc.save("out.pdf", "ascii,pretty");