简体   繁体   English

使用 Reportlab 在 PDF 中保存 SVG 时出现不透明度问题

[英]Opacity issue when saving a SVG in a PDF with Reportlab

I have this SVG file:我有这个 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>
</svg>

If you save it as a b.svg file, and open it, you will see that there is some overlapping with opacity: the text ABC and DEF is readable .如果将其另存为b.svg文件,然后打开它,您会看到有一些不透明的重叠:文本ABCDEF是可读的

Now when I render it in a PDF file with the well-known method from Generating PDFs from SVG input :现在,当我使用从 SVG 输入生成 PDF的著名方法在 PDF 文件中渲染它时:

from svglib.svglib import svg2rlg
from reportlab.graphics import renderPDF
drawing = svg2rlg("b.svg")
renderPDF.drawToFile(drawing, "b.pdf")

then the opacity is lost in the PDF file, and the left part of the D letter of "DEF" is unreadable:那么不透明度在PDF文件中丢失,“DEF”的D字母左边部分不可读:

在此处输入图像描述

Question: How to include a SVG into a PDF and keep the opacity?问题:如何将 SVG 包含到 PDF 中并保持不透明?

A quick look at the svglib code shows that svglib does not parse the "opacity" attribute, but it does parse the "fill-opacity" and "stroke-opacity" attributes.快速查看 svglib 代码表明 svglib 不解析“opacity”属性,但它解析“fill-opacity”和“stroke-opacity”属性。 So changing your svg to因此,将您的 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>
</svg>

should do the trick.应该做的伎俩。 在此处输入图像描述

UPDATE更新

This seems to be an open issue since 2018自 2018 年以来,这似乎是一个悬而未决的问题

To long for a comment and the Kudos / Bonus should go to @zap for pointing me at rebuilding all the shapes to Polygons which worked significantly better than Rect.渴望发表评论,荣誉/奖金应该 go 到@zap ,因为他指出我将所有形状重建为多边形,这比 Rect 效果要好得多。

I still struggle to understand twice so as to get WkHtmlToPDF to give me a good scalar PDF but it did give me transparency with the following.我仍然很难理解两次,以便让 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>

Here are the transparent objects on a transparent background on a snowy day.这是下雪天透明背景上的透明对象。 Somewhere there must be a means to fix scaling but I did not find it yet and others have said similar.某处必须有一种方法来修复缩放,但我还没有找到它,其他人也说过类似的话。 在此处输入图像描述

The Idea was to keep a minimal as possible approach without any special external adjustments (though I did try many).想法是在没有任何特殊外部调整的情况下保持尽可能少的方法(尽管我确实尝试了很多)。 So the SVG was included as img in an HTML "square" block.因此,SVG 作为 img 包含在 HTML“方形”块中。

"test1.htm" “test1.htm”

<!DOCTYPE html>
<html>
  <head>
  <style>
    body {background: transparent;}
  </style>
  </head>
  <body>
    <img src="file:///wkhtmltox\bin\test1.svg" height=390px width=780px />
  </body>
</html>

and called directly并直接调用

wkhtmltopdf.exe --enable-local-file-access  --disable-smart-shrinking --no-background --no-pdf-compression -n "test1.htm" test1.pdf

In the spirit of maintaining opacity I included that aspect in the body style and external --no-background but they normally would not be required And finally I seem to have got scaling stable so here is the SVG in an A4 transparent page.本着保持不透明度的精神,我在主体样式和外部 --no-background 中包含了该方面,但通常不需要它们最后我似乎已经稳定了缩放比例所以这里是 A4 透明页面中的 SVG。 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM