简体   繁体   English

Python ReportLab 设置背景色

[英]Python ReportLab set background colour

I've had a quick look around the web and haven't been able to find a way to set the background colour of a PDF when generating one using ReportLab in Python.在 ZA7F5F35426B927411FC9231B56382173 中使用 ReportLab 生成 PDF 时,我快速浏览了 web 的背景颜色。 How does one set the background colour?如何设置背景颜色?

I figured out a makeshift way of doing it. 我想出了一种临时的做法。 Assuming you have an A4-sized page (which is the default), you can simply specify your own shape like so: 假设您有一个A4大小的页面(这是默认设置),您可以简单地指定自己的形状,如下所示:

from reportlab.lib.colors import HexColor
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.units import cm
pdf = Canvas("bgColour.pdf")
pdf.setFillColor(HexColor("#99b0e7"))
path = pdf.beginPath()
path.moveTo(0*cm,0*cm)
path.lineTo(0*cm,30*cm)
path.lineTo(25*cm,30*cm)
path.lineTo(25*cm,0*cm)
#this creates a rectangle the size of the sheet
pdf.drawPath(path,True,True)
pdf.showPage()
pdf.save()

Of course, if you wanted a more robust method, you could substitute the exact measurements I have specified for variables which you can change dynamically eg: 当然,如果你想要一个更健壮的方法,你可以替换我为变量指定的精确测量,你可以动态改变,例如:

x = 25
y = 30
path.moveTo(0*cm,0*cm)
path.lineTo(0*cm,y*cm)
path.lineTo(x*cm,y*cm)
path.lineTo(x*cm,0*cm)

Hopefully this helps anyone who finds themselves in a similar situation as I did! 希望这有助于任何发现自己处于类似情况的人!

Instead of using complex methods use this simple trick: 而不是使用复杂的方法使用这个简单的技巧:

 import reportlab

from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
from reportlab.lib.pagesizes import letter
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import inch
from reportlab.lib.units import cm

def background(c):
    c.setFillColorRGB(1,0,0)
    c.rect(5,5,652,792,fill=1)


c=canvas.Canvas("Background",pagesize=letter)
c.setTitle("Background")
background(c)
c.showPage()
c.save()

Just draw a rectangle with coordinates same as that of pdf page and fill with colour of choice. 只需绘制一个坐标与pdf页面相同的矩形,并填充选择的颜色。

The docs are unfortunately somewhat cryptic at times.不幸的是,这些文档有时有些神秘。 But for anyone who stumbles across this and still uses report lab (like me)但是对于任何偶然发现这一点并且仍然使用报告实验室的人(比如我)

This is an xy grid.这是一个 xy 网格。 You need to think from lef to right and from bottom to top.你需要从左到右,从下到上思考。 Hopefully this helps to make sense of things.希望这有助于理解事物。

For a top banner, about 10cm in height:对于顶部横幅,大约 10 厘米高:

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import cm

A4_height = A4[1]
A4_width = A4[0]
    
left_padding = 0
bottom_padding = A4_height - 10*cm
width = A4_width
height = A4_height - bottom_padding

canvas = Canvas()
canvas.setFillColorRGB(0,0,0)
canvas.rect(left_padding, bottom_padding, width, height, fill=1)

For a complete black page:对于完整的黑页:

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import cm

A4_height = A4[1]
A4_width = A4[0]
    
left_padding = 0
bottom_padding = 0
width = A4_width
height = A4_height

canvas = Canvas()
canvas.setFillColorRGB(0,0,0)
canvas.rect(left_padding, bottom_padding, width, height, fill=1)

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

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