forked from dotemacs/pdfboxing
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for exporting pdf to image
[Re dotemacs#55] This commit also changes slightly the prerequisities for split function Previously it only allowed strings as inputs. IMHO it should also accept files.
- Loading branch information
Damian Hryniewicz
committed
Nov 12, 2020
1 parent
4e9d388
commit 25eff3f
Showing
7 changed files
with
101 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
(ns pdfboxing.image | ||
(:require [clojure.spec.alpha :as s] | ||
[pdfboxing.common :as common] | ||
[pdfboxing.info :as info]) | ||
(:import [org.apache.pdfbox.rendering PDFRenderer ImageType] | ||
[org.apache.pdfbox.pdmodel PDDocument] | ||
[java.awt.image BufferedImage])) | ||
|
||
(s/def ::input #(or (instance? PDDocument %) | ||
(common/is-pdf? %))) | ||
(s/def ::dpi int?) | ||
(s/def ::page-idx int?) | ||
(s/def ::export-to-image-config | ||
(s/keys :req-un [::input] | ||
:opt-un [::dpi ::page-idx])) | ||
|
||
(s/def ::export-to-image-ret #(instance? BufferedImage %)) | ||
|
||
(defn- page-idx-in-bounds | ||
[page-idx input] | ||
(if (<= 0 page-idx (dec (info/page-number input))) | ||
true | ||
(common/throw-exception "Page index out of bounds"))) | ||
|
||
(defn export-to-image | ||
"Export PDF or PDDocument into BufferedImage | ||
Only one page will be exported (first by default). | ||
Split the document first if you want one image for each page." | ||
[& {:keys [input dpi page-idx] | ||
:or {dpi 300 page-idx 0} | ||
:as config}] | ||
{:pre [(s/valid? ::export-to-image-config config) | ||
(page-idx-in-bounds page-idx input)] | ||
:post [(s/valid? ::export-to-image-ret %)]} | ||
(with-open [doc (common/obtain-document input)] | ||
(.renderImageWithDPI (PDFRenderer. doc) page-idx dpi ImageType/RGB))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
(ns pdfboxing.image-test | ||
(:require [clojure.test :refer [deftest is]] | ||
[pdfboxing.image :as image]) | ||
(:import [java.awt.image BufferedImage])) | ||
|
||
(deftest export-to-image | ||
(let [file "test/pdfs/multi-page.pdf" | ||
exporting-outcome (image/export-to-image :input file) | ||
exporting-outcome-other-page (image/export-to-image :input file :page-idx 1) | ||
exporting-outcome-small-dpi (image/export-to-image :input file :dpi 72)] | ||
(is (instance? BufferedImage exporting-outcome)) | ||
(is (instance? BufferedImage exporting-outcome-other-page)) | ||
(is (thrown? IllegalArgumentException (image/export-to-image :input file :page-idx 100))) | ||
(is (instance? BufferedImage exporting-outcome-small-dpi)) | ||
(is (not= exporting-outcome exporting-outcome-other-page)) | ||
(is (> (.getWidth exporting-outcome) (.getWidth exporting-outcome-small-dpi))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters