Extract page from PDF using iText and clojure

Posted by KobbyPemson on Stack Overflow See other posts from Stack Overflow or by KobbyPemson
Published on 2012-08-27T19:36:20Z Indexed on 2012/08/28 9:38 UTC
Read the original article Hit count: 277

Filed under:
|

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))))

© Stack Overflow or respective owner

Related posts about clojure

Related posts about itext