-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathApp.java
More file actions
134 lines (111 loc) · 4.23 KB
/
App.java
File metadata and controls
134 lines (111 loc) · 4.23 KB
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
package com.openhtmltopdf.cli;
import java.io.File;
import java.io.FileOutputStream;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import com.openhtmltopdf.outputdevice.helper.ExternalResourceControlPriority;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
import com.openhtmltopdf.util.XRLog;
import org.jsoup.Jsoup;
import org.jsoup.helper.W3CDom;
import org.w3c.dom.Document;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
public class App
{
@Command(
name = "convert",
description = "Converts a single html file into a PDF"
)
public static class Run implements Callable<Integer> {
@Option(
names = { "--input", "-i" },
description = "The html input file",
paramLabel = "<input>",
required = true
)
File input;
@Option(
names = { "--output", "-o" },
description = "The PDF output file",
paramLabel = "<output>",
required = true
)
File output;
@Option(
names = { "--xhtml", "-x" },
description = "Use to specify that the input file is valid XHTML (skipping the HTML to XHTML step)"
)
boolean xhtml = false;
@Option(
names = { "--verbose", "-v" },
description = "Verbose logging"
)
boolean verbose = false;
@Option(
names = { "--quiet", "-q" },
description = "Quiet logging"
)
boolean quiet = false;
@Option(
names = { "--block", "-b" },
description = "Block linked resources (CSS, images, fonts)"
)
boolean block;
@Option(
names = { "--accessible", "-a"},
description = "Force PDF/UA Conformance"
)
boolean accessible;
@Override
public Integer call() throws Exception {
if (quiet && !verbose) {
XRLog.listRegisteredLoggers().forEach(logger -> XRLog.setLevel(logger, Level.OFF));
}
if (!verbose && !quiet) {
XRLog.listRegisteredLoggers().forEach(logger -> XRLog.setLevel(logger, Level.WARNING));
}
long timeStart = System.currentTimeMillis();
if (!quiet) {
System.out.println("Attempting to convert '" + input.getAbsolutePath() + "' to PDF at '" + output.getAbsolutePath() + "'");
}
try (FileOutputStream os = new FileOutputStream(output)) {
PdfRendererBuilder builder = new PdfRendererBuilder();
if (block) {
builder.useUriResolver((base, rel) -> null);
builder.useExternalResourceAccessControl((uri, type) -> false, ExternalResourceControlPriority.RUN_BEFORE_RESOLVING_URI);
builder.useExternalResourceAccessControl((uri, type) -> false, ExternalResourceControlPriority.RUN_AFTER_RESOLVING_URI);
}
if (!xhtml) {
org.jsoup.nodes.Document jsoup = Jsoup.parse(input, "UTF-8");
Document doc = new W3CDom().fromJsoup(jsoup);
builder.withW3cDocument(doc, input.getAbsoluteFile().toURI().toURL().toExternalForm());
} else {
builder.withFile(input);
}
if (accessible) {
builder.usePdfUaAccessbility(true);
builder.usePdfAConformance(PdfRendererBuilder.PdfAConformance.PDFA_3_U);
}
builder.toStream(os);
builder.useFastMode();
builder.run();
if (!quiet) {
System.out.println("Successfully created PDF in " + (System.currentTimeMillis() - timeStart) + "ms");
}
return 0;
}
}
}
@Command(
mixinStandardHelpOptions = true,
subcommands = { Run.class, CommandLine.HelpCommand.class },
version = "openhtmltopdf-cli 1.0.10")
public static class Cli {
}
public static void main( String[] args ) {
int res = new CommandLine(new Cli()).execute(args);
System.exit(res);
}
}