java - SVG 文件无法转换为 Itext7 中的预期图像
问题描述
我有一个 SVG 文件,当我SvgConverter.convertToImage
将其转换为图像并添加到 PDF 文档时,输出图像与预期不符。图像笔触大小变得比 SVG 文件大得多。我的svg
插件版本是 7.1.9。如何解决此类问题?预期的图表是什么:
pdf 中的实际输出是:
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);
svg内容:
<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>
解决方案
当前版本 (7.1.10) 中的 iText 7如果在元素中指定(而不是在从 that 引用的元素中),则处理stroke-width
不正确。作为一种解决方法,您可以直接在这样的元素上指定:<use>
<path>
<use>
stroke-width
path
<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>
推荐阅读
- javascript - 防止 createElement 创建多个相同的图像
- java - RecyclerView 使应用程序崩溃
- javascript - js给元素添加特定样式的标题
- docker - 无法将我的应用程序打开到公共网络
- r - 使用 dplyr 计算时间戳上的累积总和
- docker - Docker:无法挂载卷
- pandas - 将随机数据中的数据从 10, 100 替换为 size(4,10)
- javascript - 找出有多少数组值具有绝对值
- serverless-framework - 在`serverless.yml`中部署调用外部JS函数的警告
- python - Python每次在for循环中将变量重置为默认值