Skip to content

Commit

Permalink
Merge pull request #58 from cguckes/master
Browse files Browse the repository at this point in the history
Added byte array parsing to obtain-document function
  • Loading branch information
dotemacs authored Nov 9, 2020
2 parents ccb4a63 + 5536619 commit 4e9d388
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/pdfboxing/common.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
15 changes: 13 additions & 2 deletions test/pdfboxing/common_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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")))))
Expand Down Expand Up @@ -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)))))))

0 comments on commit 4e9d388

Please sign in to comment.