Skip to content

Commit

Permalink
Add image merging to PDF
Browse files Browse the repository at this point in the history
The usage is to merge any number of images provided as a vector to a
single PDF file.

This change was added by Petr Mensik on 11th July 2018, here:
#45

But then the branch from which the PR was created, was deleted. So I
added these changes later, manually.
  • Loading branch information
pmensik authored and dotemacs committed Sep 6, 2020
1 parent 0e122dc commit c37f72e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## WIP

### Added
- Added the ability to merge multiple images into a single PDF

### Changed
- Using lists for :imports
- Updated documentation of `split-pdf-at` in the README
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ 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")
```

### Merge multiple images into single PDF

You can use either `merge-images-from-path` for providing images in
form of vector of string paths or `merge-images-from-byte-array` to
provide them as a vector of byte arrays. Each image will be inserted
into its own page.


```clojure
(require '[pdfboxing.merge :as pdf])
(pdf/merge-images-from-path ["image1.png" "image2.png"] "output.pdf")
```

### Split a PDF into mutliple PDDocuments
```clojure
(require '[pdfboxing.split :as pdf])
Expand Down
48 changes: 43 additions & 5 deletions src/pdfboxing/merge.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
(ns pdfboxing.merge
(:require [pdfboxing.common :as common])
(:import (java.io File FileInputStream)
org.apache.pdfbox.multipdf.PDFMergerUtility))
(org.apache.pdfbox.multipdf PDFMergerUtility)
(org.apache.pdfbox.pdmodel PDDocument
PDPage
PDPageContentStream)
(org.apache.pdfbox.pdmodel.common PDRectangle)
(org.apache.pdfbox.pdmodel.graphics.image PDImageXObject)))

(defn throw-exception
[message]
Expand Down Expand Up @@ -31,7 +36,40 @@
[& {:keys [output input]}]
{:pre [(arg-check output input)]}
(let [merger (PDFMergerUtility.)]
(doseq [f input]
(.addSource merger (FileInputStream. (File. f))))
(.setDestinationFileName merger output)
(.mergeDocuments merger)))
(doseq [f input]
(.addSource merger (FileInputStream. (File. f))))
(.setDestinationFileName merger output)
(.mergeDocuments merger)))

(defn- add-image-to-page
"Adds image as a page to the document object"
[doc ^org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject image]
(let [page-size PDRectangle/A4
original-width (.getWidth image)
original-height (.getHeight image)
page-width (.getWidth page-size)
page-height (.getHeight page-size)
ratio (min (/ page-width original-width) (/ page-height original-height))
scaled-width (* original-width ratio)
scaled-height (* original-height ratio)
x (/ (- page-width scaled-width) 2)
y (/ (- page-height scaled-height) 2)
page (PDPage. page-size)]
(.addPage doc page)
(with-open [stream (PDPageContentStream. doc page)]
(.drawImage stream image x y scaled-width scaled-height))))

(defn merge-images-from-byte-array
"Merges images provided as a vector of byte arrays into one PDF"
[images output-doc]
(with-open [doc (PDDocument.)]
(run! #(add-image-to-page doc (PDImageXObject/createFromByteArray doc % nil)) images)
(.save doc output-doc)))

(defn merge-images-from-path
"Merges images provided as a vector of string paths"
[images output-doc]
(with-open [doc (PDDocument.)]
(run! #(add-image-to-page doc (PDImageXObject/createFromFile % doc))
images)
(.save doc output-doc)))
Binary file added test/images/image1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/images/image2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/pdfs/image_merge.pdf
Binary file not shown.

0 comments on commit c37f72e

Please sign in to comment.