Hi Niels,
The following examples work for me in Python 3:
# trying PyPDF2
@anvil.server.callable
def test_pdf_read():
pdf_bytes = app_files.foo.get_bytes()
# Create a BytesIO object that can be used directly as a file
file = io.BytesIO(pdf_bytes)
pdfReader = PyPDF2.PdfFileReader(file)
print(pdfReader.numPages)
# trying FPDF
@anvil.server.callable
def make_pdf():
pdf = FPDF()
pdf.add_page()
pdf.set_font('Arial', 'B', 16)
pdf.cell(40, 10, 'Hello World!')
byte_string = pdf.output(dest='S').encode('latin-1')
# Create an Anvil Media object from the encoded bytes.
pdf_media = BlobMedia("application/pdf", byte_string, name="new_file.pdf")
# Create a Google Drive file from the Media object
app_files.folder.create_file("HelloWorld.pdf", pdf_media)
I do get an encoding error if I try the .encode('latin-1')
function call in Python 2, but I didn’t investigate that further. If you have a requirement for Python 2, let me know.
Does that solve your problem? Let me know if anything remains unclear.
- Thanks, Ian.