I am tring to create a pdf with a colored background in python using FPDF.
Is there a way to change the background color from white to some other color? Or do I have to insert colored cells to fill the entire 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")
You may instead add a colored image file on to the pdf page that you have created then, add the text on to same page.
Like example: create a new image file using Pillow package.
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')
Thank You!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With