-
Notifications
You must be signed in to change notification settings - Fork 96
/
Wtpdf.java
115 lines (97 loc) · 5.01 KB
/
Wtpdf.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
package com.itextpdf.samples.sandbox.pdfua;
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.attach.ITagWorker;
import com.itextpdf.html2pdf.attach.ProcessorContext;
import com.itextpdf.html2pdf.attach.impl.DefaultTagWorkerFactory;
import com.itextpdf.html2pdf.attach.impl.tags.HTagWorker;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.kernel.xmp.XMPException;
import com.itextpdf.kernel.xmp.XMPMeta;
import com.itextpdf.kernel.xmp.XMPMetaFactory;
import com.itextpdf.layout.IPropertyContainer;
import com.itextpdf.layout.element.Div;
import com.itextpdf.layout.element.IElement;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.pdfa.PdfADocument;
import com.itextpdf.styledxmlparser.node.IElementNode;
import com.itextpdf.test.pdfa.VeraPdfValidator;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Set;
import java.util.HashSet;
public class Wtpdf {
public static final String DEST = "./target/sandbox/pdfua2/pdf_wtpdf.pdf";
public static final String FONT = "./src/main/resources/font/FreeSans.ttf";
private static final String SOURCE_FOLDER = "./src/main/resources/wtpdf/";
private static final Set<String> H_TAGS = new HashSet<>(Arrays.asList("h1", "h2", "h3", "h4", "h5", "h6"));
public static void main(String[] args) throws IOException, XMPException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new Wtpdf().manipulatePdf(DEST);
}
private void manipulatePdf(String dest) throws IOException, XMPException {
PdfOutputIntent outputIntent = new PdfOutputIntent(
"Custom",
"",
"http://www.color.org",
"sRGB IEC61964-2.1",
Files.newInputStream(Paths.get(SOURCE_FOLDER + "sRGB Color Space Profile.icm")));
WriterProperties writerProperties = new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0);
PdfADocument pdfDocument = new PdfADocument(new PdfWriter(dest, writerProperties), PdfAConformance.PDF_A_4, outputIntent);
// The custom tag factory is needed because the PDF2.0 specification prohibts from a p tag being placed inside a Hn tag.
// THis is the current default behaviour for html2pdf but will change in the future.
DefaultTagWorkerFactory factory = new DefaultTagWorkerFactory() {
@Override
public ITagWorker getCustomTagWorker(IElementNode tag, ProcessorContext context) {
if (H_TAGS.contains(tag.name())) {
return new HTagWorker(tag, context) {
@Override
public IPropertyContainer getElementResult() {
IPropertyContainer elementResult = super.getElementResult();
if (elementResult instanceof Div) {
for (IElement child : ((Div) elementResult).getChildren()) {
if (child instanceof Paragraph) {
((Paragraph) child).setNeutralRole();
}
}
}
return elementResult;
}
};
}
return super.getCustomTagWorker(tag, context);
}
};
// setup the general requirements for a wtpdf document
byte[] bytes = Files.readAllBytes(Paths.get(SOURCE_FOLDER + "simplePdfUA2.xmp"));
XMPMeta xmpMeta = XMPMetaFactory.parse(new ByteArrayInputStream(bytes));
pdfDocument.setXmpMetadata(xmpMeta);
pdfDocument.setTagged();
pdfDocument.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true));
pdfDocument.getCatalog().setLang(new PdfString("en-US"));
PdfDocumentInfo info = pdfDocument.getDocumentInfo();
info.setTitle("Well tagged PDF document");
// Use custom font provider as we only want embedded fonts
DefaultFontProvider fontProvider = new DefaultFontProvider(false, false, false);
fontProvider.addFont(SOURCE_FOLDER + "NotoSans-Regular.ttf");
fontProvider.addFont(SOURCE_FOLDER + "NotoEmoji-Regular.ttf");
ConverterProperties converterProperties = new ConverterProperties()
.setBaseUri(SOURCE_FOLDER)
.setTagWorkerFactory(factory)
.setFontProvider(fontProvider);
File file = new File(SOURCE_FOLDER + "article.html");
try (FileInputStream str = new FileInputStream(file)) {
HtmlConverter.convertToPdf(str, pdfDocument, converterProperties);
}
pdfDocument.close();
VeraPdfValidator validator = new VeraPdfValidator();
assert null == validator.validate(DEST);
}
}