I am trying to generate a PDF for my Django Project. I have used pip install xhtml2pdf
The PDF was generate with plain text but when I added style in the pdf.html I got this Exception Type: TypeError:
'<' not supported between instances of 'int' and 'NoneType'
Here is the views.py:
def admin_order_pdf(request,info_id):
info = get_object_or_404(Info, id=info_id)
template_path = 'businessplan/pdf.html'
context = {'info': info}
# Create a Django response object, and specify content_type as pdf
response = HttpResponse(content_type='application/pdf')
# response['Content-Disposition'] = 'attachment; filename="report.pdf"'
response['Content-Disposition'] = 'filename="order_{}.pdf"'.format(Info.id)
# find the template and render it.
template = get_template(template_path)
html = template.render(context)
# create a pdf
pisa_status = pisa.CreatePDF(html, dest=response)
# if error then show some funy view
if pisa_status.err:
return HttpResponse('We had some errors <pre>' + html + '</pre>')
return response
Here is the pdf.html
{% load static %}
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="{% static 'css/report.css' %}" />
<!-- <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">-->
<title>{{ info.businessName }}</title>
<style>
@charset "UTF-8";
@font-face {
font-family: Fira Sans;
font-weight: 400;
src: url('/static/img/FiraSans-Regular.otf'); }
@font-face {
font-family: Fira Sans;
font-style: italic;
font-weight: 400;
src: url('/static/img/FiraSans-Italic.otf'); }
@font-face {
font-family: Fira Sans;
font-weight: 300;
src: url('/static/img/FiraSans-Light.otf'); }
@font-face {
font-family: Fira Sans;
font-style: italic;
font-weight: 300;
src: url('/static/img/FiraSans-LightItalic.otf'); }
@font-face {
font-family: Fira Sans;
font-weight: bold;
src: url('/static/img/FiraSans-Bold.otf'); }
@page {
@top-left {
background: #fbc847;
content: counter(page);
height: 1cm;
text-align: center;
width: 1cm; }
--------------long lines of similar code css----------
html body article#competences section {
padding: .5cm 0; }
html body article#competences section#table-content::before {
background: url('/static/img/table-content.svg') no-repeat center #fbc847;
background-size: 50%;
content: '';
display: inline-block;
float: left;
height: 2cm;
margin-right: .5cm;
vertical-align: middle;
width: 2cm; }
.................. long lines of similar css
</style>
<meta name="description" content="Report example">
</head>
<body>
<article id="cover">
<h1>{{ info.businessName }}<br>
<small>Business Plan <br>
Created on: {{ info.date_posted }}
</small>
</h1>
<address>
{{ info.businessName }}
{{ info.businessAddress }}
</address>
I have search for the answer for similar questions but I don't know from where to start fixing.
I understand that the error is related to the CSS code in the pdf.html but I don't know why is it showing. When I remove the style the pdf shows with plain text.
Here is the traceback:
Traceback (most recent call last):
File "C:\Users\User\Desktop\consultation\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\User\Desktop\consultation\venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\User\Desktop\consultation\businessplan\views.py", line 49, in admin_order_pdf
pisa_status = pisa.CreatePDF(html, dest=response)
File "C:\Users\User\Desktop\consultation\venv\lib\site-packages\xhtml2pdf\document.py", line 104, in pisaDocument
context = pisaStory(src, path, link_callback, debug, default_css, xhtml,
File "C:\Users\User\Desktop\consultation\venv\lib\site-packages\xhtml2pdf\document.py", line 67, in pisaStory
pisaParser(src, context, default_css, xhtml, encoding, xml_output)
File "C:\Users\User\Desktop\consultation\venv\lib\site-packages\xhtml2pdf\parser.py", line 755, in pisaParser
context.parseCSS()
File "C:\Users\User\Desktop\consultation\venv\lib\site-packages\xhtml2pdf\context.py", line 469, in parseCSS
self.css = self.cssParser.parse(self.cssText)
File "C:\Users\User\Desktop\consultation\venv\lib\site-packages\xhtml2pdf\w3c\cssParser.py", line 454, in parse
src, stylesheet = self._parseStylesheet(src)
File "C:\Users\User\Desktop\consultation\venv\lib\site-packages\xhtml2pdf\w3c\cssParser.py", line 556, in _parseStylesheet
src, atResults = self._parseAtKeyword(src)
File "C:\Users\User\Desktop\consultation\venv\lib\site-packages\xhtml2pdf\w3c\cssParser.py", line 685, in _parseAtKeyword
src, result = self._parseAtIdent(src)
File "C:\Users\User\Desktop\consultation\venv\lib\site-packages\xhtml2pdf\w3c\cssParser.py", line 875, in _parseAtIdent
if semiIdx is not None and semiIdx < blockIdx:
TypeError: '<' not supported between instances of 'int' and 'NoneType'
I am not sure whether the css is the reason of the views is causing the error
My question:
what is the reason for this error and how could i fix it?
Thanks