Академический Документы
Профессиональный Документы
Культура Документы
Normally I don’t have any trouble using PyPDF2 and FPDF when
generating/creating combined pdfs. However, in Anvil I simply cannot figure out
how the “file type” when working with pdf files in memory in FPDF and PyPDF2
when importing/exporting to/from google drive. I have tried several
combinations and now I am feeling pretty daft, so I am reaching out to the
community. The code is in a server module and called from the client.
import io
import PyPDF2
from fpdf import FPDF
# trying PyPDF2
@anvil.server.callable
def test_pdf_read():
file = app_files.test_app.get("document.pdf")
pdfFileObj = open(file.get_bytes(), 'rb')
# trying FPDF
@anvil.server.callable
def make_pdf():
pdf = FPDF()
pdf.add_page()
pdf.set_font('Arial', 'B', 16)
1 of 6 7/5/20, 9:11 PM
Creating and manipulating pdf files via PyPDF2 a... http://webcache.googleusercontent.com/search?...
EDIT 1: After rereading the FPDF docs I have changed the above code that
should making a pdf via FPDF. In order to output the pdf correctly it should be
encoded with:
.encode(‘latin-1’)
https://pyfpdf.readthedocs.io/en/latest/reference/output/index.html
but when I include the encoding in the commented line, the server throws the
following error:
Hi Niels,
# 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')
2 of 6 7/5/20, 9:11 PM
Creating and manipulating pdf files via PyPDF2 a... http://webcache.googleusercontent.com/search?...
me know.
Does that solve your problem? Let me know if anything remains unclear.
- Thanks, Ian.
Pass values from a form to a data table, and make PDF report
neriksen85 February 19, 2018, 9:28pm #3
Thank’s for the description, Ian. However, in order to fully close the circle, could
you also give a very short example of how to implement the
PyPDF2.PdfFileWriter(), which is needed in order to overlay one pdf file onto
another? I have entered some code below that doesn’t work for me.
:exploding_head:
pdf1Reader = PyPDF2.PdfFileReader(io_file)
pdf2Reader = PyPDF2.PdfFileReader(io_file2)
pdfWriter = PyPDF2.PdfFileWriter()
pdfOutputFile = io.BytesIO()
pdfWriter.write(pdfOutputFile)
pdfOutputFile.close()
io_file.close()
io_file2.close()
I have gone through the documentation for the io module a couple of times
without being able to get the full grasp of it. If anyone reading this knows of
some litterature/articles that better helps me understand io.BytesIO I would
greatly appreciate a link.
Thanks
3 of 6 7/5/20, 9:11 PM
Creating and manipulating pdf files via PyPDF2 a... http://webcache.googleusercontent.com/search?...
pdf_bytes = app_files.foo.get_bytes()
#Create a BytesIO object that can be used directly as a file
memoryFile = io.BytesIO(pdf_bytes)
pdfFileObj = PyPDF2.PdfFileReader(memoryFile,‘rb’)
if not pdfFileObj.isEncrypted:
print(pdfReader.numPages)
else:
print(‘error!’)
1 Like
daviesian February 20, 2018, 10:24am #6
Hi Niels,
Your code very nearly works as-is, the only thing you need to tweak is the use of
BytesIO for writing. In particular, you need to call seek(0) on it before you can
read its contents. Here is an example which joins two PDF app files:
@anvil.server.callable
def join_pdf():
file1 = io.BytesIO(app_files.foo1.get_bytes())
file2 = io.BytesIO(app_files.foo2.get_bytes())
pdf1Reader = PyPDF2.PdfFileReader(file1)
pdf2Reader = PyPDF2.PdfFileReader(file2)
pdfWriter = PyPDF2.PdfFileWriter()
out_file = io.BytesIO()
pdfWriter.write(out_file)
out_file.seek(0)
out_file_media = BlobMedia("application/pdf", out_file.read(), name="out.pdf")
app_files.folder.create_file("out.pdf", out_file_media)
I hope that helps - I think we’ve now covered all the angles. Just let me know if
there’s anything else you need.
1 Like
neriksen85 February 20, 2018, 3:37pm #7
4 of 6 7/5/20, 9:11 PM
Creating and manipulating pdf files via PyPDF2 a... http://webcache.googleusercontent.com/search?...
pdfFileObj = PyPDF2.PdfFileReader(memoryFile,‘rb’)
I encountered some minor issues with font etc. in the output file. However these
issues went away and resulted in a perfect output pdf, when I removed the
parameter, ‘rb’:
pdfFileObj = PyPDF2.PdfFileReader(memoryFile)
1 Like
nameispalmer June 7, 2018, 9:21am #8
I had no experience with PyPDF nor FPDF before have started sorting out with
Anvil. Then I needed to optimize a simple website in order to make it possible
for users to open up specific PDF forms, fill and sign them. I connected FPDF
but didn’t figure out what the error actually I saw. I sorted that out in a dead
simple fashion, adding links to this editor instead https://pump-it-up-job-
form.pdffiller.com/ and tracking its metrics. But nevertheless to process this
thing on the website only would be a better option, so thanks to all for these
explanations
1 Like
shaun February 24, 2020, 5:57pm #9
media_object = anvil.pdf.render_form('MyForm')
The return value is a media object, meaning you can download it:
anvil.media.download(media_object)
1 Like
Home
Categories
FAQ/Guidelines
Terms of Service
Privacy Policy
5 of 6 7/5/20, 9:11 PM
Creating and manipulating pdf files via PyPDF2 a... http://webcache.googleusercontent.com/search?...
6 of 6 7/5/20, 9:11 PM