Skip to content

Commit 77876b6

Browse files
AnhelinaMiText-CI
authored andcommitted
Support of SVG tag when using HtmlConverter#convertToElements
DEVSIX-3891 Autoported commit. Original commit hash: [19c929300]
1 parent bca3450 commit 77876b6

File tree

22 files changed

+185
-89
lines changed

22 files changed

+185
-89
lines changed

itext.tests/itext.html2pdf.tests/itext/html2pdf/Html2ElementsTest.cs

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ Any convert to elements workflow of course doesn't have a PdfDocument.
159159
[NUnit.Framework.Test]
160160
[LogMessage(iText.StyledXmlParser.Logs.StyledXmlParserLogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI
161161
, Count = 1)]
162-
[LogMessage(Html2PdfLogMessageConstant.WORKER_UNABLE_TO_PROCESS_OTHER_WORKER, Count = 1)]
163-
[LogMessage(Html2PdfLogMessageConstant.PDF_DOCUMENT_NOT_PRESENT, Count = 1)]
162+
[LogMessage(Html2PdfLogMessageConstant.WORKER_UNABLE_TO_PROCESS_OTHER_WORKER)]
164163
public virtual void HtmlObjectMalformedUrlTest() {
165164
String html = "<object data ='htt://as' type='image/svg+xml'></object>";
166165
IList<IElement> lst = HtmlConverter.ConvertToElements(html);
@@ -279,14 +278,66 @@ public virtual void ConvertToElementsAndCreateTwoDocumentsTest() {
279278
}
280279

281280
[NUnit.Framework.Test]
282-
//TODO: DEVSIX-3891 change the Assert after supporting the svg tag
283-
[LogMessage(Html2PdfLogMessageConstant.WORKER_UNABLE_TO_PROCESS_OTHER_WORKER, LogLevel = LogLevelConstants
284-
.ERROR)]
285281
public virtual void HtmlToElementsSvgTest() {
286282
String html = "<svg height=\"100\" width=\"100\">" + "<circle cx=\"50\" cy=\"50\" r=\"40\" stroke=\"black\" stroke-width=\"3\" fill=\"red\" />"
287283
+ "</svg>";
284+
String cmpPdf = sourceFolder + "cmp_htmlToElementsSvg.pdf";
285+
String outPdf = destinationFolder + "htmlToElementsSvg.pdf";
288286
IList<IElement> lst = HtmlConverter.ConvertToElements(html);
289-
NUnit.Framework.Assert.AreEqual(0, lst.Count);
287+
NUnit.Framework.Assert.AreEqual(1, lst.Count);
288+
using (Document document = new Document(new PdfDocument(new PdfWriter(outPdf)))) {
289+
foreach (IElement element in lst) {
290+
document.Add((Image)element);
291+
}
292+
}
293+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdf, cmpPdf, destinationFolder));
294+
}
295+
296+
[NUnit.Framework.Test]
297+
public virtual void HtmlToElementsSvgInTheTableTest() {
298+
String html = "<table style=\"border: 1pt solid black\">\n" + " <tr>\n" + " <td>\n" + " <svg height=\"100\" width=\"100\">\n"
299+
+ " <circle cx=\"50\" cy=\"50\" r=\"40\" stroke=\"black\" stroke-width=\"3\" fill=\"red\" />\n"
300+
+ " </svg>\n" + " </td>\n" + " <td>\n" + " test\n" + " </td>\n" + " </tr>\n"
301+
+ "</table>";
302+
String cmpPdf = sourceFolder + "cmp_htmlToElementsSvgInTheTable.pdf";
303+
String outPdf = destinationFolder + "htmlToElementsSvgInTheTable.pdf";
304+
IList<IElement> elements = HtmlConverter.ConvertToElements(html);
305+
using (Document document = new Document(new PdfDocument(new PdfWriter(outPdf)))) {
306+
foreach (IElement element in elements) {
307+
document.Add((IBlockElement)element);
308+
}
309+
}
310+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdf, cmpPdf, destinationFolder));
311+
}
312+
313+
[NUnit.Framework.Test]
314+
public virtual void HtmlToElementsSvgImgTest() {
315+
String html = "<img src=\"lines.svg\" height=\"500\" width=\"500\"/>";
316+
String cmpPdf = sourceFolder + "cmp_htmlToElementsSvgImg.pdf";
317+
String outPdf = destinationFolder + "htmlToElementsSvgImg.pdf";
318+
IList<IElement> elements = HtmlConverter.ConvertToElements(html, new ConverterProperties().SetBaseUri(sourceFolder
319+
));
320+
using (Document document = new Document(new PdfDocument(new PdfWriter(outPdf)))) {
321+
foreach (IElement element in elements) {
322+
document.Add((IBlockElement)element);
323+
}
324+
}
325+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdf, cmpPdf, destinationFolder));
326+
}
327+
328+
[NUnit.Framework.Test]
329+
public virtual void HtmlToElementsSvgObjectTest() {
330+
String html = "<object data ='lines.svg' type='image/svg+xml'></object>";
331+
String cmpPdf = sourceFolder + "cmp_htmlToElementsSvgObject.pdf";
332+
String outPdf = destinationFolder + "htmlToElementsSvgObject.pdf";
333+
IList<IElement> elements = HtmlConverter.ConvertToElements(html, new ConverterProperties().SetBaseUri(sourceFolder
334+
));
335+
using (Document document = new Document(new PdfDocument(new PdfWriter(outPdf)))) {
336+
foreach (IElement element in elements) {
337+
document.Add((Image)element);
338+
}
339+
}
340+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdf, cmpPdf, destinationFolder));
290341
}
291342

