我有这个SVG文件。
<svg version="1.1" viewBox="0 0 390 390" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<line class="arrow" opacity="0.5" stroke="#15781B" stroke-linecap="butt" stroke-width="9.0" x1="307.5" x2="279.60592002787337" y1="352.5" y2="296.7118400557468" />
<polygon class="arrow" fill="#15781B" opacity="0.5019607843137255" points="264.51246117974983,266.5249223594996 264.5124611797498,304.25856947980856 294.69937887599696,289.165110631685" />
<text fill="white" font-size="11" font-weight="bold" opacity="0.5" x="264.0" y="291.0">DEF</text>
<line class="arrow" opacity="0.5019607843137255" stroke="#15781B" stroke-linecap="butt" stroke-width="9.0" x1="262.5" x2="262.5" y1="307.5" y2="255.75" />
<polygon class="arrow" fill="#15781B" opacity="0.5" points="262.5,222.0 245.625,255.75 279.375,255.75" />
<text fill="white" font-size="11" font-weight="bold" opacity="0.5" x="252.0" y="249.0">ABC</text>
如果你把它保存为b.svg
文件,并打开它,你会看到有一些不透明的重叠:文本ABC
和DEF
。是可读的.
现在,当我用众所周知的方法在PDF文件中渲染它时,从从SVG输入生成PDF :
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPDF
drawing = svg2rlg("b.svg")
renderPDF.drawToFile(drawing, "b.pdf")
那么PDF文件中的不透明度就会丢失,"DEF "的D字母的左边部分就无法阅读。
问题。如何将SVG纳入PDF并保持其不透明度?
2 个回答
0 人赞同
快速查看一下svglib的代码就会发现,svglib并不解析 "不透明度 "属性,但它会解析 "填充-不透明度 "和 "描边-不透明度 "属性。因此,将你的svg改为
<svg version="1.1" viewBox="0 0 390 390" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<line class="arrow" fill-opacity="0.5" stroke-opacity="0.5" stroke="#15781B" stroke-linecap="butt" stroke-width="9.0" x1="307.5" x2="279.60592002787337" y1="352.5" y2="296.7118400557468" />
<polygon class="arrow" fill="#15781B" fill-opacity="0.5019607843137255" stroke-opacity="0.5019607843137255" points="264.51246117974983,266.5249223594996 264.5124611797498,304.25856947980856 294.69937887599696,289.165110631685" />
<text fill="white" font-size="11" font-weight="bold" fill-opacity="0.5" stroke-opacity="0.5" x="264.0" y="291.0">DEF</text>
<line class="arrow" fill-opacity="0.5019607843137255" stroke-opacity="0.5019607843137255" stroke="#15781B" stroke-linecap="butt" stroke-width="9.0" x1="262.5" x2="262.5" y1="307.5" y2="255.75" />
<polygon class="arrow" fill="#15781B" fill-opacity="0.5" stroke-opacity="0.5" points="262.5,222.0 245.625,255.75 279.375,255.75" />
<text fill="white" font-size="11" font-weight="bold" fill-opacity="0.5" stroke-opacity="0.5" x="252.0" y="249.0">ABC</text>
should do the trick.
这似乎是一个open issue since 2018
0 人赞同
漫长的评论和嘉奖/奖金应该给@zap在我的指点下,我把所有的形状都重建为多边形,这比Rector的效果好得多。
I still struggle为了让WkHtmlToPDF给我一个好的结果,我需要理解两次。标量PDF,但它确实给我带来了以下的透明度。
<svg version="1.1" viewBox="0 0 390 390" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<polygon class="arrow" fill="#15781B" fill-opacity="0.5" stroke-opacity="0.5" points="275.75,298.6 283.6,294.6 312,350.5 303.5,354.5" />
<polygon class="arrow" fill="#15781B" fill-opacity="0.5" stroke-opacity="0.5" points="264.5125,266.5249 264.5125,304.25856 294.6994,289.1651" />
<text fill="white" font-size="11" font-weight="bold" fill-opacity="0.5" stroke-opacity="0.5" x="264.0" y="291.0">DEF</text>
<polygon class="arrow" fill="#15781B" fill-opacity="0.5" stroke-opacity="0.5" points="258,255.75 267,255.75 267,307.5 258,307.5" />
<polygon class="arrow" fill="#15781B" fill-opacity="0.5" stroke-opacity="0.5" points="262.5,222.0 245.625,255.75 279.375,255.75" />
<text fill="white" font-size="11" font-weight="bold" fill-opacity="0.5" stroke-opacity="0.5" x="250" y="250.0">ABC</text>
这里是雪天的透明背景上的透明物体。
某处一定有解决缩放问题的方法,但我还没有找到,其他人也说过类似的话。
我们的想法是尽量保持最小的方法,不需要任何特殊的外部调整(尽管我尝试了很多)。因此,SVG被作为img包含在一个HTML "方形 "块中。
"test1.htm"
<!DOCTYPE html>
<style>
body {background: transparent;}
</style>
</head>
<img src="file:///wkhtmltox\bin\test1.svg" height=390px width=780px />
</body>
</html>
并直接调用