-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathPdfConverter.java
165 lines (152 loc) · 6.75 KB
/
PdfConverter.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
package com.vladsch.flexmark.java.samples;
import com.vladsch.flexmark.ext.toc.TocExtension;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.pdf.converter.PdfConverterExtension;
import com.vladsch.flexmark.profile.pegdown.Extensions;
import com.vladsch.flexmark.profile.pegdown.PegdownOptionsAdapter;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.data.DataHolder;
import org.apache.commons.io.IOUtils;
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
public class PdfConverter {
static final DataHolder OPTIONS = PegdownOptionsAdapter.flexmarkOptions(
Extensions.ALL & ~(Extensions.ANCHORLINKS | Extensions.EXTANCHORLINKS_WRAP)
, TocExtension.create()).toMutable()
.set(TocExtension.LIST_CLASS, PdfConverterExtension.DEFAULT_TOC_LIST_CLASS)
.toImmutable();
static String getResourceFileContent(String resourcePath) {
StringWriter writer = new StringWriter();
getResourceFileContent(writer, resourcePath);
return writer.toString();
}
private static void getResourceFileContent(StringWriter writer, String resourcePath) {
InputStream inputStream = PdfConverter.class.getResourceAsStream(resourcePath);
try {
IOUtils.copy(inputStream, writer, "UTF-8");
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String pegdown = "" +
"[TOC]\n" +
"\n" +
"#Heading\n" +
"-----\n" +
"paragraph text \n" +
"lazy continuation\n" +
"\n" +
"* list item\n" +
" > block quote\n" +
" lazy continuation\n" +
"\n" +
"\n## Heading 2" +
"\n" +
"~~~info\n" +
" with uneven indent\n" +
" with uneven indent\n" +
"indented code\n" +
"~~~ \n" +
"\n" +
"\n## Heading 3" +
"\n" +
" with uneven indent\n" +
" with uneven indent\n" +
" indented code\n" +
"1. numbered item 1 \n" +
"1. numbered item 2 \n" +
"1. numbered item 3 \n" +
" - bullet item 1 \n" +
" - bullet item 2 \n" +
" - bullet item 3 \n" +
" 1. numbered sub-item 1 \n" +
" 1. numbered sub-item 2 \n" +
" 1. numbered sub-item 3 \n" +
" \n" +
" ~~~info\n" +
" with uneven indent\n" +
" with uneven indent\n" +
" indented code\n" +
" ~~~ \n" +
" \n" +
" with uneven indent\n" +
" with uneven indent\n" +
" indented code\n" +
"";
System.out.println("pegdown\n");
System.out.println(pegdown);
Parser PARSER = Parser.builder(OPTIONS).build();
HtmlRenderer RENDERER = HtmlRenderer.builder(OPTIONS).build();
Node document = PARSER.parse(pegdown);
String html = RENDERER.render(document);
// add embedded fonts for non-latin character set rendering
// change file:///usr/local/fonts/ to your system's path for font installation Unix path or
// on windows the path should start with `file:/X:/...` where `X:/...` is the drive
// letter followed by the full installation path.
//
// Google Noto fonts can be downloaded from https://www.google.com/get/noto/
// `arialuni.ttf` from https://www.wfonts.com/font/arial-unicode-ms
String nonLatinFonts = "" +
"<style>\n" +
"@font-face {\n" +
" font-family: 'noto-cjk';\n" +
" src: url('file:///usr/local/fonts/arialuni.ttf');\n" +
" font-weight: normal;\n" +
" font-style: normal;\n" +
"}\n" +
"\n" +
"@font-face {\n" +
" font-family: 'noto-serif';\n" +
" src: url('file:///usr/local/fonts/NotoSerif-Regular.ttf');\n" +
" font-weight: normal;\n" +
" font-style: normal;\n" +
"}\n" +
"\n" +
"@font-face {\n" +
" font-family: 'noto-sans';\n" +
" src: url('file:///usr/local/fonts/NotoSans-Regular.ttf');\n" +
" font-weight: normal;\n" +
" font-style: normal;\n" +
"}\n" +
"\n" +
"@font-face {\n" +
" font-family: 'noto-mono';\n" +
" src: url('file:///usr/local/fonts/NotoMono-Regular.ttf');\n" +
" font-weight: normal;\n" +
" font-style: normal;\n" +
"}\n" +
"\n" +
"body {\n" +
" font-family: 'noto-sans', 'noto-cjk', sans-serif;\n" +
" overflow: hidden;\n" +
" word-wrap: break-word;\n" +
" font-size: 14px;\n" +
"}\n" +
"\n" +
"var,\n" +
"code,\n" +
"kbd,\n" +
"pre {\n" +
" font: 0.9em 'noto-mono', Consolas, \"Liberation Mono\", Menlo, Courier, monospace;\n" +
"}\n" +
"</style>\n" +
"";
html = "<!DOCTYPE html><html><head><meta charset=\"UTF-8\">\n" +
"" + // add your stylesheets, scripts styles etc.
// uncomment line below for adding style for custom embedded fonts
// nonLatinFonts +
"</head><body>" + html + "\n" +
"</body></html>";
PdfConverterExtension.exportToPdf("/Users/vlad/src/pdf/flexmark-java.pdf", html, "", OPTIONS);
// add PDF protection policy
OPTIONS.toMutable().set(PdfConverterExtension.PROTECTION_POLICY, new StandardProtectionPolicy("opassword", "upassword", new AccessPermission()));
PdfConverterExtension.exportToPdf("/Users/vlad/src/pdf/flexmark-java-pwd-protected.pdf", html, "", OPTIONS);
System.out.println("Output PDF to /Users/vlad/src/pdf/flexmark-java-pwd-protected.pdf");
}
}