I am trying to print invoice in PDF format in Django I used xhtml2pdf to convert HTML doc. to PDF but when I try to run my code it gives me this error :
LayoutError at /invoice/ Flowable <PmlTable@0x1D09C899130 7 rows x 5 cols(tallest row 841)> with cell(0,0) containing '<PmlKeepInFrame at 0x1d09b77d670> size=x'(538.5826771653543 x 5893.228346456693), tallest cell 841.9 points, too large on page 2 in frame 'body'(538.5826771653543 x 785.19685039370
this is in my views.py
from django.http import HttpResponse
from django.views.generic import View
from booking.utils import render_to_pdf
from django.template.loader import get_template
class GeneratePDF(View):
def get(self, request, *args, **kwargs):
template = get_template('invoice.html')
context = {
"invoice_id": 1234,
"customer_name": "John Cooper",
"amount": 1399.99,
"today": "Today",
}
html = template.render(context)
pdf = render_to_pdf('invoice.html', context)
if pdf:
response = HttpResponse(pdf, content_type='application/pdf')
filename = "Invoice_%s.pdf" %("12341231")
content = "inline; filename='%s'" %(filename)
download = request.GET.get("download")
if download:
content = "attachment; filename='%s'" %(filename)
response['Content-Disposition'] = content
return response
return HttpResponse("Not found")
and this is my urls.py
from django.urls import path
from booking.views import GeneratePDF
app_name = 'booking'
urlpatterns = [
path('invoice/', GeneratePDF.as_view(), name ="invoice"),
]
I got the answer
xhtml2pdf is not able to split table cells that are larger than the available space.
To work around it you may define what should happen in this case.
The -pdf-keep-in-frame-mode
can be one of: “error”, “overflow”, “shrink”, “truncate”
where “shrink”
is the default value.
table { -pdf-keep-in-frame-mode: shrink;}