Skip to content

Commit 38239c3

Browse files
Samuel HuylebroeckiText-CI
authored andcommitted
Fixes and tests for Svgs without dimensions
RND-1011 Autoported commit. Original commit hash: [38a6a47e]
1 parent 6d23bdb commit 38239c3

File tree

10 files changed

+102
-5
lines changed

10 files changed

+102
-5
lines changed

itext.tests/itext.html2pdf.tests/itext/html2pdf/element/SvgTest.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ source product.
4444
using System.IO;
4545
using iText.Html2pdf;
4646
using iText.Kernel.Utils;
47+
using iText.Svg.Exceptions;
4748
using iText.Test;
4849
using iText.Test.Attributes;
4950

@@ -156,5 +157,44 @@ public virtual void ExternalObjectNonExistentRefTest() {
156157
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + name + ".pdf", sourceFolder
157158
+ "cmp_" + name + ".pdf", destinationFolder, "diff_" + name + "_"));
158159
}
160+
161+
/// <exception cref="System.IO.IOException"/>
162+
/// <exception cref="System.Exception"/>
163+
[NUnit.Framework.Test]
164+
[LogMessage(SvgLogMessageConstant.MISSING_WIDTH)]
165+
[LogMessage(SvgLogMessageConstant.MISSING_HEIGHT)]
166+
public virtual void SvgWithoutDimensionsTest() {
167+
String name = "svg_without_dimensions";
168+
HtmlConverter.ConvertToPdf(new FileInfo(sourceFolder + name + ".html"), new FileInfo(destinationFolder + name
169+
+ ".pdf"));
170+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + name + ".pdf", sourceFolder
171+
+ "cmp_" + name + ".pdf", destinationFolder, "diff_" + name + "_"));
172+
}
173+
174+
/// <exception cref="System.IO.IOException"/>
175+
/// <exception cref="System.Exception"/>
176+
[NUnit.Framework.Test]
177+
[LogMessage(SvgLogMessageConstant.MISSING_WIDTH)]
178+
[LogMessage(SvgLogMessageConstant.MISSING_HEIGHT)]
179+
public virtual void SvgWithoutDimensionsWithViewboxTest() {
180+
String name = "svg_without_dimensions_with_viewbox";
181+
HtmlConverter.ConvertToPdf(new FileInfo(sourceFolder + name + ".html"), new FileInfo(destinationFolder + name
182+
+ ".pdf"));
183+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + name + ".pdf", sourceFolder
184+
+ "cmp_" + name + ".pdf", destinationFolder, "diff_" + name + "_"));
185+
}
186+
187+
/// <exception cref="System.IO.IOException"/>
188+
/// <exception cref="System.Exception"/>
189+
[NUnit.Framework.Test]
190+
[LogMessage(SvgLogMessageConstant.MISSING_WIDTH, Count = 2)]
191+
[LogMessage(SvgLogMessageConstant.MISSING_HEIGHT, Count = 2)]
192+
public virtual void SvgWithoutDimensionsImageAndObjectRef() {
193+
String name = "SvgWithoutDimensionsImageAndObjectRef";
194+
HtmlConverter.ConvertToPdf(new FileInfo(sourceFolder + name + ".html"), new FileInfo(destinationFolder + name
195+
+ ".pdf"));
196+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + name + ".pdf", sourceFolder
197+
+ "cmp_" + name + ".pdf", destinationFolder, "diff_" + name + "_"));
198+
}
159199
}
160200
}
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>
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>

itext/itext.html2pdf/itext/html2pdf/util/SvgProcessingUtil.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,12 @@ source product.
4040
For more information, please contact iText Software Corp. at this
4141
4242
*/
43-
using iText.Html2pdf.Html;
4443
using iText.Kernel.Geom;
4544
using iText.Kernel.Pdf;
4645
using iText.Kernel.Pdf.Canvas;
4746
using iText.Kernel.Pdf.Xobject;
4847
using iText.Layout.Element;
49-
using iText.StyledXmlParser.Css.Util;
48+
using iText.Svg.Converter;
5049
using iText.Svg.Processors;
5150
using iText.Svg.Renderers;
5251
using iText.Svg.Renderers.Impl;
@@ -66,8 +65,11 @@ public class SvgProcessingUtil {
6665
/// <returns>image layout object</returns>
6766
public virtual Image CreateImageFromProcessingResult(ISvgProcessorResult result, PdfDocument pdfDocument) {
6867
ISvgNodeRenderer topSvgRenderer = result.GetRootRenderer();
69-
float width = CssUtils.ParseAbsoluteLength(topSvgRenderer.GetAttribute(AttributeConstants.WIDTH));
70-
float height = CssUtils.ParseAbsoluteLength(topSvgRenderer.GetAttribute(AttributeConstants.HEIGHT));
68+
float width;
69+
float height;
70+
float[] wh = SvgConverter.ExtractWidthAndHeight(topSvgRenderer);
71+
width = wh[0];
72+
height = wh[1];
7173
PdfFormXObject pdfForm = new PdfFormXObject(new Rectangle(0, 0, width, height));
7274
PdfCanvas canvas = new PdfCanvas(pdfForm, pdfDocument);
7375
SvgDrawContext context = new SvgDrawContext(null, result.GetFontProvider());

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
65a2f712ef541526396b47ad4917bdaec45fadef
1+
38a6a47e4b60216298f9fbb3a1b63e401cf6862e

0 commit comments

Comments
 (0)