-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added the ability to overlay one PDF over another #63
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
(ns pdfboxing.overlay | ||
(:import | ||
(org.apache.pdfbox.multipdf Overlay))) | ||
|
||
(defn overlay-pdf | ||
"overlay one PDF over another" | ||
[{:keys | ||
[input-file-path | ||
input-pdf | ||
overlay-file-path | ||
overlay-pdf | ||
first-page-overlay-pdf | ||
first-page-overlay-file-path | ||
last-page-overlay-file-path | ||
last-page-overlay-pdf | ||
all-page-overlay-file-path | ||
all-page-overlay-pdf | ||
odd-page-overlay-file-path | ||
odd-page-overlay-pdf | ||
even-page-overlay-file-path | ||
even-page-overlay-pdf | ||
^String output-file-path]}] | ||
|
||
(let [overlay (Overlay.)] | ||
(.setInputFile overlay input-file-path) | ||
(.setInputPDF overlay input-pdf) | ||
|
||
(.setDefaultOverlayFile overlay overlay-file-path) | ||
(.setDefaultOverlayPDF overlay overlay-pdf) | ||
|
||
(.setFirstPageOverlayFile overlay first-page-overlay-file-path) | ||
(.setFirstPageOverlayPDF overlay first-page-overlay-pdf) | ||
|
||
(.setLastPageOverlayFile overlay last-page-overlay-file-path) | ||
(.setLastPageOverlayPDF overlay last-page-overlay-pdf) | ||
|
||
(.setAllPagesOverlayFile overlay all-page-overlay-file-path) | ||
(.setAllPagesOverlayPDF overlay all-page-overlay-pdf) | ||
|
||
(.setOddPageOverlayFile overlay odd-page-overlay-file-path) | ||
(.setOddPageOverlayPDF overlay odd-page-overlay-pdf) | ||
|
||
(.setEvenPageOverlayFile overlay even-page-overlay-file-path) | ||
(.setEvenPageOverlayPDF overlay even-page-overlay-pdf) | ||
|
||
(-> overlay (.overlay {}) (.save output-file-path)) | ||
(.close overlay))) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
(ns pdfboxing.overlay-test | ||
(:require | ||
[clojure.java.io :as io] | ||
[clojure.test :refer [deftest is]] | ||
[pdfboxing.common :as common] | ||
[pdfboxing.overlay :refer [overlay-pdf]])) | ||
|
||
(def output-file-path "test/pdfs/foo.pdf") | ||
|
||
(deftest pdf-file-merge | ||
(let [_ | ||
(overlay-pdf | ||
{:input-file-path "test/pdfs/clojure-1.pdf" | ||
:overlay-file-path "test/pdfs/clojure-2.pdf" | ||
:output-file-path output-file-path}) | ||
|
||
overlayed-pdf-file (.exists (io/as-file output-file-path))] | ||
(is (true? overlayed-pdf-file)) | ||
(is (true? (common/is-pdf? output-file-path))))) | ||
|
||
;; clean up | ||
(defn clean-up [file] | ||
(if (.exists (io/as-file file)) | ||
(io/delete-file file))) | ||
|
||
(deftest cleaner | ||
(clean-up "test/pdfs/foo.pdf")) | ||
Comment on lines
+10
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some more feedback here as well!
You could also consider the following function: (defn with-temp-pdf [prefix fn]
(let [file (java.io.File/createTempFile prefix ".pdf")]
(fn (str file))
(if (.exists file)
(io/delete-file file)) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very good points! I followed this pattern from the tests in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies for hijacking the thread, I thought I'd offer some tips... Hope it's ok 🙂
So yeah, this is dotemacs' call but you might want to avoid saving to disk since that's a side-effect (and side-effects make writing proper tests harder, among other things)
I'd also argue that returning a byte array would be more useful to the end-user. Every user of your function is interested in the output, one way or the other, but how many will be interested in storing it locally right away?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem. Thank you for the feedback. I agree a byte array would be more useful in general but would mean its different from the other functions for example
draw
,form
andmerge
all save to disk.I'm happy to have it either way. @dotemacs is there reason to save to disk here or can I change this to return a byte array?