Repository: dotemacs/pdfboxing
Pull request #58 (2020-10-29 20:55:48)

Added byte array parsing to obtain-document function
That way you can use other sources than the file system for loading pdfs.

## Description of your pull request

I needed the ability to load pdfs from other sources than files, in my case e-mails.

## Why should it be considered?

It's a small change and it adds a lot of useful functionality.

## Pull request checklist

Before submitting the PR make sure the following things have been done
(and denote this by checking the relevant checkboxes):

- [X] The code is consistent with [Clojure style guide](https://github.com/bbatsov/clojure-style-guide#the-clojure-style-guide).
- [X] All code passes the linter (`clj-kondo --lint src`).
- [X] You've added tests (if possible) to cover your change(s).
- [X] All tests are passing.
- [X] The commits are consistent with [the Git commit style guide](https://chris.beams.io/posts/git-commit/).
- [X] You've updated the [changelog](../blob/master/CHANGELOG.md) (if adding/changing user-visible functionality).


Thanks!


dotemacs: Hello @cguckes 

Sorry that I didn't spot this before and thanks for your PR.

I've got a couple of *pedantic* suggestions, I hope you don't mind :)

* instead of doing this merge:
![image](https://user-images.githubusercontent.com/152363/98449216-b0e36380-2129-11eb-98b5-c4c6d07385a2.png)
can you ensure that there are only non-merge commits in there? 
* if you can ensure that this commit's message: https://github.com/cguckes/pdfboxing/commit/5c968dd82d80e8a98e0e451212d32966c1bb6e00 conforms to the git commit style guide: https://chris.beams.io/posts/git-commit/

If this is too much work/annoying, let me know, and I'll do it.

Thanks
cguckes: @dotemacs, will do when I get the chance. Might be one or two days though...
dotemacs: 

> On 9 Nov 2020, at 09:15, Christopher Guckes <notifications@github.com> wrote:
> 
> 
> @dotemacs, will do when I get the chance. Might be one or two days though…

No rush, thank you :)



Patch:
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))


Test patch:
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