'Is it possible to change background color of PDF in python FPDF?
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")
Solution 1:[1]
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!
Solution 2:[2]
the best solution I have tried is the pdfplumber framework and here is the solution:
import pdfplumber
with pdfplumber.open(r'D:\examplepdf.pdf') as pdf:
first_page = pdf.pages[0]
print(first_page.extract_text())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | sowmya gowda |
Solution 2 | Ibrahim Ghozlane |