Home » Java » java – SVG file can't convert to an expected image in Itext7-Exceptionshub

java – SVG file can't convert to an expected image in Itext7-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have an SVG file, when I used SvgConverter.convertToImage to convert it as an image and added into a PDF document, the output image isn’t as expected. The image stroke size became much bigger than that of the SVG file. My svg plugin version is 7.1.9. How to fix such issue?
What’s the expected chart:expected Chart
The actual output in pdf is:actual chart

SvgConverterProperties props = new SvgConverterProperties();
FontProvider provider = new FontProvider();
provider.addFont(FontProgramFactory.createFont("fontname"));
props.setFontProvider(provider);
Image pdfImage = SvgConverter.convertToImage(new FileInputStream(imagePath), pdfDoc, props);

The svg contents:

<svg version="1.1" id="Layer_1" xmlns:x="&ns_extend;" xmlns:i="&ns_ai;" xmlns:graph="&ns_graphs;"
	 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="17px" height="21px"
	 viewBox="0 0 17 21" enable-background="new 0 0 17 21" xml:space="preserve">
<g>
			<g>
				<defs>
					<path id="SVGID_3_" d="M12.435,6.162c0,0.496-0.371,0.903-0.867,0.946l0.036,0.396c0.699-0.065,1.225-0.64,1.225-1.343H12.435z
						"/>
				</defs>
				<use xlink:href="#SVGID_3_"  overflow="visible" fill="#2156A3"/>
				<clipPath id="SVGID_4_">
					<use xlink:href="#SVGID_3_"  overflow="visible"/>
				</clipPath>

					<rect x="9.591" y="4.183" clip-path="url(#SVGID_4_)" fill="#2156A3" stroke="#518428" stroke-width="0.25" stroke-miterlimit="10" width="5.218" height="5.297"/>
				<use xlink:href="#SVGID_3_"  overflow="visible" fill="none" stroke="#2156A3" stroke-width="0.25" stroke-miterlimit="10"/>
			</g>
			<g>
				<defs>
					<path id="SVGID_5_" d="M11.17,4.509c0-0.357,0.289-0.646,0.647-0.646c0.357,0,0.647,0.289,0.647,0.646
						c0,0.356-0.29,0.648-0.647,0.648C11.459,5.157,11.17,4.866,11.17,4.509 M10.774,4.509c0,0.575,0.466,1.043,1.043,1.043
						c0.576,0,1.043-0.468,1.043-1.043c0-0.577-0.467-1.043-1.043-1.043C11.24,3.467,10.774,3.933,10.774,4.509"/>
				</defs>
				<use xlink:href="#SVGID_5_"  overflow="visible" fill="#2156A3"/>
				<clipPath id="SVGID_6_">
					<use xlink:href="#SVGID_5_"  overflow="visible"/>
				</clipPath>
					<rect x="8.796" y="1.489" clip-path="url(#SVGID_6_)" fill="#2156A3" stroke="#2156A3" stroke-width="0.25" stroke-miterlimit="10" width="6.041" height="6.043"/>
				<use xlink:href="#SVGID_5_"  overflow="visible" fill="none" stroke="#2156A3" stroke-width="0.25" stroke-miterlimit="10"/>
			</g>
</g>
</svg>
How to&Answers:

iText 7 in current version (7.1.10) processes stroke-width incorrectly if it’s specified in <use> elements (instead of in <path> elements which are referenced from that <use>). As a workaround you can specify stroke-width directly on path elements like that:

<svg version="1.1" id="Layer_1" 
	 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="17px" height="21px"
	 viewBox="0 0 17 21" enable-background="new 0 0 17 21" xml:space="preserve">
<g>
			<g>
				<defs>
					<path stroke-width="0.25" id="SVGID_3_" d="M12.435,6.162c0,0.496-0.371,0.903-0.867,0.946l0.036,0.396c0.699-0.065,1.225-0.64,1.225-1.343H12.435z
						"/>
				</defs>
				<use xlink:href="#SVGID_3_"  overflow="visible" fill="#2156A3"/>
				<clipPath id="SVGID_4_">
					<use xlink:href="#SVGID_3_"  overflow="visible"/>
				</clipPath>

					<rect x="9.591" y="4.183" clip-path="url(#SVGID_4_)" fill="#2156A3" stroke="#518428" stroke-width="0.25" stroke-miterlimit="10" width="5.218" height="5.297"/>
				<use xlink:href="#SVGID_3_"  overflow="visible" fill="none" stroke="#2156A3"  stroke-miterlimit="10"/>
			</g>
			<g>
				<defs>
					<path stroke-width="0.25" id="SVGID_5_" d="M11.17,4.509c0-0.357,0.289-0.646,0.647-0.646c0.357,0,0.647,0.289,0.647,0.646
						c0,0.356-0.29,0.648-0.647,0.648C11.459,5.157,11.17,4.866,11.17,4.509 M10.774,4.509c0,0.575,0.466,1.043,1.043,1.043
						c0.576,0,1.043-0.468,1.043-1.043c0-0.577-0.467-1.043-1.043-1.043C11.24,3.467,10.774,3.933,10.774,4.509"/>
				</defs>
				<use xlink:href="#SVGID_5_"  overflow="visible" fill="#2156A3"/>
				<clipPath id="SVGID_6_">
					<use xlink:href="#SVGID_5_"  overflow="visible"/>
				</clipPath>
					<rect x="8.796" y="1.489" clip-path="url(#SVGID_6_)" fill="#2156A3" stroke="#2156A3" stroke-width="0.25" stroke-miterlimit="10" width="6.041" height="6.043"/>
				<use xlink:href="#SVGID_5_"  overflow="visible" fill="none" stroke="#2156A3"  stroke-miterlimit="10"/>
			</g>
</g>
</svg>