Skip to content

Commit e6fa6de

Browse files
committed
Update dependencies and migrate JODConverter API changes
1 parent 0ca1288 commit e6fa6de

File tree

6 files changed

+80
-48
lines changed

6 files changed

+80
-48
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ contains final result of those calculations.
113113

114114
### PDF Generation
115115

116-
If you're on a system that uses an OpenOffice implementation of Excel, PDF
116+
If you're on a system that uses a LibreOffice or Apache OpenOffice implementation of Excel, PDF
117117
generation works the same was as creating a spreadsheet:
118118

119119
```clojure
@@ -250,4 +250,3 @@ For example, you can try:
250250
- A way to read in a saved workbook to the `{sheet-name [[cell]]}` format. I'm
251251
not sure what the best way to extract style data is, since there are so many
252252
possible values.
253-

project.clj

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
:url "https://github.com/matthewdowney/excel-clj"
44
:license {:name "Eclipse Public License"
55
:url "http://www.eclipse.org/legal/epl-v10.html"}
6-
:dependencies [[org.clojure/clojure "1.10.1"]
7-
[com.taoensso/encore "3.12.1"]
6+
:dependencies [[org.clojure/clojure "1.10.3"]
7+
[com.taoensso/encore "3.21.0"]
88
[com.taoensso/tufte "2.2.0"]
9-
[org.apache.poi/poi-ooxml "4.1.2"]
10-
[org.jodconverter/jodconverter-core "4.0.0-RELEASE"]])
9+
[org.apache.poi/poi-ooxml "5.2.0"]
10+
[org.jodconverter/jodconverter-local "4.4.2"]]
11+
:profiles {:test {:dependencies [[org.apache.logging.log4j/log4j-core "2.17.1"]
12+
[org.slf4j/slf4j-nop "1.7.36"]]}})

src/excel_clj/file.clj

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
(:import (org.apache.poi.xssf.streaming SXSSFSheet)
1616
(org.apache.poi.ss.usermodel Sheet)
1717
(java.io File)
18-
(org.jodconverter.office DefaultOfficeManagerBuilder)
19-
(org.jodconverter OfficeDocumentConverter)
18+
(org.jodconverter.local.office LocalOfficeManager)
19+
(org.jodconverter.local LocalConverter)
2020
(java.awt Desktop HeadlessException)))
2121

2222

@@ -140,16 +140,20 @@
140140
"Convert the `from-document`, either a File or a path to any office document,
141141
to pdf format and write the pdf to the given pdf-path.
142142
143-
Requires OpenOffice. See https://github.com/sbraconnier/jodconverter.
143+
Requires LibreOffice or Apache OpenOffice https://github.com/sbraconnier/jodconverter
144144
145145
Returns a File pointing at the PDF."
146146
[from-document pdf-path]
147147
(let [path (force-extension pdf-path "pdf")
148-
office-manager (.build (DefaultOfficeManagerBuilder.))]
148+
office-manager (-> (LocalOfficeManager/builder)
149+
(.build))]
149150
(.start office-manager)
150151
(try
151-
(let [document-converter (OfficeDocumentConverter. office-manager)]
152-
(.convert document-converter (io/file from-document) (io/file path)))
152+
(let [document-converter (LocalConverter/make office-manager)]
153+
(-> document-converter
154+
(.convert (io/file from-document))
155+
(.to (io/file path))
156+
(.execute)))
153157
(finally
154158
(.stop office-manager)))
155159
(io/file path)))
@@ -181,37 +185,38 @@
181185
(open (write-pdf! workbook (temp ".pdf"))))
182186

183187

184-
(defn example
185-
"Write & open a sheet composed of a simple grid."
186-
[]
187-
(let [grid [["A" "B" "C"]
188-
[1 2 3]]]
189-
(quick-open! {"Sheet 1" grid})))
190-
191-
192-
(defn example-plus
193-
"Write & open a sheet composed of a more involved grid."
194-
[]
195-
(let [t (java.util.Calendar/getInstance)
196-
grid [["String" "Abc"]
197-
["Numbers" 100M 1.234 1234 12345N]
198-
["Date (not styled, styled)" t (style t {:data-format :ymd})]]
199-
200-
header-style {:border-bottom :thin :font {:bold true}}
201-
header-rows [[(-> "Type"
202-
(style header-style)
203-
(dims {:height 2})
204-
(style {:vertical-alignment :center}))
205-
(-> "Examples"
206-
(style header-style)
207-
(dims {:width 4})
208-
(style {:alignment :center :border-bottom :none}))]
209-
(mapv #(style % {:font {:italic true}
210-
:alignment :center
211-
:border-bottom :thin})
212-
[nil 1 2 3 4])]
213-
excel-file (quick-open! {"Sheet 1" (concat header-rows grid)})]
214-
(try
215-
(open (convert-pdf! excel-file (temp ".pdf")))
216-
(catch Exception e
217-
(println "(Couldn't open a PDF on this platform.)")))))
188+
(comment
189+
(defn example
190+
"Write & open a sheet composed of a simple grid."
191+
[]
192+
(let [grid [["A" "B" "C"]
193+
[1 2 3]]]
194+
(quick-open! {"Sheet 1" grid})))
195+
196+
197+
(defn example-plus
198+
"Write & open a sheet composed of a more involved grid."
199+
[]
200+
(let [t (java.util.Calendar/getInstance)
201+
grid [["String" "Abc"]
202+
["Numbers" 100M 1.234 1234 12345N]
203+
["Date (not styled, styled)" t (style t {:data-format :ymd})]]
204+
205+
header-style {:border-bottom :thin :font {:bold true}}
206+
header-rows [[(-> "Type"
207+
(style header-style)
208+
(dims {:height 2})
209+
(style {:vertical-alignment :center}))
210+
(-> "Examples"
211+
(style header-style)
212+
(dims {:width 4})
213+
(style {:alignment :center :border-bottom :none}))]
214+
(mapv #(style % {:font {:italic true}
215+
:alignment :center
216+
:border-bottom :thin})
217+
[nil 1 2 3 4])]
218+
excel-file (quick-open! {"Sheet 1" (concat header-rows grid)})]
219+
(try
220+
(open (convert-pdf! excel-file (temp ".pdf")))
221+
(catch Exception e
222+
(println "(Couldn't open a PDF on this platform.)"))))))

test/excel_clj/core_test.clj

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@
5454
(println "Writing example template...")
5555
(let [template (clojure.java.io/resource "uptime-template.xlsx")
5656
new-data {"raw" (table-grid example-template-data)}]
57-
(append! new-data template "filled-in-template.xlsx")))
57+
(append! new-data template temp-file)))
5858
(finally
5959
(io/delete-file temp-file)))))
60+
61+
62+
(deftest convert-pdy-test
63+
(let [temp-file (io/file (temp ".xlsx"))]
64+
(try
65+
(testing "Example code snippet writes successfully."
66+
(println "Writing example template...")
67+
(let [template (clojure.java.io/resource "uptime-template.xlsx")
68+
new-data {"raw" (table-grid example-template-data)}]
69+
(append! new-data template temp-file)))
70+
(finally
71+
(io/delete-file temp-file)))))
72+

test/excel_clj/file_test.clj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(ns excel-clj.file-test
2+
(:require [clojure.test :refer :all]
3+
[excel-clj.file :as file]
4+
[clojure.java.io :as io]))
5+
6+
(deftest convert-to-pdf-test
7+
(let [input-file (clojure.java.io/resource "uptime-template.xlsx")
8+
temp-pdf-file (io/file (file/temp ".pdf"))]
9+
(try
10+
(testing "Convert XLSX file to PDF"
11+
(println "Writing example PDF...")
12+
(file/convert-pdf! input-file temp-pdf-file))
13+
(finally
14+
(io/delete-file temp-pdf-file)))))

test/excel_clj/poi_test.clj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
:success)
1212
"Example function writes successfully."))
1313

14-
1514
(deftest performance-test
1615
(testing "Performance is reasonable"
1716
(println "Starting performance test -- writing to a temp file...")

0 commit comments

Comments
 (0)