-
Notifications
You must be signed in to change notification settings - Fork 548
/
PDFRenderer.java
300 lines (272 loc) · 10.1 KB
/
PDFRenderer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package org.xhtmlrenderer.simple;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.HashMap;
/**
* <p/>
* PDFRenderer supports headless rendering of XHTML documents, outputting
* to PDF format. There are two static utility methods, one for rendering
* a {@link java.net.URL}, {@link #renderToPDF(String, String)} and one
* for rendering a {@link File}, {@link #renderToPDF(File, String)}</p>
* <p>You can use this utility from the command line by passing in
* the URL or file location as first parameter, and PDF path as second
* parameter:
* <pre>
* java -cp %classpath% org.xhtmlrenderer.simple.PDFRenderer <url> <pdf>
* </pre>
*
* @author Pete Brant
* @author Patrick Wright
*/
public class PDFRenderer {
private static final Map versionMap = new HashMap();
static {
versionMap.put("1.2", new Character(PdfWriter.VERSION_1_2));
versionMap.put("1.3", new Character(PdfWriter.VERSION_1_3));
versionMap.put("1.4", new Character(PdfWriter.VERSION_1_4));
versionMap.put("1.5", new Character(PdfWriter.VERSION_1_5));
versionMap.put("1.6", new Character(PdfWriter.VERSION_1_6));
versionMap.put("1.7", new Character(PdfWriter.VERSION_1_7));
}
/**
* Renders the XML file at the given URL as a PDF file
* at the target location.
*
* @param url url for the XML file to render
* @param pdf path to the PDF file to create
* @throws IOException if the URL or PDF location is
* invalid
* @throws DocumentException if an error occurred
* while building the Document.
*/
public static void renderToPDF(String url, String pdf)
throws IOException, DocumentException {
renderToPDF(url, pdf, null);
}
/**
* Renders the XML file at the given URL as a PDF file
* at the target location.
*
* @param url url for the XML file to render
* @param pdf path to the PDF file to create
* @param pdfVersion version of PDF to output; null uses default version
* @throws IOException if the URL or PDF location is
* invalid
* @throws DocumentException if an error occurred
* while building the Document.
*/
public static void renderToPDF(String url, String pdf, Character pdfVersion)
throws IOException, DocumentException {
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
if (pdfVersion != null) renderer.setPDFVersion(pdfVersion.charValue());
doRenderToPDF(renderer, pdf);
}
/**
* Renders the XML file as a PDF file at the target location.
*
* @param file XML file to render
* @param pdf path to the PDF file to create
* @throws IOException if the file or PDF location is
* invalid
* @throws DocumentException if an error occurred
* while building the Document.
*/
public static void renderToPDF(File file, String pdf)
throws IOException, DocumentException {
renderToPDF(file, pdf, null);
}
/**
* Renders the XML file as a PDF file at the target location.
*
* @param file XML file to render
* @param pdf path to the PDF file to create
* @param pdfVersion version of PDF to output; null uses default version
* @throws IOException if the file or PDF location is
* invalid
* @throws DocumentException if an error occurred
* while building the Document.
*/
public static void renderToPDF(File file, String pdf, Character pdfVersion)
throws IOException, DocumentException {
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(file);
if (pdfVersion != null) renderer.setPDFVersion(pdfVersion.charValue());
doRenderToPDF(renderer, pdf);
}
/**
* Renders the XML file as a PDF file and return its bytes.
*
* @param bytes XML file bytes to render
* @throws IOException if the file or PDF location is
* invalid
* @throws DocumentException if an error occurred
* while building the Document.
*/
public static byte[] renderToPDFBytes(byte[] bytes)
throws IOException, DocumentException {
byte[] b = renderToPDFBytes(bytes, null);
return b;
}
/**
* Renders the XML file as a PDF file return its bytes.
*
* @param bytes XML file bytes to render
* @param pdfVersion version of PDF to output; null uses default version
* @throws IOException if the file or PDF location is
* invalid
* @throws DocumentException if an error occurred
* while building the Document.
*/
public static byte[] renderToPDFBytes(byte[] bytes, Character pdfVersion)
throws IOException, DocumentException {
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(bytes);
if (pdfVersion != null) renderer.setPDFVersion(pdfVersion.charValue());
byte[] b = doRenderToPDF(renderer);
return b;
}
/**
* Renders the XML file as a PDF file at the target location.
*
* @param bytes XML file bytes to render
* @param pdf path to the PDF file to create
* @throws IOException if the file or PDF location is
* invalid
* @throws DocumentException if an error occurred
* while building the Document.
*/
public static void renderToPDF(byte[] bytes, String pdf)
throws IOException, DocumentException {
renderToPDF(bytes, pdf, null);
}
/**
* Renders the XML file as a PDF file at the target location.
*
* @param bytes XML file bytes to render
* @param pdf path to the PDF file to create
* @param pdfVersion version of PDF to output; null uses default version
* @throws IOException if the file or PDF location is
* invalid
* @throws DocumentException if an error occurred
* while building the Document.
*/
public static void renderToPDF(byte[] bytes, String pdf, Character pdfVersion)
throws IOException, DocumentException {
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(bytes);
if (pdfVersion != null) renderer.setPDFVersion(pdfVersion.charValue());
doRenderToPDF(renderer, pdf);
}
/**
* Internal use, runs the render process
* @param renderer
* @param pdf
* @throws com.itextpdf.text.DocumentException
* @throws java.io.IOException
*/
private static void doRenderToPDF(ITextRenderer renderer, String pdf)
throws IOException, DocumentException {
OutputStream os = null;
try {
os = new FileOutputStream(pdf);
renderer.layout();
renderer.createPDF(os);
os.close();
os = null;
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
// ignore
}
}
}
}
/**
* Internal use, runs the render process
* @param renderer
* @throws com.itextpdf.text.DocumentException
* @throws java.io.IOException
*/
private static byte[] doRenderToPDF(ITextRenderer renderer)
throws IOException, DocumentException {
ByteArrayOutputStream os = null;
byte[] pdf;
try {
os = new ByteArrayOutputStream();
renderer.layout();
renderer.createPDF(os);
pdf = os.toByteArray();
os.close();
os = null;
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
// ignore
}
}
}
return pdf;
}
/**
* Renders a file or URL to a PDF. Command line use: first
* argument is URL or file path, second
* argument is path to PDF file to generate.
*
* @param args see desc
* @throws IOException if source could not be read, or if
* PDF path is invalid
* @throws DocumentException if an error occurs while building
* the document
*/
public static void main(String[] args) throws IOException, DocumentException {
if (args.length < 2) {
usage("Incorrect argument list.");
}
Character pdfVersion = null;
if (args.length == 3) {
pdfVersion = checkVersion(args[2]);
}
String url = args[0];
if (url.indexOf("://") == -1) {
// maybe it's a file
File f = new File(url);
if (f.exists()) {
PDFRenderer.renderToPDF(f, args[1], pdfVersion);
} else {
usage("File to render is not found: " + url);
}
} else {
PDFRenderer.renderToPDF(url, args[1], pdfVersion);
}
}
private static Character checkVersion(String version) {
final Character val = (Character) versionMap.get(version.trim());
if (val == null) {
usage("Invalid PDF version number; use 1.2 through 1.7");
}
return val;
}
/** prints out usage information, with optional error message
* @param err
*/
private static void usage(String err) {
if (err != null && err.length() > 0) {
System.err.println("==>" + err);
}
System.err.println("Usage: ... url pdf [version]");
System.err.println(" where version (optional) is between 1.2 and 1.7");
System.exit(1);
}
}