Hello guys,
I’m trying to embed a picture into a pdf using fpdf. The image is stored in data tables in a table called assets. When I get the image from my database and use self.image(image_url_here)
in fpdf and pass the url of the image media object, I get Unsupported image format or file not found exception. If I get an image from the web and pass the url in self.image(image_url_here)
, it works fine.
Please find below a snippet of my server code.
import anvil.email
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables
import anvil.secrets
import anvil.server
import anvil.media
import anvil.pdf
from fpdf import FPDF
import random
from io import BytesIO
import datetime
class REPORT(FPDF):
def __init__(self):
FPDF.__init__(self)
def generate(self):
try:
self.add_page()
self.set_text_color(0, 0, 0)
#### Method one, using image from app_tables.
image = app_tables.assets.get(name="signature_image")['media']
#print(image.url)
#formatting url to remove extra tokens in url
url = ""
for letter in image.url:
if letter == '?':
break
url += letter
self.image(url) #This generates an error message
#### End of method one ######
##### Method two, using an image url from the web.
#Insert any url here that links to a picture, this works
self.image("https://i.ibb.co/Br6mLby/england.png")
#### End of method two ########
except Exception as err:
print("Error in report class:", err)
### End of CYCLE_REPORT class
@anvil.server.callable
def generate_report_test():
report = REPORT()
report.generate()
try:
pdf_bytes = report.output(dest='S').encode('latin-1')
pdf_report = anvil.BlobMedia("application/pdf", pdf_bytes, name="Appointment Report.pdf")
except Exception as err:
print("Error in generate report fxn:", err)
return pdf_report
And this is the client code that calls the server function
import anvil.media
#Calling server function
pdf = anvil.server.call('generate_report_test')
anvil.media.download(pdf)
Any ideas why the image from my database doesn’t work?
Thanks.