11/*
22 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
44 Authors: Apryse Software.
55
66 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.
2222 */
2323package com .itextpdf .html2pdf ;
2424
25+ import com .itextpdf .commons .actions .contexts .IMetaInfo ;
26+ import com .itextpdf .commons .utils .FileUtil ;
2527import com .itextpdf .html2pdf .attach .Attacher ;
2628import 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 ;
2930import com .itextpdf .kernel .pdf .DocumentProperties ;
31+ import com .itextpdf .kernel .pdf .PdfAConformanceLevel ;
3032import com .itextpdf .kernel .pdf .PdfDocument ;
33+ import com .itextpdf .kernel .pdf .PdfVersion ;
3134import com .itextpdf .kernel .pdf .PdfWriter ;
35+ import com .itextpdf .kernel .pdf .WriterProperties ;
3236import com .itextpdf .layout .Document ;
3337import com .itextpdf .layout .element .IElement ;
3438import com .itextpdf .layout .properties .Property ;
3539import com .itextpdf .layout .renderer .MetaInfoContainer ;
40+ import com .itextpdf .pdfa .PdfADocument ;
3641import com .itextpdf .styledxmlparser .IXmlParser ;
3742import com .itextpdf .styledxmlparser .node .IDocumentNode ;
3843import com .itextpdf .styledxmlparser .node .impl .jsoup .JsoupHtmlParser ;
@@ -43,6 +48,8 @@ This file is part of the iText (R) project.
4348import java .io .IOException ;
4449import java .io .InputStream ;
4550import java .io .OutputStream ;
51+ import java .util .ArrayList ;
52+ import java .util .Arrays ;
4653import java .util .List ;
4754
4855/**
@@ -55,6 +62,8 @@ This file is part of the iText (R) project.
5562 */
5663public class HtmlConverter {
5764
65+ private static final List <PdfAConformanceLevel > pdf2ConformanceLevels = new ArrayList <>(Arrays . asList (PdfAConformanceLevel .PDF_A_4 , PdfAConformanceLevel .PDF_A_4E , PdfAConformanceLevel .PDF_A_4F ));
66+
5867 /**
5968 * Instantiates a new HtmlConverter instance.
6069 */
@@ -81,6 +90,10 @@ public static void convertToPdf(String html, OutputStream pdfStream) {
8190 * @param converterProperties a {@link ConverterProperties} instance
8291 */
8392 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+ }
8497 convertToPdf (html , new PdfWriter (pdfStream ), converterProperties );
8598 }
8699
@@ -104,8 +117,19 @@ public static void convertToPdf(String html, PdfWriter pdfWriter) {
104117 * @param converterProperties a {@link ConverterProperties} instance
105118 */
106119 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 );
109133 }
110134
111135 /**
@@ -178,6 +202,10 @@ public static void convertToPdf(InputStream htmlStream, OutputStream pdfStream)
178202 * @throws IOException Signals that an I/O exception has occurred.
179203 */
180204 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+ }
181209 convertToPdf (htmlStream , new PdfWriter (pdfStream ), converterProperties );
182210 }
183211
@@ -217,8 +245,19 @@ public static void convertToPdf(InputStream htmlStream, PdfWriter pdfWriter) thr
217245 * @throws IOException Signals that an I/O exception has occurred.
218246 */
219247 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 );
222261 }
223262
224263 /**
@@ -231,6 +270,7 @@ public static void convertToPdf(InputStream htmlStream, PdfWriter pdfWriter, Con
231270 * @throws IOException Signals that an I/O exception has occurred.
232271 */
233272 public static void convertToPdf (InputStream htmlStream , PdfDocument pdfDocument , ConverterProperties converterProperties ) throws IOException {
273+ converterProperties = setDefaultFontProviderForPdfA (pdfDocument , converterProperties );
234274 final Document document = convertToDocument (htmlStream , pdfDocument , converterProperties );
235275 IMetaInfo metaInfo = resolveMetaInfo (converterProperties );
236276 document .setProperty (Property .META_INFO , new MetaInfoContainer (metaInfo ));
@@ -305,6 +345,7 @@ public static Document convertToDocument(String html, PdfDocument pdfDocument, C
305345 if (pdfDocument .getReader () != null ) {
306346 throw new Html2PdfException (Html2PdfException .PDF_DOCUMENT_SHOULD_BE_IN_WRITING_MODE );
307347 }
348+ converterProperties = setDefaultFontProviderForPdfA (pdfDocument , converterProperties );
308349 IXmlParser parser = new JsoupHtmlParser ();
309350 IDocumentNode doc = parser .parse (html );
310351 return Attacher .attach (doc , pdfDocument , converterProperties );
@@ -325,6 +366,7 @@ public static Document convertToDocument(InputStream htmlStream, PdfDocument pdf
325366 if (pdfDocument .getReader () != null ) {
326367 throw new Html2PdfException (Html2PdfException .PDF_DOCUMENT_SHOULD_BE_IN_WRITING_MODE );
327368 }
369+ converterProperties = setDefaultFontProviderForPdfA (pdfDocument , converterProperties );
328370 IXmlParser parser = new JsoupHtmlParser ();
329371 IDocumentNode doc = parser .parse (htmlStream , converterProperties != null ? converterProperties .getCharset () : null );
330372 return Attacher .attach (doc , pdfDocument , converterProperties );
@@ -363,6 +405,7 @@ public static List<IElement> convertToElements(InputStream htmlStream) throws IO
363405 * @return a list of iText building blocks
364406 */
365407 public static List <IElement > convertToElements (String html , ConverterProperties converterProperties ) {
408+ converterProperties = setDefaultFontProviderForPdfA (null , converterProperties );
366409 IXmlParser parser = new JsoupHtmlParser ();
367410 IDocumentNode doc = parser .parse (html );
368411 return Attacher .attach (doc , converterProperties );
@@ -379,6 +422,7 @@ public static List<IElement> convertToElements(String html, ConverterProperties
379422 * @throws IOException Signals that an I/O exception has occurred.
380423 */
381424 public static List <IElement > convertToElements (InputStream htmlStream , ConverterProperties converterProperties ) throws IOException {
425+ converterProperties = setDefaultFontProviderForPdfA (null , converterProperties );
382426 IXmlParser parser = new JsoupHtmlParser ();
383427 IDocumentNode doc = parser .parse (htmlStream , converterProperties != null ? converterProperties .getCharset () : null );
384428 return Attacher .attach (doc , converterProperties );
@@ -394,6 +438,22 @@ private static IMetaInfo resolveMetaInfo(ConverterProperties converterProperties
394438 : converterProperties .getEventMetaInfo ();
395439 }
396440
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+
397457 private static class HtmlMetaInfo implements IMetaInfo {
398458 }
399459}
0 commit comments