Skip to content

Commit 2bd011a

Browse files
committed
[RELEASE] iText 7 pdfHTML - 2.1.1
https://github.com/itext/i7j-pdfhtml/releases/tag/2.1.1 * release/2.1.1: [RELEASE] 2.1.1-SNAPSHOT -> 2.1.1 Add test. DEVSIX-2335 Remove unneeded args from invoke method Update ProductInfo Add new test for handling SVG in <object> tag with inner resources Add test for check resources released after conversion Add processing of calligraphic indic and arabic fonts Free resources after reading them Add some new min/max width test. Update cmps. Fix tests not checking for enough logging messages Use ConverterProperties when converting SVG to PDF. RND-1108 Fixes and tests for Svgs without dimensions Add new ignored tests for vertical alignment of inline blocks Create tests for FontSelector with different font families Fix issues with additional separator in ResourceResolver Update cmp files Update the reset method of FontProvider. Update the default font and the default font-family for an input element. Add tests demonstrating background bug for inner inline elements Remove unnecessary html file. Add a test related to font-family processing. Add a new white-space test. [RELEASE] 2.1.0-SNAPSHOT -> 2.1.1-SNAPSHOT Fix incorrect class passed to logger creation Add a new test related to plain text processing Minor fix for a constant Javadoc Add test
2 parents 741932c + b66dbd7 commit 2bd011a

File tree

164 files changed

+5253
-106
lines changed

Some content is hidden

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

164 files changed

