-
Notifications
You must be signed in to change notification settings - Fork 276
/
PrintHTML.java
414 lines (355 loc) · 16.7 KB
/
PrintHTML.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/**
* @author Tres Finocchiaro
*
* Copyright (C) 2016 Tres Finocchiaro, QZ Industries, LLC
*
* LGPL 2.1 This is free software. This software and source code are released under
* the "LGPL 2.1 License". A copy of this license should be distributed with
* this software. http://www.gnu.org/licenses/lgpl-2.1.html
*/
package qz.printer.action;
import com.sun.javafx.print.PrintHelper;
import com.sun.javafx.print.Units;
import javafx.print.*;
import org.apache.commons.io.IOUtils;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import qz.common.Constants;
import qz.printer.PrintOptions;
import qz.printer.PrintOutput;
import qz.printer.action.html.WebApp;
import qz.printer.action.html.WebAppModel;
import qz.utils.PrintingUtilities;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.CopiesSupported;
import javax.print.attribute.standard.Sides;
import javax.swing.*;
import java.awt.*;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
public class PrintHTML extends PrintImage implements PrintProcessor {
private static final Logger log = LogManager.getLogger(PrintHTML.class);
private List<WebAppModel> models;
private JLabel legacyLabel = null;
public PrintHTML() {
super();
models = new ArrayList<>();
}
@Override
public PrintingUtilities.Format getFormat() {
return PrintingUtilities.Format.HTML;
}
@Override
public void parseData(JSONArray printData, PrintOptions options) throws JSONException, UnsupportedOperationException {
try {
PrintOptions.Pixel pxlOpts = options.getPixelOptions();
if (!pxlOpts.isLegacy()) {
WebApp.initialize();
}
for(int i = 0; i < printData.length(); i++) {
JSONObject data = printData.getJSONObject(i);
PrintingUtilities.Flavor flavor = PrintingUtilities.Flavor.parse(data, PrintingUtilities.Flavor.FILE);
String source;
switch(flavor) {
case FILE:
case PLAIN:
// We'll toggle between 'plain' and 'file' when we construct WebAppModel
source = data.getString("data");
break;
default:
source = new String(flavor.read(data.getString("data")), StandardCharsets.UTF_8);
}
double pageZoom = (pxlOpts.getDensity() * pxlOpts.getUnits().as1Inch()) / 72.0;
if (pageZoom <= 1) { pageZoom = 1; }
double pageWidth = 0;
double pageHeight = 0;
double convertFactor = (72.0 / pxlOpts.getUnits().as1Inch());
boolean renderFromHeight = Arrays.asList(PrintOptions.Orientation.LANDSCAPE,
PrintOptions.Orientation.REVERSE_LANDSCAPE).contains(pxlOpts.getOrientation());
if (pxlOpts.getSize() != null) {
if (!renderFromHeight) {
pageWidth = pxlOpts.getSize().getWidth() * convertFactor;
} else {
pageWidth = pxlOpts.getSize().getHeight() * convertFactor;
}
} else if (options.getDefaultOptions().getPageSize() != null) {
if (!renderFromHeight) {
pageWidth = options.getDefaultOptions().getPageSize().getWidth();
} else {
pageWidth = options.getDefaultOptions().getPageSize().getHeight();
}
}
if (pxlOpts.getMargins() != null) {
PrintOptions.Margins margins = pxlOpts.getMargins();
if (!renderFromHeight || pxlOpts.isRasterize()) {
pageWidth -= (margins.left() + margins.right()) * convertFactor;
} else {
pageWidth -= (margins.top() + margins.bottom()) * convertFactor; //due to vector margin matching
}
}
if (!data.isNull("options")) {
JSONObject dataOpt = data.getJSONObject("options");
if (!dataOpt.isNull("pageWidth") && dataOpt.optDouble("pageWidth") > 0) {
pageWidth = dataOpt.optDouble("pageWidth") * convertFactor;
}
if (!dataOpt.isNull("pageHeight") && dataOpt.optDouble("pageHeight") > 0) {
pageHeight = dataOpt.optDouble("pageHeight") * convertFactor;
}
}
models.add(new WebAppModel(source, (flavor != PrintingUtilities.Flavor.FILE), pageWidth, pageHeight, pxlOpts.isScaleContent(), pageZoom));
}
log.debug("Parsed {} html records", models.size());
}
catch(IOException e) {
throw new UnsupportedOperationException("Unable to start JavaFX service", e);
}
catch(NoClassDefFoundError e) {
throw new UnsupportedOperationException("JavaFX libraries not found", e);
}
}
@Override
public void print(PrintOutput output, PrintOptions options) throws PrinterException {
if (options.getPixelOptions().isLegacy()) {
printLegacy(output, options);
} else if (options.getPixelOptions().isRasterize()) {
//grab a snapshot of the pages for PrintImage instead of printing directly
for(WebAppModel model : models) {
try { images.add(WebApp.raster(model)); }
catch(Throwable t) {
if (model.getZoom() > 1 && t instanceof IllegalArgumentException) {
//probably a unrecognized image loader error, try at default zoom
try {
log.warn("Capture failed with increased zoom, attempting with default value");
model.setZoom(1);
images.add(WebApp.raster(model));
}
catch(Throwable tt) {
throw new PrinterException(tt.getMessage());
}
} else {
throw new PrinterException(t.getMessage());
}
}
}
super.print(output, options);
} else {
Printer fxPrinter = null;
for(Printer p : Printer.getAllPrinters()) {
if (p.getName().equals(output.getPrintService().getName())) {
fxPrinter = p;
break;
}
}
if (fxPrinter == null) {
throw new PrinterException("Cannot find printer under the JavaFX libraries");
}
PrinterJob job = PrinterJob.createPrinterJob(fxPrinter);
// apply option settings
PrintOptions.Pixel pxlOpts = options.getPixelOptions();
JobSettings settings = job.getJobSettings();
settings.setJobName(pxlOpts.getJobName(Constants.HTML_PRINT));
settings.setPrintQuality(PrintQuality.HIGH);
if (pxlOpts.getColorType() != null) {
settings.setPrintColor(getColor(pxlOpts));
}
if (pxlOpts.getDuplex() == Sides.DUPLEX || pxlOpts.getDuplex() == Sides.TWO_SIDED_LONG_EDGE) {
settings.setPrintSides(PrintSides.DUPLEX);
}
if (pxlOpts.getDuplex() == Sides.TUMBLE || pxlOpts.getDuplex() == Sides.TWO_SIDED_SHORT_EDGE) {
settings.setPrintSides(PrintSides.TUMBLE);
}
if (pxlOpts.getPrinterTray() != null) {
PaperSource tray = findFXTray(fxPrinter.getPrinterAttributes().getSupportedPaperSources(), pxlOpts.getPrinterTray());
if (tray != null) {
settings.setPaperSource(tray);
}
}
if (pxlOpts.getDensity() > 0) {
settings.setPrintResolution(PrintHelper.createPrintResolution((int)pxlOpts.getDensity(), (int)pxlOpts.getDensity()));
}
Paper paper;
if (pxlOpts.getSize() != null && pxlOpts.getSize().getWidth() > 0 && pxlOpts.getSize().getHeight() > 0) {
double convert = 1;
Units units = getUnits(pxlOpts);
if (units == null) {
convert = 10; //need to adjust from cm to mm only for DPCM sizes
units = Units.MM;
}
paper = PrintHelper.createPaper("Custom", pxlOpts.getSize().getWidth() * convert, pxlOpts.getSize().getHeight() * convert, units);
} else {
PrintOptions.Size paperSize = options.getDefaultOptions().getPageSize();
paper = PrintHelper.createPaper("Default", paperSize.getWidth(), paperSize.getHeight(), Units.POINT);
}
PageOrientation orient = fxPrinter.getPrinterAttributes().getDefaultPageOrientation();
if (pxlOpts.getOrientation() != null) {
orient = getOrientation(pxlOpts);
}
try {
PageLayout layout;
PrintOptions.Margins m = pxlOpts.getMargins();
if (m != null) {
//force access to the page layout constructor as the adjusted margins on small sizes are wildly inaccurate
Constructor<PageLayout> plCon = PageLayout.class.getDeclaredConstructor(Paper.class, PageOrientation.class, double.class, double.class, double.class, double.class);
plCon.setAccessible(true);
//margins defined as pnt (1/72nds)
double asPnt = pxlOpts.getUnits().toInches() * 72;
if (orient == PageOrientation.PORTRAIT || orient == PageOrientation.REVERSE_PORTRAIT) {
layout = plCon.newInstance(paper, orient, m.left() * asPnt, m.right() * asPnt, m.top() * asPnt, m.bottom() * asPnt);
} else {
//rotate margins to match raster prints
layout = plCon.newInstance(paper, orient, m.top() * asPnt, m.bottom() * asPnt, m.right() * asPnt, m.left() * asPnt);
}
} else {
//if margins are not provided, use default paper margins
PageLayout valid = fxPrinter.getDefaultPageLayout();
layout = fxPrinter.createPageLayout(paper, orient, valid.getLeftMargin(), valid.getRightMargin(), valid.getTopMargin(), valid.getBottomMargin());
}
//force our layout as the default to avoid default-margin exceptions on small paper sizes
Field field = fxPrinter.getClass().getDeclaredField("defPageLayout");
field.setAccessible(true);
field.set(fxPrinter, layout);
settings.setPageLayout(layout);
}
catch(Exception e) {
log.error("Failed to set custom layout", e);
}
settings.setCopies(pxlOpts.getCopies());
log.trace("{}", settings.toString());
//javaFX lies about this value, so pull from original print service
CopiesSupported cSupport = (CopiesSupported)output.getPrintService()
.getSupportedAttributeValues(Copies.class, output.getPrintService().getSupportedDocFlavors()[0], null);
try {
if (cSupport != null && cSupport.contains(pxlOpts.getCopies())) {
for(WebAppModel model : models) {
WebApp.print(job, model);
}
} else {
settings.setCopies(1); //manually handle copies if they are not supported
for(int i = 0; i < pxlOpts.getCopies(); i++) {
for(WebAppModel model : models) {
WebApp.print(job, model);
}
}
}
}
catch(Throwable t) {
job.cancelJob();
throw new PrinterException(t.getMessage());
}
//send pending prints
job.endJob();
}
}
private void printLegacy(PrintOutput output, PrintOptions options) throws PrinterException {
PrintOptions.Pixel pxlOpts = options.getPixelOptions();
java.awt.print.PrinterJob job = java.awt.print.PrinterJob.getPrinterJob();
job.setPrintService(output.getPrintService());
PageFormat page = job.getPageFormat(null);
PrintRequestAttributeSet attributes = applyDefaultSettings(pxlOpts, page, output.getSupportedMedia());
//setup swing ui
JFrame legacyFrame = new JFrame(pxlOpts.getJobName(Constants.HTML_PRINT));
legacyFrame.setUndecorated(true);
legacyFrame.setLayout(new FlowLayout());
legacyFrame.setExtendedState(Frame.ICONIFIED);
legacyLabel = new JLabel();
legacyLabel.setOpaque(true);
legacyLabel.setBackground(Color.WHITE);
legacyLabel.setBorder(null);
legacyLabel.setDoubleBuffered(false);
legacyFrame.add(legacyLabel);
try {
for(WebAppModel model : models) {
if (model.isPlainText()) {
legacyLabel.setText(cleanHtmlContent(model.getSource()));
} else {
try(InputStream fis = new URL(model.getSource()).openStream()) {
String webPage = cleanHtmlContent(IOUtils.toString(fis, "UTF-8"));
legacyLabel.setText(webPage);
}
}
legacyFrame.pack();
legacyFrame.setVisible(true);
job.setPrintable(this);
printCopies(output, pxlOpts, job, attributes);
}
}
catch(Exception e) {
throw new PrinterException(e.getMessage());
}
finally {
legacyFrame.dispose();
}
}
private String cleanHtmlContent(String html) {
return html.replaceAll("^[\\s\\S]*<(HTML|html)\\b.*?>", "<html>");
}
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (legacyLabel == null) {
return super.print(graphics, pageFormat, pageIndex);
} else {
if (graphics == null) { throw new PrinterException("No graphics specified"); }
if (pageFormat == null) { throw new PrinterException("No page format specified"); }
if (pageIndex + 1 > models.size()) {
return NO_SUCH_PAGE;
}
log.trace("Requested page {} for printing", pageIndex);
Graphics2D graphics2D = (Graphics2D)graphics;
graphics2D.setRenderingHints(buildRenderingHints(dithering, interpolation));
graphics2D.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
graphics2D.scale(pageFormat.getImageableWidth() / pageFormat.getWidth(), pageFormat.getImageableHeight() / pageFormat.getHeight());
legacyLabel.paint(graphics2D);
return PAGE_EXISTS;
}
}
@Override
public void cleanup() {
super.cleanup();
models.clear();
legacyLabel = null;
}
public static Units getUnits(PrintOptions.Pixel opts) {
switch(opts.getUnits()) {
case INCH:
return Units.INCH;
case MM:
return Units.MM;
default:
return null;
}
}
public static PageOrientation getOrientation(PrintOptions.Pixel opts) {
switch(opts.getOrientation()) {
case LANDSCAPE:
return PageOrientation.LANDSCAPE;
case REVERSE_LANDSCAPE:
return PageOrientation.REVERSE_LANDSCAPE;
case REVERSE_PORTRAIT:
return PageOrientation.REVERSE_PORTRAIT;
default:
return PageOrientation.PORTRAIT;
}
}
public static PrintColor getColor(PrintOptions.Pixel opts) {
switch(opts.getColorType()) {
case COLOR:
return PrintColor.COLOR;
default:
return PrintColor.MONOCHROME;
}
}
}