"I'm working on creating a PDF from Python. As a beginner, a lot of errors are occurring in my code. This is my code:
class PDF(FPDF):
def __init__(self):
super().__init__()
self.header_text = ""
self.footer_text = ""
self.table_data = []
self.table_header = []
self.image_file = ""
self.pdf_title = ""
# Page header
def header(self):
if self.image_file:
self.image(self.image_file, 80, 25, 50)
if self.pdf_title:
self.set_font('Arial', 'B', 15)
self.cell(80)
self.cell(30, 10, self.pdf_title, 0, 0, 'C')
self.ln(50)
if self.header_text:
self.set_font('Arial', 'B', 12)
self.cell(0, 10, self.header_text, 0, 0, 'C')
self.ln(20)
# Add a table to the PDF
def add_table(self, header, data):
# Set the font and style for the table
self.set_font('Arial', '', 10)
# Calculate the width of each column and the height of each row
col_width = self.w * 0.9 / len(header)
row_height = self.font_size * 1.5
# Add the table header
for col_number, column_title in enumerate(header):
self.cell(col_width, row_height, str(column_title), border=1)
self.ln()
# Add the table data
self.set_font('Arial', '', 10)
for row in data:
for item in row:
self.cell(col_width, row_height, str(item), border=1)
self.ln()
# Add text to the PDF
def add_text(self):
self.add_page()
# Set the font and style for the text
self.set_font('Arial', '', 12)
# Add the text
self.multi_cell(0, 10, self.header_text)
self.ln()
# Add image to the PDF
def add_image(self):
self.add_page()
# Add the image
self.image(self.image_file, x=10, y=10, w=190)
self.ln()
# Page footer
def footer(self):
if self.footer_text:
# Position at 1.5 cm from bottom
self.set_y(-15)
# Arial italic 8
self.set_font('Arial', 'I', 8)
# Page number
self.cell(0, 10, self.footer_text + ' - Page ' + str(self.page_no()) + '/{nb}', 0, 0, 'C')
# Instantiation of inherited class
pdf = PDF()
# Printing lines on first page
pdf.add_page()
pdf.set_font('Times', '', 10)
# Switch statement
while True:
print("1. Add page header")
print("2. Add page footer")
print("3. Add table")
print("4. Add text")
print("5. Add image")
print("6. Generate PDF and exit")
choice = int(input("Enter your choice (1-6): "))
if choice == 1:
pdf.header_text = input("Enter the header text: ")
pdf.image_file = input("Enter the name of the image file (including extension): ")
pdf.pdf_title = input("Enter the title for the PDF: ")
elif choice == 2:
pdf.footer_text = input("Enter the footer text: ")
pdf.footer_page_number = input("Enter the page number for the footer: ")
elif choice == 3:
# Get user input for number of rows and columns
num_cols = int(input("Enter number of columns: "))
# Create empty header and data lists
header = []
data = []
# Accept table header from user
header_str = input("Enter table header separated by commas: ")
header = header_str.split(",")
# Accept table data from user
while True:
row_str = input("Enter table row separated by commas (enter 'done' when finished): ")
if row_str.lower() == "done":
break
else:
row = row_str.split(",")
data.append(row)
# Add the table to the PDF
pdf.add_table(header, data)
elif choice == 4:
text = input("Enter the text: ")
pdf.add_text()
elif choice == 5:
image_file = input("Enter the name of the image file (including extension): ")
pdf.add_image()`
elif choice == 6:
pdf.generate_pdf()
break
else:
print("Invalid choice. Please enter a number between 1 and 6.")
# Creating File
pdf.output('tuto2.pdf')"
**here is the error im facing**
"Enter the name of the image file (including extension): image.jpg
Traceback (most recent call last):
File "d:\SOFTWARE\PDF in Python\pdf1.py", line 242, in <module>
pdf.add_image()
File "d:\SOFTWARE\PDF in Python\pdf1.py", line 183, in add_image
File "C:\Users\aiena\AppData\Local\Programs\Python\Python311\Lib\site-packages\fpdf\fpdf.py", line 150, in wrapper
return fn(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\aiena\AppData\Local\Programs\Python\Python311\Lib\site-packages\fpdf\fpdf.py", line 990, in image
self.error('Unsupported image type: '+type)
File "C:\Users\aiena\AppData\Local\Programs\Python\Python311\Lib\site-packages\fpdf\fpdf.py", line 227, in error
raise RuntimeError('FPDF error: '+msg)
RuntimeError: FPDF error: Unsupported image type:"
please help me solve this error to add pictures in pdf simply
i expect from code to add image with user unput in pdf
There are some basic issues with your code related to adding image file.
The self.image_file
is initialized to empty string and never assigned the value provided by the user. This is fixed in below code by passing it as an argument.
The generate_pdf()
is an unknown method you are calling which is commented out and the output
method is sufficient to generate the PDF file.
The below code is tested for generation of a PDF image with a PNG file.
from fpdf import FPDF
class PDF(FPDF):
def __init__(self):
super().__init__()
self.header_text = ""
self.footer_text = ""
self.table_data = []
self.table_header = []
self.image_file = ""
self.pdf_title = ""
# Page header
def header(self):
if self.image_file:
self.image(self.image_file, 80, 25, 50)
if self.pdf_title:
self.set_font('Arial', 'B', 15)
self.cell(80)
self.cell(30, 10, self.pdf_title, 0, 0, 'C')
self.ln(50)
if self.header_text:
self.set_font('Arial', 'B', 12)
self.cell(0, 10, self.header_text, 0, 0, 'C')
self.ln(20)
# Add a table to the PDF
def add_table(self, header, data):
# Set the font and style for the table
self.set_font('Arial', '', 10)
# Calculate the width of each column and the height of each row
col_width = self.w * 0.9 / len(header)
row_height = self.font_size * 1.5
# Add the table header
for col_number, column_title in enumerate(header):
self.cell(col_width, row_height, str(column_title), border=1)
self.ln()
# Add the table data
self.set_font('Arial', '', 10)
for row in data:
for item in row:
self.cell(col_width, row_height, str(item), border=1)
self.ln()
# Add text to the PDF
def add_text(self):
self.add_page()
# Set the font and style for the text
self.set_font('Arial', '', 12)
# Add the text
self.multi_cell(0, 10, self.header_text)
self.ln()
# Add image to the PDF
def add_image(self, image_file):
self.add_page()
# Add the image
self.image_file=image_file
self.image(self.image_file, x=10, y=10, w=190)
self.ln()
# Page footer
def footer(self):
if self.footer_text:
# Position at 1.5 cm from bottom
self.set_y(-15)
# Arial italic 8
self.set_font('Arial', 'I', 8)
# Page number
self.cell(0, 10, self.footer_text + ' - Page ' + str(self.page_no()) + '/{nb}', 0, 0, 'C')
# Instantiation of inherited class
pdf = PDF()
# Printing lines on first page
pdf.add_page()
pdf.set_font('Times', '', 10)
# Switch statement
while True:
print("1. Add page header")
print("2. Add page footer")
print("3. Add table")
print("4. Add text")
print("5. Add image")
print("6. Generate PDF and exit")
choice = int(input("Enter your choice (1-6): "))
if choice == 1:
pdf.header_text = input("Enter the header text: ")
pdf.image_file = input("Enter the name of the image file (including extension): ")
pdf.pdf_title = input("Enter the title for the PDF: ")
elif choice == 2:
pdf.footer_text = input("Enter the footer text: ")
pdf.footer_page_number = input("Enter the page number for the footer: ")
elif choice == 3:
# Get user input for number of rows and columns
num_cols = int(input("Enter number of columns: "))
# Create empty header and data lists
header = []
data = []
# Accept table header from user
header_str = input("Enter table header separated by commas: ")
header = header_str.split(",")
# Accept table data from user
while True:
row_str = input("Enter table row separated by commas (enter 'done' when finished): ")
if row_str.lower() == "done":
break
else:
row = row_str.split(",")
data.append(row)
# Add the table to the PDF
pdf.add_table(header, data)
elif choice == 4:
text = input("Enter the text: ")
pdf.add_text()
elif choice == 5:
image_file = input("Enter the name of the image file (including extension): ")
pdf.add_image(image_file)
elif choice == 6:
#pdf.generate_pdf()
break
else:
print("Invalid choice. Please enter a number between 1 and 6.")
# Creating File
pdf.output('tuto2.pdf');