Generate html to pdf using xhtml2pdf in django project
I have an HTML page containing multiple tables. When converting it to a PDF using xhtml2pdf, it works fine if only a few tables are expanded. However, if I expand more tables, I run into a size limitation where the PDF turns out blank if the content exceeds a certain size(30Mb).
Views.py
html template
How can I ensure that large PDFs download correctly?
Simply expand the capacity of the buffer that will be used by the parser. By default, the parser uses a buffer of 100 * 1024
bytes where 1024
bytes is equal to 1
kilobyte.
Since the document size could reach up to 20 * 1024 * 1024
bytes, pass this value to xhtml2pdf.pisa.CreatePDF()
like so:
xhtml2pdf.pisa.CreatePDF(
src=html.encode('UTF-8'),
dest=response,
encoding='UTF-8',
capacity=20*1024*1024,
show_error_as_pdf=True,
pdf_options=pdf_options
)