jpx-to-pdf.js 946 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Script to create a PDF from JPEG2000 images.
  2. // Each image be put on its own page.
  3. // This script can be used to create files to test JPEG2000 support in PDF viewers.
  4. var doc = new PDFDocument();
  5. function addJPXImage(filename, w, h) {
  6. return doc.addRawStream(
  7. readFile(filename),
  8. {
  9. Type: "XObject",
  10. Subtype: "Image",
  11. Width: w,
  12. Height: h,
  13. Filter: "JPXDecode"
  14. }
  15. );
  16. }
  17. function addJPXPage(filename) {
  18. var image = new Image(filename);
  19. var w = image.getWidth();
  20. var h = image.getHeight();
  21. var mediabox = [0, 0, w, h];
  22. var resources = { XObject: { I: addJPXImage(filename, w, h) } };
  23. var contents = "q " + w + " 0 0 " + h + " 0 0 cm /I Do Q";
  24. doc.insertPage(-1, doc.addPage(mediabox, 0, resources, contents));
  25. }
  26. var i, n = scriptArgs.length;
  27. if (n < 1) {
  28. print("usage: mutool run jpx-to-pdf.js file.jpx ...");
  29. quit();
  30. }
  31. for (i = 0; i < n; ++i) {
  32. addJPXPage(scriptArgs[i]);
  33. }
  34. doc.save("out.pdf", "ascii,pretty");