From 73b7422c6b7dce14da456bf12c7c65c7301dc2e8 Mon Sep 17 00:00:00 2001 From: Brian Button Date: Fri, 15 Jan 2021 13:22:19 +0200 Subject: [PATCH] Added the ability to overlay one PDF over another --- CHANGELOG.md | 1 + README.md | 10 +++++++ src/pdfboxing/overlay.clj | 47 +++++++++++++++++++++++++++++++++ test/pdfboxing/overlay_test.clj | 27 +++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 src/pdfboxing/overlay.clj create mode 100644 test/pdfboxing/overlay_test.clj diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a59b71..aca5694 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Added - Added the ability to merge multiple images into a single PDF - Added the ability to load PDFs from byte arrays +- Added the ability to overlay one PDF over another ### Changed - Using lists for :imports diff --git a/README.md b/README.md index edf9f32..02dc34a 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,16 @@ Clojure PDF manipulation library & wrapper for [PDFBox](http://pdfbox.apache.org (pdf/merge-pdfs :input ["test/pdfs/clojure-1.pdf" "test/pdfs/clojure-2.pdf"] :output "foo.pdf") ``` +### Overlay one PDF over another + +```clojure +(require '[pdfboxing.overlay :as pdf-overlay]) +(pdf-overlay/overlay-pdf + {:input-file-path "test/pdfs/clojure-1.pdf" + :overlay-file-path "test/pdfs/clojure-2.pdf" + :output-file-path "test/pdfs/foo.pdf"}) +``` + ### Merge multiple images into single PDF You can use either `merge-images-from-path` for providing images in diff --git a/src/pdfboxing/overlay.clj b/src/pdfboxing/overlay.clj new file mode 100644 index 0000000..c3025e7 --- /dev/null +++ b/src/pdfboxing/overlay.clj @@ -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))) \ No newline at end of file diff --git a/test/pdfboxing/overlay_test.clj b/test/pdfboxing/overlay_test.clj new file mode 100644 index 0000000..6846305 --- /dev/null +++ b/test/pdfboxing/overlay_test.clj @@ -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"))