292343
private static void AddElementsToDocument(Document document, IList<IElement> elements) {

itext.tests/itext.html2pdf.tests/itext/html2pdf/resolver/resource/HtmlResourceResolverTest.cs

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ You should have received a copy of the GNU Affero General Public License
3232
using iText.Kernel.Pdf;
3333
using iText.Kernel.Pdf.Xobject;
3434
using iText.Kernel.Utils;
35+
using iText.Layout;
3536
using iText.Svg;
3637
using iText.Svg.Converter;
3738
using iText.Svg.Exceptions;
@@ -198,8 +199,6 @@ public virtual void ResourceResolverHtmlWithSvgDifferentLevels() {
198199
}
199200

200201
[NUnit.Framework.Test]
201-
[LogMessage(iText.StyledXmlParser.Logs.StyledXmlParserLogMessageConstant.UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_BASE_URI
202-
)]
203202
public virtual void AttemptToProcessBySvgProcessingUtilSvgWithImageTest() {
204203
// TODO review this test in the scope of DEVSIX-4107
205204
String fileName = "svgWithImage.svg";
@@ -209,21 +208,19 @@ public virtual void AttemptToProcessBySvgProcessingUtilSvgWithImageTest() {
209208
ISvgProcessorResult res = SvgConverter.ParseAndProcess(resourceResolver.RetrieveResourceAsInputStream(fileName
210209
), svgConverterProperties);
211210
ISvgNodeRenderer imageRenderer = ((SvgTagSvgNodeRenderer)res.GetRootRenderer()).GetChildren()[0];
212-
// Remove the previous result of the resource resolving in order to demonstrate that the resource will not be
213-
// resolved due to not setting of baseUri in the SvgProcessingUtil#createXObjectFromProcessingResult method.
214-
imageRenderer.SetAttribute(SvgConstants.Attributes.XLINK_HREF, "doggo.jpg");
211+
// Remove the previous result of the resource resolving in order to demonstrate that the resource will be
212+
// resolved due to setting of baseUri in the SvgProcessingUtil#createXObjectFromProcessingResult method.
213+
imageRenderer.SetAttribute(SvgConstants.Attributes.XLINK_HREF, "res/itextpdf.com/doggo.jpg");
215214
SvgProcessingUtil processingUtil = new SvgProcessingUtil(resourceResolver);
216-
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
217-
PdfFormXObject pdfFormXObject = processingUtil.CreateXObjectFromProcessingResult(res, pdfDocument);
215+
PdfDocument document = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
216+
PdfFormXObject pdfFormXObject = processingUtil.CreateXObjectFromProcessingResult(res, document);
218217
PdfDictionary resources = (PdfDictionary)pdfFormXObject.GetResources().GetPdfObject().Get(PdfName.XObject);
219218
PdfDictionary fm1Dict = (PdfDictionary)resources.Get(new PdfName("Fm1"));
220-
NUnit.Framework.Assert.IsFalse(((PdfDictionary)fm1Dict.Get(PdfName.Resources)).ContainsKey(PdfName.XObject
221-
));
219+
NUnit.Framework.Assert.IsTrue(((PdfDictionary)fm1Dict.Get(PdfName.Resources)).ContainsKey(PdfName.XObject)
220+
);
222221
}
223222

224223
[NUnit.Framework.Test]
225-
[LogMessage(iText.StyledXmlParser.Logs.StyledXmlParserLogMessageConstant.UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_BASE_URI
226-
)]
227224
public virtual void AttemptToProcessBySvgProcessingUtilSvgWithSvgTest() {
228225
// TODO review this test in the scope of DEVSIX-4107
229226
String fileName = "svgWithSvg.svg";
@@ -233,23 +230,45 @@ public virtual void AttemptToProcessBySvgProcessingUtilSvgWithSvgTest() {
233230
ISvgProcessorResult res = SvgConverter.ParseAndProcess(resourceResolver.RetrieveResourceAsInputStream(fileName
234231
), svgConverterProperties);
235232
ISvgNodeRenderer imageRenderer = ((SvgTagSvgNodeRenderer)res.GetRootRenderer()).GetChildren()[1];
236-
// Remove the previous result of the resource resolving in order to demonstrate that the resource will not be
237-
// resolved due to not setting of baseUri in the SvgProcessingUtil#createXObjectFromProcessingResult method.
238-
// But even if set baseUri in the SvgProcessingUtil#createXObjectFromProcessingResult method, the SVG will not
239-
// be processed, because in the createXObjectFromProcessingResult method we create ResourceResolver, not HtmlResourceResolver.
240-
imageRenderer.SetAttribute(SvgConstants.Attributes.XLINK_HREF, "res\\itextpdf.com\\lines.svg");
233+
// Remove the previous result of the resource resolving in order to demonstrate that the resource will be
234+
// resolved due to setting of baseUri in the SvgProcessingUtil#createXObjectFromProcessingResult method.
235+
imageRenderer.SetAttribute(SvgConstants.Attributes.XLINK_HREF, "res/itextpdf.com/lines.svg");
241236
SvgProcessingUtil processingUtil = new SvgProcessingUtil(resourceResolver);
242-
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
243-
PdfFormXObject pdfFormXObject = processingUtil.CreateXObjectFromProcessingResult(res, pdfDocument);
237+
PdfDocument document = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
238+
PdfFormXObject pdfFormXObject = processingUtil.CreateXObjectFromProcessingResult(res, document);
244239
PdfDictionary resources = (PdfDictionary)pdfFormXObject.GetResources().GetPdfObject().Get(PdfName.XObject);
245240
PdfDictionary fm1Dict = (PdfDictionary)resources.Get(new PdfName("Fm1"));
246-
NUnit.Framework.Assert.IsFalse(((PdfDictionary)fm1Dict.Get(PdfName.Resources)).ContainsKey(PdfName.XObject
247-
));
241+
NUnit.Framework.Assert.IsTrue(((PdfDictionary)fm1Dict.Get(PdfName.Resources)).ContainsKey(PdfName.XObject)
242+
);
243+
}
244+
245+
[NUnit.Framework.Test]
246+
public virtual void SvgInsideSvgTest() {
247+
String svgFileName = "svgInsideSvg.svg";
248+
String cmpFileName = SOURCE_FOLDER + "cmp_svgInsideSvg.pdf";
249+
String outFileName = DESTINATION_FOLDER + "svgInsideSvg.pdf";
250+
using (Document document = new Document(new PdfDocument(new PdfWriter(outFileName)))) {
251+
ProcessorContext context = new ProcessorContext(new ConverterProperties());
252+
HtmlResourceResolver resourceResolver = new HtmlResourceResolver(SOURCE_FOLDER, context);
253+
ISvgConverterProperties svgConverterProperties = ContextMappingHelper.MapToSvgConverterProperties(context);
254+
ISvgProcessorResult result = SvgConverter.ParseAndProcess(resourceResolver.RetrieveResourceAsInputStream(svgFileName
255+
), svgConverterProperties);
256+
ISvgNodeRenderer imageRenderer = ((SvgTagSvgNodeRenderer)result.GetRootRenderer()).GetChildren()[0];
257+
ISvgNodeRenderer svgRenderer = ((SvgTagSvgNodeRenderer)result.GetRootRenderer()).GetChildren()[2];
258+
// Remove the previous result of the resource resolving in order to demonstrate that the resource will be
259+
// resolved due to setting of baseUri in the SvgProcessingUtil#createXObjectFromProcessingResult method.
260+
// TODO DEVSIX-4107 However the SVG will not be displayed since it is expected to be drawn during
261+
// HtmlResourceResolver#processAsSvg call, but document is null, so SvgProcessingUtil#
262+
// createSvgImageFromProcessingResult method is called instead of createXObjectFromProcessingResult.
263+
imageRenderer.SetAttribute(SvgConstants.Attributes.XLINK_HREF, "res/itextpdf.com/doggo.jpg");
264+
svgRenderer.SetAttribute(SvgConstants.Attributes.XLINK_HREF, "res/itextpdf.com/lines.svg");
265+
document.Add(new SvgProcessingUtil(resourceResolver).CreateSvgImageFromProcessingResult(result));
266+
}
267+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, DESTINATION_FOLDER
268+
, "diff"));
248269
}
249270

250271
[NUnit.Framework.Test]
251-
[LogMessage(iText.StyledXmlParser.Logs.StyledXmlParserLogMessageConstant.UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_BASE_URI
252-
)]
253272
public virtual void ResourceResolverSvgEmbeddedSvg() {
254273
// TODO review this test in the scope of DEVSIX-4107
255274
String outPdf = DESTINATION_FOLDER + "resourceResolverSvgEmbeddedSvg.pdf";
@@ -259,8 +278,6 @@ public virtual void ResourceResolverSvgEmbeddedSvg() {
259278
}
260279

261280
[NUnit.Framework.Test]
262-
[LogMessage(iText.StyledXmlParser.Logs.StyledXmlParserLogMessageConstant.UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_BASE_URI
263-
)]
264281
public virtual void ResourceResolverObjectWithSvgEmbeddedSvg() {
265282
// TODO review this test in the scope of DEVSIX-4107
266283
String outPdf = DESTINATION_FOLDER + "resourceResolverObjectWithSvgEmbeddedSvg.pdf";
Lines changed: 11 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)