+5253
-106
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
<parent>
55
<groupId>com.itextpdf</groupId>
66
<artifactId>root</artifactId>
7-
<version>7.1.3</version>
7+
<version>7.1.4</version>
88
<relativePath/>
99
</parent>
1010
<artifactId>html2pdf</artifactId>
11-
<version>2.1.0</version>
11+
<version>2.1.1</version>
1212
<name>pdfHTML</name>
1313
<description>pdfHTML is an iText 7 add-on that lets you to parse (X)HTML snippets and the associated CSS and converts
1414
them to PDF.</description>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,5 @@ public class Html2PdfProductInfo {
5656
public static final int MAJOR_VERSION = 2;
5757

5858
/** The minor version number. */
59-
public static final int MINOR_VERSION = 0;
59+
public static final int MINOR_VERSION = 1;
6060
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,11 @@ public static void convertToPdf(File htmlFile, File pdfFile) throws IOException
173173
*/
174174
public static void convertToPdf(File htmlFile, File pdfFile, ConverterProperties converterProperties) throws IOException {
175175
if (converterProperties == null) {
176-
converterProperties = new ConverterProperties().setBaseUri(Paths.get(FileUtil.getParentDirectory(htmlFile.getAbsolutePath())).toUri().toURL().toExternalForm() + File.separator);
176+
String baseUri = FileUtil.getParentDirectory(htmlFile);
177+
converterProperties = new ConverterProperties().setBaseUri(baseUri);
177178
} else if (converterProperties.getBaseUri() == null) {
178-
converterProperties = new ConverterProperties(converterProperties).setBaseUri(Paths.get(FileUtil.getParentDirectory(htmlFile.getAbsolutePath())).toUri().toURL().toExternalForm() + File.separator);
179+
String baseUri = FileUtil.getParentDirectory(htmlFile);
180+
converterProperties = new ConverterProperties(converterProperties).setBaseUri(baseUri);
179181
}
180182
try (FileInputStream fileInputStream = new FileInputStream(htmlFile.getAbsolutePath());
181183
FileOutputStream fileOutputStream = new FileOutputStream(pdfFile.getAbsolutePath())) {

src/main/java/com/itextpdf/html2pdf/attach/ProcessorContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ This file is part of the iText (R) project.
6060
import com.itextpdf.layout.font.FontSet;
6161
import com.itextpdf.layout.font.Range;
6262
import com.itextpdf.styledxmlparser.css.media.MediaDeviceDescription;
63+
import com.itextpdf.styledxmlparser.resolver.font.BasicFontProvider;
6364
import com.itextpdf.styledxmlparser.resolver.resource.ResourceResolver;
6465

6566
/**
@@ -414,7 +415,7 @@ public void reset() {
414415
this.linkContext = new LinkContext();
415416
this.formFieldNameResolver.reset();
416417
//Reset font provider. PdfFonts shall be reseted.
417-
this.fontProvider = new FontProvider(this.fontProvider.getFontSet());
418+
this.fontProvider = new BasicFontProvider(this.fontProvider.getFontSet(), this.fontProvider.getDefaultFontFamily());
418419
this.tempFonts = null;
419420
this.outlineHandler.reset();
420421
this.processingInlineSvg = false;

src/main/java/com/itextpdf/html2pdf/attach/impl/tags/ImgTagWorker.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,18 @@ This file is part of the iText (R) project.
4646
import com.itextpdf.html2pdf.attach.ITagWorker;
4747
import com.itextpdf.html2pdf.attach.ProcessorContext;
4848
import com.itextpdf.html2pdf.css.CssConstants;
49+
import com.itextpdf.html2pdf.html.AttributeConstants;
4950
import com.itextpdf.html2pdf.util.SvgProcessingUtil;
5051
import com.itextpdf.io.util.MessageFormatUtil;
5152
import com.itextpdf.kernel.pdf.xobject.PdfImageXObject;
5253
import com.itextpdf.layout.IPropertyContainer;
5354
import com.itextpdf.layout.element.Image;
54-
import com.itextpdf.html2pdf.html.AttributeConstants;
5555
import com.itextpdf.styledxmlparser.node.IElementNode;
5656
import com.itextpdf.styledxmlparser.resolver.resource.ResourceResolver;
5757
import com.itextpdf.svg.converter.SvgConverter;
5858
import com.itextpdf.svg.exceptions.SvgProcessingException;
5959
import com.itextpdf.svg.processors.ISvgProcessorResult;
60+
import com.itextpdf.svg.processors.impl.SvgConverterProperties;
6061
import org.slf4j.Logger;
6162
import org.slf4j.LoggerFactory;
6263

@@ -72,7 +73,7 @@ public class ImgTagWorker implements ITagWorker {
7273
/**
7374
* The logger.
7475
*/
75-
private static final Logger LOGGER = LoggerFactory.getLogger(ObjectTagWorker.class);
76+
private static final Logger LOGGER = LoggerFactory.getLogger(ImgTagWorker.class);
7677

7778
/**
7879
* The image.
@@ -98,14 +99,15 @@ public ImgTagWorker(IElementNode element, ProcessorContext context) {
9899
} else {
99100
byte[] resourceBytes = context.getResourceResolver().retrieveBytesFromResource(src);
100101
if (resourceBytes != null) {
101-
InputStream resourceStream = context.getResourceResolver().retrieveResourceAsInputStream(src);
102-
//Try with svg
103-
try {
104-
processAsSvg(resourceStream, context);
105-
} catch (SvgProcessingException spe) {
106-
LOGGER.error(MessageFormatUtil.format(LogMessageConstant.UNABLE_TO_PROCESS_IMAGE_AS_SVG, context.getBaseUri(), src));
107-
} catch(IOException ioe){
108-
LOGGER.error(MessageFormatUtil.format(LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI,context.getBaseUri(),src));
102+
try (InputStream resourceStream = context.getResourceResolver().retrieveResourceAsInputStream(src)) {
103+
//Try with svg
104+
try {
105+
processAsSvg(resourceStream, context);
106+
} catch (SvgProcessingException spe) {
107+
LOGGER.error(MessageFormatUtil.format(LogMessageConstant.UNABLE_TO_PROCESS_IMAGE_AS_SVG, context.getBaseUri(), src));
108+
}
109+
} catch (IOException ioe) {
110+
LOGGER.error(MessageFormatUtil.format(LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI, context.getBaseUri(), src));
109111
}
110112
}
111113
}
@@ -129,7 +131,11 @@ public ImgTagWorker(IElementNode element, ProcessorContext context) {
129131

130132
private void processAsSvg(InputStream stream, ProcessorContext context) throws IOException {
131133
SvgProcessingUtil processingUtil = new SvgProcessingUtil();
132-
ISvgProcessorResult res = SvgConverter.parseAndProcess(stream);
134+
SvgConverterProperties svgConverterProperties = new SvgConverterProperties();
135+
svgConverterProperties.setBaseUri(context.getBaseUri())
136+
.setFontProvider(context.getFontProvider())
137+
.setMediaDeviceDescription(context.getDeviceDescription());
138+
ISvgProcessorResult res = SvgConverter.parseAndProcess(stream, svgConverterProperties);
133139
if (context.getPdfDocument() != null) {
134140
image = processingUtil.createImageFromProcessingResult(res, context.getPdfDocument());
135141
}

src/main/java/com/itextpdf/html2pdf/attach/impl/tags/ObjectTagWorker.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,17 @@ This file is part of the iText (R) project.
4545
import com.itextpdf.html2pdf.LogMessageConstant;
4646
import com.itextpdf.html2pdf.attach.ITagWorker;
4747
import com.itextpdf.html2pdf.attach.ProcessorContext;
48+
import com.itextpdf.html2pdf.html.AttributeConstants;
4849
import com.itextpdf.html2pdf.util.SvgProcessingUtil;
4950
import com.itextpdf.io.util.MessageFormatUtil;
5051
import com.itextpdf.kernel.pdf.PdfDocument;
5152
import com.itextpdf.layout.IPropertyContainer;
5253
import com.itextpdf.layout.element.Image;
53-
import com.itextpdf.html2pdf.html.AttributeConstants;
5454
import com.itextpdf.styledxmlparser.node.IElementNode;
5555
import com.itextpdf.svg.converter.SvgConverter;
5656
import com.itextpdf.svg.exceptions.SvgProcessingException;
5757
import com.itextpdf.svg.processors.ISvgProcessorResult;
58+
import com.itextpdf.svg.processors.impl.SvgConverterProperties;
5859
import org.slf4j.Logger;
5960
import org.slf4j.LoggerFactory;
6061

@@ -94,18 +95,24 @@ public ObjectTagWorker(IElementNode element, ProcessorContext context) {
9495
String type = element.getAttribute(AttributeConstants.TYPE);
9596
if (isSvgImage(type)) {
9697
//Use resource resolver to retrieve the URL
97-
InputStream svgStream = context.getResourceResolver().retrieveResourceAsInputStream(element.getAttribute(AttributeConstants.DATA));
98-
if(svgStream != null) {
99-
try {
100-
res = SvgConverter.parseAndProcess(svgStream);
101-
} catch (SvgProcessingException spe) {
102-
LOGGER.error(spe.getMessage());
103-
} catch(IOException ie){
104-
LOGGER.error(MessageFormatUtil.format(LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI,context.getBaseUri(),element.getAttribute(AttributeConstants.DATA)));
98+
try (InputStream svgStream = context.getResourceResolver().retrieveResourceAsInputStream(element.getAttribute(AttributeConstants.DATA))) {
99+
if (svgStream != null) {
100+
try {
101+
SvgConverterProperties svgConverterProperties = new SvgConverterProperties();
102+
svgConverterProperties.setBaseUri(context.getBaseUri())
103+
.setFontProvider(context.getFontProvider())
104+
.setMediaDeviceDescription(context.getDeviceDescription());
105+
res = SvgConverter.parseAndProcess(svgStream, svgConverterProperties);
106+
} catch (SvgProcessingException spe) {
107+
LOGGER.error(spe.getMessage());
108+
}
105109
}
110+
} catch (IOException ie) {
111+
LOGGER.error(MessageFormatUtil.format(LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI, context.getBaseUri(), element.getAttribute(AttributeConstants.DATA)));
106112
}
107113
}
108114
}
115+
109116
@Override
110117
public void processEnd(IElementNode element, ProcessorContext context) {
111118
if (context.getPdfDocument() != null) {

src/main/java/com/itextpdf/html2pdf/css/CssConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public class CssConstants extends CommonCssConstants {
112112
/** The Constant VERTICAL_ALIGN. */
113113
public static final String VERTICAL_ALIGN = "vertical-align";
114114

115-
/** The Constant VISIBILITY. */
115+
/** The Constant VISIBLE. */
116116
public static final String VISIBLE = "visible";
117117

118118
/** The Constant MAX_WIDTH. */

src/main/java/com/itextpdf/html2pdf/css/apply/impl/SpanTagCssApplier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void apply(ProcessorContext context, IStylesContainer stylesContainer, IT
117117
*/
118118
protected void applyChildElementStyles(IPropertyContainer element, Map<String, String> css, ProcessorContext context, IStylesContainer stylesContainer) {
119119
FontStyleApplierUtil.applyFontStyles(css, context, stylesContainer, element);
120-
//TODO: Background-applying currently doesn't work in html way for spans inside other spans.
120+
//TODO DEVSIX-2118: Background-applying currently doesn't work in html way for spans inside other spans.
121121
BackgroundApplierUtil.applyBackground(css, context, element);
122122
//TODO: Border-applying currently doesn't work in html way for spans inside other spans.
123123
BorderStyleApplierUtil.applyBorders(css, context, element);

src/main/java/com/itextpdf/html2pdf/resolver/font/DefaultFontProvider.java

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,18 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.html2pdf.resolver.font;
4444

45+
import com.itextpdf.html2pdf.LogMessageConstant;
4546
import com.itextpdf.io.util.ResourceUtil;
4647
import com.itextpdf.io.util.StreamUtil;
48+
import com.itextpdf.layout.font.FontProvider;
49+
import com.itextpdf.layout.font.Range;
50+
import com.itextpdf.layout.font.RangeBuilder;
4751
import com.itextpdf.styledxmlparser.resolver.font.BasicFontProvider;
52+
import org.slf4j.LoggerFactory;
4853

4954
import java.io.InputStream;
55+
import java.lang.reflect.Method;
56+
import java.util.ArrayList;
5057

5158
/**
5259
* The default {@link BasicFontProvider} for pdfHTML, that, as opposed to
@@ -55,6 +62,11 @@ This file is part of the iText (R) project.
5562
*/
5663
public class DefaultFontProvider extends BasicFontProvider {
5764

65+
/**
66+
* The logger.
67+
*/
68+
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DefaultFontProvider.class);
69+
5870
/** The path to the shipped fonts. */
5971
private static final String SHIPPED_FONT_RESOURCE_PATH = "com/itextpdf/html2pdf/font/";
6072

@@ -74,6 +86,13 @@ public class DefaultFontProvider extends BasicFontProvider {
7486
"FreeSerifItalic.ttf",
7587
};
7688

89+
// This range exclude Hebrew, Arabic, Syriac, Arabic Supplement, Thaana, NKo, Samaritan,
90+
// Mandaic, Syriac Supplement, Arabic Extended-A, Devanagari, Bengali, Gurmukhi, Gujarati,
91+
// Oriya, Tamil, Telugu, Kannada, Malayalam, Sinhala, Thai unicode blocks.
92+
// Those blocks either require pdfCalligraph or do not supported by GNU Free Fonts.
93+
private static final Range FREE_FONT_RANGE = new RangeBuilder()
94+
.addRange(0, 0x058F).addRange(0x0E80, Integer.MAX_VALUE).create();
95+
7796
/**
7897
* Creates a new {@link DefaultFontProvider} instance.
7998
*/
@@ -91,22 +110,48 @@ public DefaultFontProvider() {
91110
public DefaultFontProvider(boolean registerStandardPdfFonts, boolean registerShippedFreeFonts, boolean registerSystemFonts) {
92111
super(registerStandardPdfFonts, registerSystemFonts);
93112
if (registerShippedFreeFonts) {
94-
addShippedFreeFonts();
113+
addShippedFreeFonts(addCalligraphFonts());
95114
}
96115
}
97116

98117
/**
99118
* Adds the shipped free fonts.
100119
*/
101-
private void addShippedFreeFonts() {
120+
private void addShippedFreeFonts(Range rangeToLoad) {
102121
for (String fontName : SHIPPED_FONT_NAMES) {
103-
InputStream stream = ResourceUtil.getResourceStream(SHIPPED_FONT_RESOURCE_PATH + fontName);
104-
try {
122+
try (InputStream stream = ResourceUtil.getResourceStream(SHIPPED_FONT_RESOURCE_PATH + fontName)) {
105123
byte[] fontProgramBytes = StreamUtil.inputStreamToArray(stream);
106-
addFont(fontProgramBytes);
107-
} catch (Exception exc) {
124+
addFont(fontProgramBytes, null, rangeToLoad);
125+
} catch (Exception e) {
126+
LOGGER.error(LogMessageConstant.ERROR_LOADING_FONT);
108127
}
109128
}
110129
}
111130

131+
private Range addCalligraphFonts() {
132+
String methodName = "loadShippedFonts";
133+
Class<?> klass = null;
134+
try {
135+
klass = getTypographyUtilsClass();
136+
} catch (ClassNotFoundException ignored) { }
137+
if (klass != null) {
138+
try {
139+
Method m = klass.getMethod(methodName);
140+
ArrayList<byte[]> fontStreams = (ArrayList<byte[]>) m.invoke(null, null);
141+
for (byte[] font : fontStreams)
142+
addFont(font);
143+
// here we return a unicode range that excludes the loaded from the calligraph module fonts
144+
// i.e. the unicode range that is to be rendered with standard or shipped free fonts
145+
return FREE_FONT_RANGE;
146+
} catch (Exception e) {
147+
LOGGER.error(LogMessageConstant.ERROR_LOADING_FONT);
148+
}
149+
}
150+
return null;
151+
}
152+
153+
private static Class<?> getTypographyUtilsClass() throws ClassNotFoundException {
154+
String typographyClassFullName = "com.itextpdf.typography.util.TypographyShippedFontsUtil";
155+
return Class.forName(typographyClassFullName);
156+
}
112157
}

src/main/java/com/itextpdf/html2pdf/util/SvgProcessingUtil.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ This file is part of the iText (R) project.
4949
import com.itextpdf.layout.element.Image;
5050
import com.itextpdf.html2pdf.html.AttributeConstants;
5151
import com.itextpdf.styledxmlparser.css.util.CssUtils;
52+
import com.itextpdf.svg.converter.SvgConverter;
5253
import com.itextpdf.svg.processors.ISvgProcessorResult;
5354
import com.itextpdf.svg.renderers.ISvgNodeRenderer;
5455
import com.itextpdf.svg.renderers.SvgDrawContext;
@@ -69,8 +70,10 @@ public class SvgProcessingUtil {
6970
*/
7071
public Image createImageFromProcessingResult(ISvgProcessorResult result, PdfDocument pdfDocument){
7172
ISvgNodeRenderer topSvgRenderer = result.getRootRenderer();
72-
float width = CssUtils.parseAbsoluteLength(topSvgRenderer.getAttribute(AttributeConstants.WIDTH));
73-
float height = CssUtils.parseAbsoluteLength(topSvgRenderer.getAttribute(AttributeConstants.HEIGHT));
73+
float width, height;
74+
float[] wh = SvgConverter.extractWidthAndHeight(topSvgRenderer);
75+
width = wh[0];
76+
height=wh[1];
7477
PdfFormXObject pdfForm = new PdfFormXObject(new Rectangle(0, 0, width, height));
7578
PdfCanvas canvas = new PdfCanvas(pdfForm, pdfDocument);
7679

0 commit comments

Comments
 (0)