Skip to content

Commit 67c9cfa

Browse files
author
Dmitry Radchuk
committed
Improve <object> and <img> svg support
DEVSIX-8829
1 parent 194c365 commit 67c9cfa

File tree

370 files changed

+4047
-25
lines changed

Some content is hidden

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

370 files changed

+4047
-25
lines changed

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,8 @@ public ImgTagWorker(IElementNode element, ProcessorContext context) {
7676
image = new HtmlImage((PdfImageXObject) imageXObject);
7777
} else if (imageXObject instanceof SvgImageXObject) {
7878
SvgImageXObject svgImageXObject = (SvgImageXObject) imageXObject;
79-
// TODO DEVSIX-8829 remove relative sized SVG generating after adding support in object element
80-
if (svgImageXObject.isRelativeSized()) {
81-
svgImageXObject.updateBBox(null, null);
82-
if (context.getPdfDocument() != null) {
83-
svgImageXObject.generate(context.getPdfDocument());
84-
}
85-
svgImageXObject.setRelativeSized(false);
86-
}
8779
image = new SvgImage(svgImageXObject);
80+
svgImageXObject.setIsCreatedByImg(true);
8881
} else if (imageXObject instanceof PdfFormXObject) {
8982
image = new HtmlImage((PdfFormXObject) imageXObject);
9083
} else {

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,8 @@ private boolean isSvgImage(String typeAttribute) {
119119
public void processEnd(IElementNode element, ProcessorContext context) {
120120
// Create Image object
121121
if (res != null) {
122-
SvgImageXObject svgImageXObject = processUtil.createXObjectFromProcessingResult(res, context);
123-
// TODO DEVSIX-8829 remove relative sized SVG generating after adding support in object element
124-
if (svgImageXObject.isRelativeSized()) {
125-
svgImageXObject.updateBBox(null, null);
126-
if (context.getPdfDocument() != null) {
127-
svgImageXObject.generate(context.getPdfDocument());
128-
}
129-
svgImageXObject.setRelativeSized(false);
130-
}
122+
SvgImageXObject svgImageXObject = processUtil.createXObjectFromProcessingResult(res, context, false);
123+
svgImageXObject.setIsCreatedByObject(true);
131124
image = new SvgImage(svgImageXObject);
132125
AccessiblePropHelper.trySetLangAttribute(image, element);
133126
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public SvgTagWorker(IElementNode element, ProcessorContext context) {
7272
public void processEnd(IElementNode element, ProcessorContext context) {
7373
if (processingResult != null) {
7474
SvgImageXObject svgImageXObject = new SvgProcessingUtil(context.getResourceResolver())
75-
.createXObjectFromProcessingResult(processingResult, context);
75+
.createXObjectFromProcessingResult(processingResult, context, true);
7676
svgImage = new SvgImage(svgImageXObject);
7777

7878
AccessiblePropHelper.trySetLangAttribute(svgImage, element);

src/main/java/com/itextpdf/html2pdf/css/apply/util/BackgroundApplierUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ protected float[] resolveWidthAndHeight(Float width, Float height, float areaWid
570570
svgRootRenderer.setAttribute(Attributes.WIDTH, null);
571571
svgRootRenderer.setAttribute(Attributes.HEIGHT, null);
572572
}
573-
svgImageXObject.updateBBox(finalWidth, finalHeight);
573+
svgImageXObject.updateBBox((float) finalWidth, (float) finalHeight);
574574
svgImageXObject.generate(null);
575575

576576
return new float[] {(float) finalWidth, (float) finalHeight};

src/main/java/com/itextpdf/html2pdf/resolver/resource/HtmlResourceResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,6 @@ private static PdfFormXObject processAsSvg(InputStream stream, ProcessorContext
149149
}
150150
ISvgProcessorResult res = SvgConverter.parseAndProcess(stream, svgConverterProperties);
151151
SvgProcessingUtil processingUtil = new SvgProcessingUtil(context.getResourceResolver());
152-
return processingUtil.createXObjectFromProcessingResult(res, context);
152+
return processingUtil.createXObjectFromProcessingResult(res, context, true);
153153
}
154154
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,13 @@ public PdfFormXObject createXObjectFromProcessingResult(ISvgProcessorResult resu
110110
*
111111
* @param result processing result containing the SVG information
112112
* @param context html2pdf processor context
113+
* @param generateAbsolutelySizedSvg if true and context has pdf document and svg is not relative sized, it will be immediately
114+
* generated, otherwise no generation will be performed
113115
*
114116
* @return new {@link SvgImageXObject} instance
115117
*/
116-
public SvgImageXObject createXObjectFromProcessingResult(ISvgProcessorResult result, ProcessorContext context) {
118+
public SvgImageXObject createXObjectFromProcessingResult(ISvgProcessorResult result, ProcessorContext context,
119+
boolean generateAbsolutelySizedSvg) {
117120
float em = context.getCssContext().getCurrentFontSize();
118121
SvgDrawContext svgContext = new SvgDrawContext(resourceResolver, result.getFontProvider());
119122
svgContext.getCssContext().setRootFontSize(context.getCssContext().getRootFontSize());
@@ -123,7 +126,7 @@ public SvgImageXObject createXObjectFromProcessingResult(ISvgProcessorResult res
123126
} else {
124127
Rectangle bbox = SvgCssUtils.extractWidthAndHeight(result.getRootRenderer(), em, svgContext);
125128
SvgImageXObject svgImageXObject = new SvgImageXObject(bbox, result, resourceResolver);
126-
if (context.getPdfDocument() != null) {
129+
if (context.getPdfDocument() != null && generateAbsolutelySizedSvg) {
127130
svgImageXObject.generate(context.getPdfDocument());
128131
}
129132
return svgImageXObject;

src/test/java/com/itextpdf/html2pdf/css/BackgroundSvgImageTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,11 @@ public void contain_fixed_height_percent_widthTest() throws IOException, Interru
598598
convertToPdfAndCompare("contain_fixed_height_percent_width", SOURCE_FOLDER, DESTINATION_FOLDER);
599599
}
600600

601+
@Test
602+
public void contain_fixed_height_percent_same_svg_widthTest() throws IOException, InterruptedException {
603+
convertToPdfAndCompare("contain_fixed_height_percent_width_same_svg", SOURCE_FOLDER, DESTINATION_FOLDER);
604+
}
605+
601606
@Test
602607
public void contain_fixed_height_percent_width_prRatio_noneTest() throws IOException, InterruptedException {
603608
convertToPdfAndCompare("contain_fixed_height_percent_width_prRatio_none", SOURCE_FOLDER, DESTINATION_FOLDER);

0 commit comments

Comments
 (0)