1
1
/*
2
2
This file is part of the iText (R) project.
3
- Copyright (c) 1998-2023 Apryse Group NV
3
+ Copyright (c) 1998-2024 Apryse Group NV
4
4
Authors: Apryse Software.
5
5
6
6
This program is offered under a commercial and under the AGPL license.
@@ -22,17 +22,22 @@ This file is part of the iText (R) project.
22
22
*/
23
23
package com .itextpdf .html2pdf ;
24
24
25
+ import com .itextpdf .commons .actions .contexts .IMetaInfo ;
26
+ import com .itextpdf .commons .utils .FileUtil ;
25
27
import com .itextpdf .html2pdf .attach .Attacher ;
26
28
import com .itextpdf .html2pdf .exceptions .Html2PdfException ;
27
- import com .itextpdf .commons .utils .FileUtil ;
28
- import com .itextpdf .commons .actions .contexts .IMetaInfo ;
29
+ import com .itextpdf .html2pdf .resolver .font .DefaultFontProvider ;
29
30
import com .itextpdf .kernel .pdf .DocumentProperties ;
31
+ import com .itextpdf .kernel .pdf .PdfAConformanceLevel ;
30
32
import com .itextpdf .kernel .pdf .PdfDocument ;
33
+ import com .itextpdf .kernel .pdf .PdfVersion ;
31
34
import com .itextpdf .kernel .pdf .PdfWriter ;
35
+ import com .itextpdf .kernel .pdf .WriterProperties ;
32
36
import com .itextpdf .layout .Document ;
33
37
import com .itextpdf .layout .element .IElement ;
34
38
import com .itextpdf .layout .properties .Property ;
35
39
import com .itextpdf .layout .renderer .MetaInfoContainer ;
40
+ import com .itextpdf .pdfa .PdfADocument ;
36
41
import com .itextpdf .styledxmlparser .IXmlParser ;
37
42
import com .itextpdf .styledxmlparser .node .IDocumentNode ;
38
43
import com .itextpdf .styledxmlparser .node .impl .jsoup .JsoupHtmlParser ;
@@ -43,6 +48,8 @@ This file is part of the iText (R) project.
43
48
import java .io .IOException ;
44
49
import java .io .InputStream ;
45
50
import java .io .OutputStream ;
51
+ import java .util .ArrayList ;
52
+ import java .util .Arrays ;
46
53
import java .util .List ;
47
54
48
55
/**
@@ -55,6 +62,8 @@ This file is part of the iText (R) project.
55
62
*/
56
63
public class HtmlConverter {
57
64
65
+ private static final List <PdfAConformanceLevel > pdf2ConformanceLevels = new ArrayList <>(Arrays . asList (PdfAConformanceLevel .PDF_A_4 , PdfAConformanceLevel .PDF_A_4E , PdfAConformanceLevel .PDF_A_4F ));
66
+
58
67
/**
59
68
* Instantiates a new HtmlConverter instance.
60
69
*/
@@ -81,6 +90,10 @@ public static void convertToPdf(String html, OutputStream pdfStream) {
81
90
* @param converterProperties a {@link ConverterProperties} instance
82
91
*/
83
92
public static void convertToPdf (String html , OutputStream pdfStream , ConverterProperties converterProperties ) {
93
+ if (converterProperties != null && pdf2ConformanceLevels .contains (converterProperties .getConformanceLevel ())) {
94
+ convertToPdf (html , new PdfWriter (pdfStream , new WriterProperties ().setPdfVersion (PdfVersion .PDF_2_0 )), converterProperties );
95
+ return ;
96
+ }
84
97
convertToPdf (html , new PdfWriter (pdfStream ), converterProperties );
85
98
}
86
99
@@ -104,8 +117,19 @@ public static void convertToPdf(String html, PdfWriter pdfWriter) {
104
117
* @param converterProperties a {@link ConverterProperties} instance
105
118
*/
106
119
public static void convertToPdf (String html , PdfWriter pdfWriter , ConverterProperties converterProperties ) {
107
- convertToPdf (html , new PdfDocument (pdfWriter , new DocumentProperties ()
108
- .setEventCountingMetaInfo (resolveMetaInfo (converterProperties ))), converterProperties );
120
+ if (converterProperties == null || converterProperties .getConformanceLevel () == null ) {
121
+ convertToPdf (html , new PdfDocument (pdfWriter , new DocumentProperties ()
122
+ .setEventCountingMetaInfo (resolveMetaInfo (converterProperties ))), converterProperties );
123
+ return ;
124
+ }
125
+ PdfDocument document = new PdfADocument (pdfWriter , converterProperties .getConformanceLevel (),
126
+ converterProperties .getDocumentOutputIntent (), new DocumentProperties ()
127
+ .setEventCountingMetaInfo (resolveMetaInfo (converterProperties )));
128
+ converterProperties = setDefaultFontProviderForPdfA (document , converterProperties );
129
+ if ("A" .equals (converterProperties .getConformanceLevel ().getConformance ())) {
130
+ document .setTagged ();
131
+ }
132
+ convertToPdf (html , document , converterProperties );
109
133
}
110
134
111
135
/**
@@ -178,6 +202,10 @@ public static void convertToPdf(InputStream htmlStream, OutputStream pdfStream)
178
202
* @throws IOException Signals that an I/O exception has occurred.
179
203
*/
180
204
public static void convertToPdf (InputStream htmlStream , OutputStream pdfStream , ConverterProperties converterProperties ) throws IOException {
205
+ if (converterProperties != null && pdf2ConformanceLevels .contains (converterProperties .getConformanceLevel ())) {
206
+ convertToPdf (htmlStream , new PdfWriter (pdfStream , new WriterProperties ().setPdfVersion (PdfVersion .PDF_2_0 )), converterProperties );
207
+ return ;
208
+ }
181
209
convertToPdf (htmlStream , new PdfWriter (pdfStream ), converterProperties );
182
210
}
183
211
@@ -217,8 +245,19 @@ public static void convertToPdf(InputStream htmlStream, PdfWriter pdfWriter) thr
217
245
* @throws IOException Signals that an I/O exception has occurred.
218
246
*/
219
247
public static void convertToPdf (InputStream htmlStream , PdfWriter pdfWriter , ConverterProperties converterProperties ) throws IOException {
220
- convertToPdf (htmlStream , new PdfDocument (pdfWriter , new DocumentProperties ().setEventCountingMetaInfo (
221
- resolveMetaInfo (converterProperties ))), converterProperties );
248
+ if (converterProperties == null || converterProperties .getConformanceLevel () == null ) {
249
+ convertToPdf (htmlStream , new PdfDocument (pdfWriter , new DocumentProperties ().setEventCountingMetaInfo (
250
+ resolveMetaInfo (converterProperties ))), converterProperties );
251
+ return ;
252
+ }
253
+ PdfDocument document = new PdfADocument (pdfWriter , converterProperties .getConformanceLevel (),
254
+ converterProperties .getDocumentOutputIntent (), new DocumentProperties ()
255
+ .setEventCountingMetaInfo (resolveMetaInfo (converterProperties )));
256
+ converterProperties = setDefaultFontProviderForPdfA (document , converterProperties );
257
+ if ("A" .equals (converterProperties .getConformanceLevel ().getConformance ())) {
258
+ document .setTagged ();
259
+ }
260
+ convertToPdf (htmlStream , document , converterProperties );
222
261
}
223
262
224
263
/**
@@ -231,6 +270,7 @@ public static void convertToPdf(InputStream htmlStream, PdfWriter pdfWriter, Con
231
270
* @throws IOException Signals that an I/O exception has occurred.
232
271
*/
233
272
public static void convertToPdf (InputStream htmlStream , PdfDocument pdfDocument , ConverterProperties converterProperties ) throws IOException {
273
+ converterProperties = setDefaultFontProviderForPdfA (pdfDocument , converterProperties );
234
274
final Document document = convertToDocument (htmlStream , pdfDocument , converterProperties );
235
275
IMetaInfo metaInfo = resolveMetaInfo (converterProperties );
236
276
document .setProperty (Property .META_INFO , new MetaInfoContainer (metaInfo ));
@@ -305,6 +345,7 @@ public static Document convertToDocument(String html, PdfDocument pdfDocument, C
305
345
if (pdfDocument .getReader () != null ) {
306
346
throw new Html2PdfException (Html2PdfException .PDF_DOCUMENT_SHOULD_BE_IN_WRITING_MODE );
307
347
}
348
+ converterProperties = setDefaultFontProviderForPdfA (pdfDocument , converterProperties );
308
349
IXmlParser parser = new JsoupHtmlParser ();
309
350
IDocumentNode doc = parser .parse (html );
310
351
return Attacher .attach (doc , pdfDocument , converterProperties );
@@ -325,6 +366,7 @@ public static Document convertToDocument(InputStream htmlStream, PdfDocument pdf
325
366
if (pdfDocument .getReader () != null ) {
326
367
throw new Html2PdfException (Html2PdfException .PDF_DOCUMENT_SHOULD_BE_IN_WRITING_MODE );
327
368
}
369
+ converterProperties = setDefaultFontProviderForPdfA (pdfDocument , converterProperties );
328
370
IXmlParser parser = new JsoupHtmlParser ();
329
371
IDocumentNode doc = parser .parse (htmlStream , converterProperties != null ? converterProperties .getCharset () : null );
330
372
return Attacher .attach (doc , pdfDocument , converterProperties );
@@ -363,6 +405,7 @@ public static List<IElement> convertToElements(InputStream htmlStream) throws IO
363
405
* @return a list of iText building blocks
364
406
*/
365
407
public static List <IElement > convertToElements (String html , ConverterProperties converterProperties ) {
408
+ converterProperties = setDefaultFontProviderForPdfA (null , converterProperties );
366
409
IXmlParser parser = new JsoupHtmlParser ();
367
410
IDocumentNode doc = parser .parse (html );
368
411
return Attacher .attach (doc , converterProperties );
@@ -379,6 +422,7 @@ public static List<IElement> convertToElements(String html, ConverterProperties
379
422
* @throws IOException Signals that an I/O exception has occurred.
380
423
*/
381
424
public static List <IElement > convertToElements (InputStream htmlStream , ConverterProperties converterProperties ) throws IOException {
425
+ converterProperties = setDefaultFontProviderForPdfA (null , converterProperties );
382
426
IXmlParser parser = new JsoupHtmlParser ();
383
427
IDocumentNode doc = parser .parse (htmlStream , converterProperties != null ? converterProperties .getCharset () : null );
384
428
return Attacher .attach (doc , converterProperties );
@@ -394,6 +438,22 @@ private static IMetaInfo resolveMetaInfo(ConverterProperties converterProperties
394
438
: converterProperties .getEventMetaInfo ();
395
439
}
396
440
441
+ private static ConverterProperties setDefaultFontProviderForPdfA (PdfDocument document , ConverterProperties properties ) {
442
+ if (document instanceof PdfADocument ) {
443
+ if (properties == null ) {
444
+ properties = new ConverterProperties ();
445
+ }
446
+ if (properties .getFontProvider () == null ) {
447
+ properties .setFontProvider (new DefaultFontProvider (false , true , false ));
448
+ }
449
+ } else if (document == null && properties != null && properties .getConformanceLevel () != null ) {
450
+ if (properties .getFontProvider () == null ) {
451
+ properties .setFontProvider (new DefaultFontProvider (false , true , false ));
452
+ }
453
+ }
454
+ return properties ;
455
+ }
456
+
397
457
private static class HtmlMetaInfo implements IMetaInfo {
398
458
}
399
459
}
0 commit comments