diff --git a/CHANGELOG.md b/CHANGELOG.md index f3a99aa..8a59b71 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Added - Added the ability to merge multiple images into a single PDF +- Added the ability to load PDFs from byte arrays ### Changed - Using lists for :imports diff --git a/src/pdfboxing/common.clj b/src/pdfboxing/common.clj index aa2a153..a7d36e4 100644 --- a/src/pdfboxing/common.clj +++ b/src/pdfboxing/common.clj @@ -39,6 +39,10 @@ (obtain-document [source])) (extend-protocol PDFDocument + (class (byte-array 0)) + (obtain-document [source] + (PDDocument/load source)) + String (obtain-document [source] (load-pdf source)) diff --git a/test/pdfboxing/common_test.clj b/test/pdfboxing/common_test.clj index a507750..34165eb 100644 --- a/test/pdfboxing/common_test.clj +++ b/test/pdfboxing/common_test.clj @@ -2,9 +2,10 @@ (:require [clojure.java.io :as io] [clojure.test :refer [deftest is testing]] [pdfboxing.common :refer [is-pdf? obtain-document]]) - (:import org.apache.pdfbox.pdmodel.PDDocument)) + (:import (org.apache.pdfbox.pdmodel PDDocument) + (java.nio.file Files))) -(deftest pdf-existance-check +(deftest pdf-existence-check (testing "If document supplied is a PDF" (is (false? (is-pdf? "test/pdfs/a.txt"))) (is (true? (is-pdf? "test/pdfs/hello.pdf"))))) @@ -36,3 +37,13 @@ (finally (when (instance? PDDocument doc) (.close doc))))))) + +(deftest obtain-document-returns-pddocument-if-provided-byte-array-check + (testing "obtain-document returns a PDDocument if a byte-array of a pdf is supplied" + (let [bytes (Files/readAllBytes (.toPath (io/file "test/pdfs/hello.pdf"))) + doc (obtain-document bytes)] + (try + (is (instance? PDDocument doc)) + (finally + (when (instance? PDDocument doc) + (.close doc))))))) \ No newline at end of file