Extract page from PDF using iText and clojure
- by KobbyPemson
I am trying to extract a single page from a pdf with clojure by translating the splitPDF method I found here http://viralpatel.net/blogs/itext-tutorial-merge-split-pdf-files-using-itext-jar/
I keep getting this error
IOException Stream Closed java.io.FileOutputStream.writeBytes (:-2)
This prevents me from opening the document while the repl is still open. Once I close the repl I'm able to access the document.
Why do I get the error?
How do I fix it ?
How can I make it more clojurey?
(import '(com.itextpdf.text Document)
'(com.itextpdf.text.pdf PdfReader PdfWriter PdfContentByte PdfImportedPage BaseFont)
'(java.io File FileInputStream FileOutputStream InputStream OutputStream))
(defn extract-page [src dest pagenum]
(with-open [ d (Document.)
os (FileOutputStream. dest)]
(let [ srcpdf (->> src FileInputStream. PdfReader.)
destpdf (PdfWriter/getInstance d os)]
(doto d
(.open )
(.newPage ))
(.addTemplate
(.getDirectContent destpdf)
(.getImportedPage destpdf srcpdf pagenum) 0 0))))