我正在尝试创建一个具有彩色背景的pdf在python中使用FPDF。
有没有办法把背景色从白色改成其他颜色?或者我必须插入彩色单元格来填充整个pdf?
from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
pdf.set_fill_color(248,245,235)
pdf.cell(200, 40,'Colored cell', 0, 1, 'C', fill=True)
pdf.output("test.pdf")
发布于 2020-06-21 14:55:04
您可以将彩色图像文件添加到已创建的pdf页面,然后将文本添加到同一页面。
例如:使用Pillow包创建一个新的图像文件。
from fpdf import FPDF
from PIL import Image
pdf = FPDF()
pdf.add_page()
# creating a new image file with light blue color with A4 size dimensions using PIL
img = Image.new('RGB', (210,297), "#afeafe" )
img.save('blue_colored.png')
# adding image to pdf page that e created using fpdf
pdf.image('blue_colored.png', x = 0, y = 0, w = 210, h = 297, type = '', link = '')
# setting font and size and writing text to cell
pdf.set_font("Arial", size=12)
pdf.cell(ln=200, h=40, align='L', w=0, txt="Hello World", border=0,fill = False)
pdf.output("test.pdf", 'F')
谢谢!
https://stackoverflow.com/questions/59264554
复制相似问题