@@ -84,7 +84,6 @@ public class HtmlConverter {
84
84
private HtmlConverter () {
85
85
}
86
86
87
- // TODO add overloads with Charset provided
88
87
// TODO add overloads without automatic elements flushing
89
88
90
89
/**
@@ -134,8 +133,21 @@ public static void convertToPdf(String html, PdfWriter pdfWriter) throws IOExcep
134
133
* @throws IOException Signals that an I/O exception has occurred.
135
134
*/
136
135
public static void convertToPdf (String html , PdfWriter pdfWriter , ConverterProperties converterProperties ) throws IOException {
137
- InputStream stream = new ByteArrayInputStream (html .getBytes ());
138
- convertToPdf (stream , pdfWriter , converterProperties );
136
+ convertToPdf (html , new PdfDocument (pdfWriter ), converterProperties );
137
+ }
138
+
139
+ /**
140
+ * Converts HTML obtained from an {@link InputStream} to objects that
141
+ * will be added to a {@link PdfDocument}, using specific {@link ConverterProperties}.
142
+ *
143
+ * @param html the html in the form of a {@link String}
144
+ * @param pdfDocument the {@link PdfDocument} instance
145
+ * @param converterProperties a {@link ConverterProperties} instance
146
+ * @throws IOException Signals that an I/O exception has occurred.
147
+ */
148
+ public static void convertToPdf (String html , PdfDocument pdfDocument , ConverterProperties converterProperties ) throws IOException {
149
+ Document document = convertToDocument (html , pdfDocument , converterProperties );
150
+ document .close ();
139
151
}
140
152
141
153
/**
@@ -247,6 +259,19 @@ public static void convertToPdf(InputStream htmlStream, PdfDocument pdfDocument,
247
259
document .close ();
248
260
}
249
261
262
+ /**
263
+ * Converts HTML obtained from an {@link InputStream} to content that
264
+ * will be written to a {@link PdfWriter}, returning a {@link Document} instance.
265
+ *
266
+ * @param html the html in the form of a {@link String}
267
+ * @param pdfWriter the {@link PdfWriter} containing the resulting PDF
268
+ * @return a {@link Document} instance
269
+ * @throws IOException Signals that an I/O exception has occurred.
270
+ */
271
+ public static Document convertToDocument (String html , PdfWriter pdfWriter ) throws IOException {
272
+ return convertToDocument (html , pdfWriter , null );
273
+ }
274
+
250
275
/**
251
276
* Converts HTML obtained from an {@link InputStream} to content that
252
277
* will be written to a {@link PdfWriter}, returning a {@link Document} instance.
@@ -260,6 +285,21 @@ public static Document convertToDocument(InputStream htmlStream, PdfWriter pdfWr
260
285
return convertToDocument (htmlStream , pdfWriter , null );
261
286
}
262
287
288
+ /**
289
+ * Converts HTML obtained from an {@link InputStream} to content that
290
+ * will be written to a {@link PdfWriter}, using specific
291
+ * {@link ConverterProperties}, returning a {@link Document} instance.
292
+ *
293
+ * @param html the html in the form of a {@link String}
294
+ * @param pdfWriter the pdf writer
295
+ * @param converterProperties a {@link ConverterProperties} instance
296
+ * @return a {@link Document} instance
297
+ * @throws IOException Signals that an I/O exception has occurred.
298
+ */
299
+ public static Document convertToDocument (String html , PdfWriter pdfWriter , ConverterProperties converterProperties ) throws IOException {
300
+ return convertToDocument (html , new PdfDocument (pdfWriter ), converterProperties );
301
+ }
302
+
263
303
/**
264
304
* Converts HTML obtained from an {@link InputStream} to content that
265
305
* will be written to a {@link PdfWriter}, using specific
@@ -275,6 +315,62 @@ public static Document convertToDocument(InputStream htmlStream, PdfWriter pdfWr
275
315
return convertToDocument (htmlStream , new PdfDocument (pdfWriter ), converterProperties );
276
316
}
277
317
318
+ /**
319
+ * Converts HTML obtained from an {@link InputStream} to objects that
320
+ * will be added to a {@link PdfDocument}, using specific {@link ConverterProperties},
321
+ * returning a {@link Document} instance.
322
+ *
323
+ * @param html the html in the form of a {@link String}
324
+ * @param pdfDocument the {@link PdfDocument} instance
325
+ * @param converterProperties a {@link ConverterProperties} instance
326
+ * @return a {@link Document} instance
327
+ * @throws IOException Signals that an I/O exception has occurred.
328
+ */
329
+ public static Document convertToDocument (String html , PdfDocument pdfDocument , ConverterProperties converterProperties ) throws IOException {
330
+ String licenseKeyClassName = "com.itextpdf.licensekey.LicenseKey" ;
331
+ String licenseKeyProductClassName = "com.itextpdf.licensekey.LicenseKeyProduct" ;
332
+ String licenseKeyFeatureClassName = "com.itextpdf.licensekey.LicenseKeyProductFeature" ;
333
+ String checkLicenseKeyMethodName = "scheduledCheck" ;
334
+
335
+ try {
336
+ Class licenseKeyClass = Class .forName (licenseKeyClassName );
337
+ Class licenseKeyProductClass = Class .forName (licenseKeyProductClassName );
338
+ Class licenseKeyProductFeatureClass = Class .forName (licenseKeyFeatureClassName );
339
+
340
+ Object licenseKeyProductFeatureArray = Array .newInstance (licenseKeyProductFeatureClass , 0 );
341
+
342
+ Class [] params = new Class [] {
343
+ String .class ,
344
+ Integer .TYPE ,
345
+ Integer .TYPE ,
346
+ licenseKeyProductFeatureArray .getClass ()
347
+ };
348
+
349
+ Constructor licenseKeyProductConstructor = licenseKeyProductClass .getConstructor (params );
350
+
351
+ Object licenseKeyProductObject = licenseKeyProductConstructor .newInstance (
352
+ Html2PdfProductInfo .PRODUCT_NAME ,
353
+ Html2PdfProductInfo .MAJOR_VERSION ,
354
+ Html2PdfProductInfo .MINOR_VERSION ,
355
+ licenseKeyProductFeatureArray
356
+ );
357
+
358
+ Method method = licenseKeyClass .getMethod (checkLicenseKeyMethodName , licenseKeyProductClass );
359
+ method .invoke (null , licenseKeyProductObject );
360
+ } catch (Exception e ) {
361
+ if ( ! Version .isAGPLVersion () ) {
362
+ throw new RuntimeException (e .getCause ());
363
+ }
364
+ }
365
+
366
+ if (pdfDocument .getReader () != null ) {
367
+ throw new Html2PdfException (Html2PdfException .PdfDocumentShouldBeInWritingMode );
368
+ }
369
+ IHtmlParser parser = new JsoupHtmlParser ();
370
+ IDocumentNode doc = parser .parse (html );
371
+ return Attacher .attach (doc , pdfDocument , converterProperties );
372
+ }
373
+
278
374
/**
279
375
* Converts HTML obtained from an {@link InputStream} to objects that
280
376
* will be added to a {@link PdfDocument}, using specific {@link ConverterProperties},
@@ -327,8 +423,7 @@ public static Document convertToDocument(InputStream htmlStream, PdfDocument pdf
327
423
throw new Html2PdfException (Html2PdfException .PdfDocumentShouldBeInWritingMode );
328
424
}
329
425
IHtmlParser parser = new JsoupHtmlParser ();
330
- String detectedCharset = detectEncoding (htmlStream );
331
- IDocumentNode doc = parser .parse (htmlStream , detectedCharset );
426
+ IDocumentNode doc = parser .parse (htmlStream , converterProperties != null ? converterProperties .getCharset () : null );
332
427
return Attacher .attach (doc , pdfDocument , converterProperties );
333
428
}
334
429
@@ -341,8 +436,7 @@ public static Document convertToDocument(InputStream htmlStream, PdfDocument pdf
341
436
* @throws IOException Signals that an I/O exception has occurred.
342
437
*/
343
438
public static List <IElement > convertToElements (String html ) throws IOException {
344
- InputStream stream = new ByteArrayInputStream (html .getBytes ());
345
- return convertToElements (stream , null );
439
+ return convertToElements (html , null );
346
440
}
347
441
348
442
/**
@@ -368,8 +462,45 @@ public static List<IElement> convertToElements(InputStream htmlStream) throws IO
368
462
* @throws IOException Signals that an I/O exception has occurred.
369
463
*/
370
464
public static List <IElement > convertToElements (String html , ConverterProperties converterProperties ) throws IOException {
371
- ByteArrayInputStream stream = new ByteArrayInputStream (html .getBytes ());
372
- return convertToElements (stream , converterProperties );
465
+ String licenseKeyClassName = "com.itextpdf.licensekey.LicenseKey" ;
466
+ String licenseKeyProductClassName = "com.itextpdf.licensekey.LicenseKeyProduct" ;
467
+ String licenseKeyFeatureClassName = "com.itextpdf.licensekey.LicenseKeyProductFeature" ;
468
+ String checkLicenseKeyMethodName = "scheduledCheck" ;
469
+
470
+ try {
471
+ Class licenseKeyClass = Class .forName (licenseKeyClassName );
472
+ Class licenseKeyProductClass = Class .forName (licenseKeyProductClassName );
473
+ Class licenseKeyProductFeatureClass = Class .forName (licenseKeyFeatureClassName );
474
+
475
+ Object licenseKeyProductFeatureArray = Array .newInstance (licenseKeyProductFeatureClass , 0 );
476
+
477
+ Class [] params = new Class [] {
478
+ String .class ,
479
+ Integer .TYPE ,
480
+ Integer .TYPE ,
481
+ licenseKeyProductFeatureArray .getClass ()
482
+ };
483
+
484
+ Constructor licenseKeyProductConstructor = licenseKeyProductClass .getConstructor (params );
485
+
486
+ Object licenseKeyProductObject = licenseKeyProductConstructor .newInstance (
487
+ Html2PdfProductInfo .PRODUCT_NAME ,
488
+ Html2PdfProductInfo .MAJOR_VERSION ,
489
+ Html2PdfProductInfo .MINOR_VERSION ,
490
+ licenseKeyProductFeatureArray
491
+ );
492
+
493
+ Method method = licenseKeyClass .getMethod (checkLicenseKeyMethodName , licenseKeyProductClass );
494
+ method .invoke (null , licenseKeyProductObject );
495
+ } catch (Exception e ) {
496
+ if ( ! Version .isAGPLVersion () ) {
497
+ throw new RuntimeException (e .getCause ());
498
+ }
499
+ }
500
+
501
+ IHtmlParser parser = new JsoupHtmlParser ();
502
+ IDocumentNode doc = parser .parse (html );
503
+ return Attacher .attach (doc , converterProperties );
373
504
}
374
505
375
506
/**
@@ -420,19 +551,7 @@ public static List<IElement> convertToElements(InputStream htmlStream, Converter
420
551
}
421
552
422
553
IHtmlParser parser = new JsoupHtmlParser ();
423
- IDocumentNode doc = parser .parse (htmlStream , detectEncoding ( htmlStream ) );
554
+ IDocumentNode doc = parser .parse (htmlStream , converterProperties != null ? converterProperties . getCharset () : null );
424
555
return Attacher .attach (doc , converterProperties );
425
556
}
426
-
427
- /**
428
- * Detects encoding of a specific {@link InputStream}.
429
- *
430
- * @param in the {@link InputStream}
431
- * @return the encoding; currently always returns "UTF-8".
432
- */
433
- // TODO
434
- private static String detectEncoding (final InputStream in ) {
435
- return "UTF-8" ;
436
- }
437
-
438
557
}
0 commit comments