Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Enable saving of PDF metadata

 - Can store basic metadata such as: author, title, keywords
 - Added relevant tests to read the metadata back out from the PDF
  • Loading branch information...
commit 73e39adc67c2c206fe4c9db951cdded40607bedd 1 parent aa720be
@KushalP authored
View
2  HISTORY.md
@@ -1,6 +1,8 @@
0.1.2 (in progress)
==================
+* The document map can now contain (some) metadata, which will be affixed to the PDF when saving.
+
0.1.1 (2012-04-09)
==================
View
5 README.md
@@ -24,7 +24,10 @@ Start using the library!
;; Create PDF files with some text.
(-> {:font "Helvetica-Bold"
:size 12
- :text "Hello World"}
+ :text "Hello World!"
+ :metadata {:author "Joe Bloggs"
+ :title "Hello World"
+ :keywords ["test" "hello" "world"]}}
(save-as "/tmp/test.pdf"))
;; Merge a number of existing PDF files.
View
10 src/camelot/core.clj
@@ -1,5 +1,6 @@
(ns camelot.core
- (:import (org.apache.pdfbox.pdmodel PDDocument PDPage)
+ (:require [clojure.string :as str])
+ (:import (org.apache.pdfbox.pdmodel PDDocument PDDocumentInformation PDPage)
(org.apache.pdfbox.pdmodel.edit PDPageContentStream)
(org.apache.pdfbox.pdmodel.font PDType1Font)
(org.apache.pdfbox.util PDFMergerUtility)
@@ -42,6 +43,13 @@
(.drawString content (doc-map :text))
(.endText content)
(.close content)
+ (when (contains? doc-map :metadata)
+ (let [meta (PDDocumentInformation.)
+ data (doc-map :metadata)]
+ (.setAuthor meta (data :author))
+ (.setKeywords meta (str/join ", " (data :keywords)))
+ (.setTitle meta (data :title))
+ (.setDocumentInformation doc meta)))
(.save doc filename)
(finally (if (not (nil? doc))
(.close doc))))
View
17 test/camelot/test/core.clj
@@ -39,6 +39,23 @@
(is (= "file" (file-kind filename)))
(is (= 1 (.getPageCount doc)))))
+(deftest save-as-builds-a-basic-pdf-file-with-metadata
+ (let [filename (temp-pdf-filename)
+ doc (-> {:font "Helvetica-Bold"
+ :size 12
+ :text "Hello World"
+ :metadata {:author "Joe Bloggs"
+ :title "Hello World"
+ :keywords ["test" "hello" "world"]}}
+ (save-as filename))
+ info (.getDocumentInformation doc)]
+ (is (instance? PDDocument doc))
+ (is (= "file" (file-kind filename)))
+ (is (= 1 (.getPageCount doc)))
+ (is (= "Joe Bloggs" (.getAuthor info)))
+ (is (= "test, hello, world" (.getKeywords info)))
+ (is (= "Hello World" (.getTitle info)))))
+
(deftest merge-pdf-bad-input-throws-assertion-error
(let [filename (temp-pdf-filename)
string-vec ["one" "two"]]
Please sign in to comment.
Something went wrong with that request. Please try again.