【发布时间】:2021-10-16 08:08:37
【问题描述】:
我正在尝试为我的 Django 项目生成 PDF。我用过pip install xhtml2pdf
PDF 是用纯文本生成的,但是当我在 pdf.html 中添加样式时,我得到了这个Exception Type: TypeError:
'<' not supported between instances of 'int' and 'NoneType'
这里是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
这里是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>
我正在寻找类似问题的答案,但我不知道从哪里开始修复。
我知道该错误与 pdf.html 中的 CSS 代码有关,但我不知道为什么会显示。当我删除 pdf 以纯文本显示的样式时。
这是回溯:
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'
我不确定css是否是视图导致错误的原因
我的问题:
这个错误的原因是什么,我该如何解决?
谢谢
【问题讨论】:
-
你能显示整个堆栈跟踪吗?
-
@bdbd 我已经用回溯更新了帖子,谢谢
-
尝试
remove.format(Info.id) -
@PrOgRaMmEr 同样的错误显示
-
尝试将您的 css 导出到外部文件。可能是您的 css 有一些难以解析的 xml 字符。您还可以使用 CDATA 部分
标签: python css python-3.x django xhtml2pdf