Skip to content

Commit 38a6a47

Browse files
author
Samuel Huylebroeck
committed
Fixes and tests for Svgs without dimensions
RND-1011
1 parent 65a2f71 commit 38a6a47

File tree

9 files changed

+94
-2
lines changed

9 files changed

+94
-2
lines changed

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

src/test/java/com/itextpdf/html2pdf/element/SvgTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ This file is part of the iText (R) project.
4545
import com.itextpdf.html2pdf.HtmlConverter;
4646
import com.itextpdf.styledxmlparser.LogMessageConstant;
4747
import com.itextpdf.kernel.utils.CompareTool;
48+
import com.itextpdf.svg.exceptions.SvgLogMessageConstant;
4849
import com.itextpdf.test.ExtendedITextTest;
4950
import com.itextpdf.test.annotations.LogMessage;
5051
import com.itextpdf.test.annotations.LogMessages;
@@ -151,4 +152,37 @@ public void externalObjectNonExistentRefTest() throws IOException, InterruptedEx
151152
HtmlConverter.convertToPdf(new File(sourceFolder + name + ".html"), new File(destinationFolder + name + ".pdf"));
152153
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + name + ".pdf", sourceFolder + "cmp_" + name + ".pdf", destinationFolder, "diff_" + name + "_"));
153154
}
155+
156+
@Test
157+
@LogMessages(messages = {
158+
@LogMessage(messageTemplate = SvgLogMessageConstant.MISSING_WIDTH),
159+
@LogMessage(messageTemplate = SvgLogMessageConstant.MISSING_HEIGHT),
160+
})
161+
public void SvgWithoutDimensionsTest() throws IOException, InterruptedException {
162+
String name = "svg_without_dimensions";
163+
HtmlConverter.convertToPdf(new File(sourceFolder + name + ".html"), new File(destinationFolder + name + ".pdf"));
164+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + name + ".pdf", sourceFolder + "cmp_" + name + ".pdf", destinationFolder, "diff_" + name + "_"));
165+
}
166+
@Test
167+
@LogMessages(messages = {
168+
@LogMessage(messageTemplate = SvgLogMessageConstant.MISSING_WIDTH),
169+
@LogMessage(messageTemplate = SvgLogMessageConstant.MISSING_HEIGHT),
170+
})
171+
public void SvgWithoutDimensionsWithViewboxTest() throws IOException, InterruptedException {
172+
String name = "svg_without_dimensions_with_viewbox";
173+
HtmlConverter.convertToPdf(new File(sourceFolder + name + ".html"), new File(destinationFolder + name + ".pdf"));
174+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + name + ".pdf", sourceFolder + "cmp_" + name + ".pdf", destinationFolder, "diff_" + name + "_"));
175+
}
176+
177+
@Test
178+
@LogMessages(messages = {
179+
@LogMessage(messageTemplate = SvgLogMessageConstant.MISSING_WIDTH, count = 2),
180+
@LogMessage(messageTemplate = SvgLogMessageConstant.MISSING_HEIGHT, count = 2),
181+
})
182+
public void SvgWithoutDimensionsImageAndObjectRef() throws IOException, InterruptedException {
183+
String name = "SvgWithoutDimensionsImageAndObjectRef";
184+
HtmlConverter.convertToPdf(new File(sourceFolder + name + ".html"), new File(destinationFolder + name + ".pdf"));
185+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + name + ".pdf", sourceFolder + "cmp_" + name + ".pdf", destinationFolder, "diff_" + name + "_"));
186+
}
187+
154188
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Example</title>
5+
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
6+
</head>
7+
<body>
8+
<h1>HTML5 SVG Demo</h1>
9+
<p> Example Image</p>
10+
<img src="resources/widthHeightNotDefined.svg" />
11+
<p>Example Object1</p>
12+
<object data="resources/widthHeightNotDefined.svg" type="image/svg+xml"></object>
13+
</body>
14+
</html>
Binary file not shown.
Lines changed: 9 additions & 0 deletions
Loading
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<html>
2+
<head>
3+
<title>Inline SVG</title>
4+
</head>
5+
<body>
6+
<p>Svg completly defined inside the html below</p>
7+
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
8+
<rect width="500" height="400" fill="none" stroke="black"/>
9+
<line x1="0" y1="0" x2="20" y2="135" stroke="black"/>
10+
<circle cx="20" cy="135" r="5" fill="none" stroke="black"/>
11+
<text x="20" y="135" font-family="Verdana" font-size="35">
12+
Hello world
13+
</text>
14+
</svg>
15+
</body>
16+
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<html>
2+
<head>
3+
<title>Inline SVG</title>
4+
</head>
5+
<body>
6+
<p>Svg completly defined inside the html below</p>
7+
<svg viewbox="0 0 50 100" xmlns="http://www.w3.org/2000/svg" version="1.1">
8+
<rect width="500" height="400" fill="none" stroke="black"/>
9+
<line x1="0" y1="0" x2="20" y2="135" stroke="black"/>
10+
<circle cx="20" cy="135" r="5" fill="none" stroke="black"/>
11+
<text x="20" y="135" font-family="Verdana" font-size="35">
12+
Hello world
13+
</text>
14+
</svg>
15+
</body>
16+
</html>

0 commit comments

Comments
 (0)