使用 reportlab 将背景图像添加到 EPS

Add background image to EPS using reportlab

我正在使用 reportlab 创建图表。所以我想生成一个 eps 格式的文档,我想在其中添加带有图表的背景图像。当我尝试将背景图像添加到文档而不是作为背景出现时,它堆叠在图表下方。 当我 运行 for pdf 它工作时使用相同的逻辑。为了找出问题所在,我查看了 reportlab 库中的 renderPS.py 和 renderPDF.py,它们分别是用于创建 EPS 和 PDF 的文件。 在 renderPDF 中,我们使用 drawInlineImage() 在图表中生成内联图像,但 renderPS 不存在同样的情况。 路径是正确的,因为我可以对 PDF 执行相同的操作,并且图像大小也是正确的。我也无法将 drawInlineImage 函数添加到 renderPS.py。

import reportlab
from reportlab.graphics.charts.barcharts import VerticalBarChart
from reportlab.graphics.shapes import Drawing, _DrawingEditorMixin,Image
from reportlab.lib.colors import blue, PCMYKColor
from reportlab.graphics.charts.textlabels import LabelOffset
from reportlab.graphics.charts.legends import Legend

class VBarChartWLineBarLabels01(_DrawingEditorMixin,Drawing):
    def __init__(self,width=200,height=300,*args,**kw):
        Drawing.__init__(self,width,height,*args,**kw)
        self._add(self,VerticalBarChart(),name='chart',validate=None,desc=None)
        self._add(self,Legend(),name='legend',validate=None,desc=None)
        self.legend.columnMaximum    = 2
        self.legend.alignment='right'
        self.legend.dx               = 6
        self.legend.dy               = 6
        self.legend.dxTextSpace      = 5
        self.legend.deltay           = 10
        self.legend.strokeWidth      = 0
        self.legend.strokeColor      = None
        self.legend.subCols[0].minWidth = 75
        self.legend.subCols[0].align = 'left'
        self.legend.subCols[1].minWidth = 25
        self.legend.subCols[1].align = 'right'
        self.legend.boxAnchor      = 'c'
        self.legend.y              = 150
        self.legend.colorNamePairs = [(PCMYKColor(0,54,100,10,alpha=100), ('1985')),(PCMYKColor(100,60,0,50,alpha=100), ('2035'))]
        self.width       = 400
        self.legend.x              = 440
        self.chart.y = 30
        self.chart.x = 35
        self.chart.width=self.width-self.chart.x - 60
        self.chart.height=self.height-2*self.chart.y
        self.chart.valueAxis.drawGridLast = False
        self.chart.categoryAxis.drawGridLast = False
        self.chart.valueAxis.valueMax = 30
        self.chart.valueAxis.valueMin = 0
        self.chart.valueAxis.valueStep = 5
        self.chart.valueAxis.gridStrokeDashArray = None
        self.chart.valueAxis.gridEnd = self.width-60
        self.chart.valueAxis.gridStart = 35
        self.chart.valueAxis.visibleGrid =1
        self.chart.valueAxis.labelTextFormat = '%s%%'
        self.chart.valueAxis.maximumTicks = 20
        self.chart.valueAxis.minimumTickSpacing = 10
        self.chart.valueAxis.gridStrokeColor = PCMYKColor(0,0,0,25,alpha=85)
        self.chart.valueAxis.labels.dx = -10
        self.chart.barWidth = 3
        self.chart.groupSpacing = 5
        self.chart.data = [(15,17,14,12,15),(23,26,25,23,23)]
        self.chart.bars[0].fillColor   = PCMYKColor(0,54,100,10,alpha=100)
        self.chart.bars[1].fillColor   = PCMYKColor(100,90,0,50,alpha=100)
        self.chart.categoryAxis.categoryNames = ["England","Wales","Scotland","Northern\nIreland","UK"]
        self.chart.categoryAxis.strokeColor = PCMYKColor(0,0,0,25,alpha=85)
        self.chart.categoryAxis.labels.textAnchor='middle'
        self.chart.valueAxis.strokeColor = PCMYKColor(0,0,0,25,alpha=85)
        self.chart.bars.strokeColor = None
        self.chart.bars.strokeWidth = 0
        inPath = 'gepgraphic-allocation-piechart-map.jpg'
        img = Image(0, 0, self.chart.width, self.chart.height, inPath)
        self.chart.background = img
if __name__=="__main__": #NORUNTESTS
    VBarChartWLineBarLabels01().save(formats=['eps'],outDir='.',fnRoot=None)

这是上述问题的代码,它也适用于 pdf 格式的最后一行代码=['eps'] 我们需要用 pdf 替换 eps 并尝试 运行 文件生成的代码我希望它以 eps 格式生成相同的代码。

这是 reportlab 的错误,问题出在 renderPS.py 文件上。 这是您需要在函数 def drawImage(self, image):def drawImage(self, image, x1,y1, width=None,height=None):分别在class_PSRenderer(Renderer)PSCanvas中.

def drawImage(self, image):
        from reportlab.lib.utils import ImageReader
        im = ImageReader(image.path)
        self._canvas.drawImage(im._image,image.x,image.y,image.width,image.height) 


def drawImage(self, image, x1,y1, width=None,height=None): # Postscript Level2 version
        # select between postscript level 1 or level 2
        if self.PostScriptLevel==1:
            self._drawImageLevel1(image, x1,y1, width, height)
        elif self.PostScriptLevel==2:
            self._drawImageLevel2(image, x1, y1, width, height)
        else :
            raise ValueError('Unsupported Postscript Level %s' % self.PostScriptLevel)

注意:它适用于 reportlab 版本 2.5