Skip to content

Commit a1e49be

Browse files
committed
Merge branch 'release/1.0.2'
2 parents 8b8bef4 + c4a4bf4 commit a1e49be

File tree

422 files changed

+6704
-10937
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

422 files changed

+6704
-10937
lines changed

pom.xml

+56-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
<parent>
88
<groupId>com.itextpdf</groupId>
99
<artifactId>root</artifactId>
10-
<version>7.0.4</version>
10+
<version>7.0.5</version>
1111
<relativePath/>
1212
</parent>
1313

1414
<artifactId>html2pdf</artifactId>
15-
<version>1.0.1</version>
15+
<version>1.0.2</version>
1616

1717
<name>pdfHTML</name>
1818
<description>pdfHTML is an iText 7 add-on that lets you to parse (X)HTML snippets and the associated CSS and converts
@@ -144,6 +144,60 @@
144144
</execution>
145145
</executions>
146146
</plugin>
147+
<plugin>
148+
<groupId>org.apache.felix</groupId>
149+
<artifactId>maven-bundle-plugin</artifactId>
150+
<version>3.2.0</version>
151+
<extensions>true</extensions>
152+
<executions>
153+
<execution>
154+
<id>bundle-manifest</id>
155+
<phase>process-classes</phase>
156+
<goals>
157+
<goal>manifest</goal>
158+
</goals>
159+
</execution>
160+
</executions>
161+
<configuration>
162+
<unpackBundle>true</unpackBundle>
163+
</configuration>
164+
</plugin>
165+
<plugin>
166+
<groupId>com.github.ekryd.sortpom</groupId>
167+
<artifactId>sortpom-maven-plugin</artifactId>
168+
<version>2.4.0</version>
169+
<executions>
170+
<execution>
171+
<phase>verify</phase>
172+
<goals>
173+
<goal>sort</goal>
174+
</goals>
175+
</execution>
176+
</executions>
177+
<configuration>
178+
<lineSeparator>\n</lineSeparator>
179+
<encoding>${project.build.sourceEncoding}</encoding>
180+
<sortProperties>true</sortProperties>
181+
<keepBlankLines>true</keepBlankLines>
182+
<expandEmptyElements>false</expandEmptyElements>
183+
<nrOfIndentSpace>2</nrOfIndentSpace>
184+
<sortDependencies>scope</sortDependencies>
185+
<skip>${skipSortPom}</skip>
186+
</configuration>
187+
</plugin>
188+
<plugin>
189+
<groupId>org.codehaus.mojo</groupId>
190+
<artifactId>tidy-maven-plugin</artifactId>
191+
<version>1.0.0</version>
192+
<executions>
193+
<execution>
194+
<phase>verify</phase>
195+
<goals>
196+
<goal>pom</goal>
197+
</goals>
198+
</execution>
199+
</executions>
200+
</plugin>
147201
</plugins>
148202
<resources>
149203
<resource>

src/main/java/com/itextpdf/html2pdf/ConverterProperties.java

+55-2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ public class ConverterProperties {
7575
/** Indicates whether an AcroForm should be created. */
7676
private boolean createAcroForm = false;
7777

78+
/** Character set used in conversion of input streams */
79+
private String charset;
80+
81+
/** Indicates whether the document should be opened in immediate flush or not **/
82+
private boolean immediateFlush = true;
83+
7884
/**
7985
* Instantiates a new {@link ConverterProperties} instance.
8086
*/
@@ -95,6 +101,7 @@ public ConverterProperties(ConverterProperties other) {
95101
this.baseUri = other.baseUri;
96102
this.createAcroForm = other.createAcroForm;
97103
this.outlineHandler = other.outlineHandler;
104+
this.charset = other.charset;
98105
}
99106

100107
/**
@@ -127,7 +134,9 @@ public FontProvider getFontProvider() {
127134
}
128135

129136
/**
130-
* Sets the font provider.
137+
* Sets the font provider. Please note that {@link FontProvider} instances cannot be reused across several documents
138+
* and thus as soon as you set this property, this {@link ConverterProperties} instance becomes only useful for a single
139+
* HTML conversion.
131140
*
132141
* @param fontProvider the font provider
133142
* @return the ConverterProperties instance
@@ -227,7 +236,9 @@ public OutlineHandler getOutlineHandler() {
227236
}
228237

229238
/**
230-
* Sets the outline handler.
239+
* Sets the outline handler. Please note that {@link OutlineHandler} is not thread safe, thus
240+
* as soon as you have set this property, this {@link ConverterProperties} instance cannot be used in converting multiple
241+
* HTMLs simultaneously.
231242
*
232243
* @param outlineHandler the outline handler
233244
* @return the ConverterProperties instance
@@ -236,4 +247,46 @@ public ConverterProperties setOutlineHandler(OutlineHandler outlineHandler) {
236247
this.outlineHandler = outlineHandler;
237248
return this;
238249
}
250+
251+
/**
252+
* Gets the encoding charset.
253+
*
254+
* @return the charset
255+
*/
256+
public String getCharset() {
257+
return charset;
258+
}
259+
260+
/**
261+
* Sets the encoding charset.
262+
*
263+
* @param charset the charset
264+
* @return the ConverterProperties instance
265+
*/
266+
public ConverterProperties setCharset(String charset) {
267+
this.charset = charset;
268+
return this;
269+
}
270+
271+
/**
272+
* Checks if immediateFlush is set
273+
* @return true if immediateFlush is set, false if not.
274+
*/
275+
public boolean isImmediateFlush(){
276+
return immediateFlush;
277+
}
278+
279+
/**
280+
* set the immediate flush property of the layout document
281+
* This is used for convertToDocument methods and will be overwritten to
282+
* false if a page-counter declaration is present in the CSS of the HTML being
283+
* converted.
284+
* Has no effect when used in conjunction with convertToPdf or convertToElements
285+
* @param immediateFlush the immediate flush value
286+
* @return the ConverterProperties
287+
*/
288+
public ConverterProperties setImmediateFlush(boolean immediateFlush){
289+
this.immediateFlush = immediateFlush;
290+
return this;
291+
}
239292
}

src/main/java/com/itextpdf/html2pdf/HtmlConverter.java

+141-22
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ public class HtmlConverter {
8484
private HtmlConverter() {
8585
}
8686

87-
// TODO add overloads with Charset provided
8887
// TODO add overloads without automatic elements flushing
8988

9089
/**
@@ -134,8 +133,21 @@ public static void convertToPdf(String html, PdfWriter pdfWriter) throws IOExcep
134133
* @throws IOException Signals that an I/O exception has occurred.
135134
*/
136135
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();
139151
}
140152

141153
/**
@@ -247,6 +259,19 @@ public static void convertToPdf(InputStream htmlStream, PdfDocument pdfDocument,
247259
document.close();
248260
}
249261

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+
250275
/**
251276
* Converts HTML obtained from an {@link InputStream} to content that
252277
* will be written to a {@link PdfWriter}, returning a {@link Document} instance.
@@ -260,6 +285,21 @@ public static Document convertToDocument(InputStream htmlStream, PdfWriter pdfWr
260285
return convertToDocument(htmlStream, pdfWriter, null);
261286
}
262287

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+
263303
/**
264304
* Converts HTML obtained from an {@link InputStream} to content that
265305
* will be written to a {@link PdfWriter}, using specific
@@ -275,6 +315,62 @@ public static Document convertToDocument(InputStream htmlStream, PdfWriter pdfWr
275315
return convertToDocument(htmlStream, new PdfDocument(pdfWriter), converterProperties);
276316
}
277317

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+
278374
/**
279375
* Converts HTML obtained from an {@link InputStream} to objects that
280376
* will be added to a {@link PdfDocument}, using specific {@link ConverterProperties},
@@ -327,8 +423,7 @@ public static Document convertToDocument(InputStream htmlStream, PdfDocument pdf
327423
throw new Html2PdfException(Html2PdfException.PdfDocumentShouldBeInWritingMode);
328424
}
329425
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);
332427
return Attacher.attach(doc, pdfDocument, converterProperties);
333428
}
334429

@@ -341,8 +436,7 @@ public static Document convertToDocument(InputStream htmlStream, PdfDocument pdf
341436
* @throws IOException Signals that an I/O exception has occurred.
342437
*/
343438
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);
346440
}
347441

348442
/**
@@ -368,8 +462,45 @@ public static List<IElement> convertToElements(InputStream htmlStream) throws IO
368462
* @throws IOException Signals that an I/O exception has occurred.
369463
*/
370464
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);
373504
}
374505

375506
/**
@@ -420,19 +551,7 @@ public static List<IElement> convertToElements(InputStream htmlStream, Converter
420551
}
421552

422553
IHtmlParser parser = new JsoupHtmlParser();
423-
IDocumentNode doc = parser.parse(htmlStream, detectEncoding(htmlStream));
554+
IDocumentNode doc = parser.parse(htmlStream, converterProperties != null ? converterProperties.getCharset() : null);
424555
return Attacher.attach(doc, converterProperties);
425556
}
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-
438557
}

0 commit comments

Comments
 (0)