Skip to content

Instantly share code, notes, and snippets.

@jamesdavidson
Created June 2, 2023 01:45
Show Gist options
  • Save jamesdavidson/51e6730656b168f46362c1bf9832fbea to your computer and use it in GitHub Desktop.
Save jamesdavidson/51e6730656b168f46362c1bf9832fbea to your computer and use it in GitHub Desktop.
Clojure snippet for creating PDF where each page is from a PNG image file
;; org.clojure/clojure {:mvn/version "1.11.1"}
;; com.github.librepdf/openpdf {:mvn/version "1.3.30"}
(import 'com.lowagie.text.Document)
(import 'com.lowagie.text.Image)
(import 'com.lowagie.text.PageSize)
(import 'com.lowagie.text.pdf.PdfWriter)
(def doc (new Document))
(def os (io/output-stream "output.pdf"))
(PdfWriter/getInstance doc os)
(def img-is (io/input-stream "../Downloads/screenshot-7.png"))
(def img-baos (new java.io.ByteArrayOutputStream))
(io/copy img-is img-baos)
(def img (Image/getInstance (.toByteArray img-baos)))
(.setPageSize doc img)
(.newPage doc)
(.setAbsolutePosition img 0 0)
(.open doc)
(.add doc img)
(.close doc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment