gpt4 book ai didi

python - 使用 wkhtmltopdf Django 生成 pdf

转载 作者:太空宇宙 更新时间:2023-11-03 14:23:29 28 4
gpt4 key购买 nike

我正在尝试使用这个库创建一个 pdf,实现最简单的 de docs 示例

像这样使用 url 并直接从模板呈现

url(r'^pdf/$', PDFTemplateView.as_view(template_name='my_template.html',
filename='my_pdf.pdf'), name='pdf'),

这是模板文件的html

<!DOCTYPE html>
<html>
<head>
<title>Hello Wold</title>
</head>
<body>
<p>My First Pdf</p>
</body>
</html>

但是当我尝试 url错误是这样的

AttributeError at /pdf/ 'NoneType' object has no attribute 'endswith'

我该如何解决

这是回溯

Traceback: File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response 139. response = response.render() File "/usr/local/lib/python2.7/dist-packages/django/template/response.py" in render 105. self.content = self.rendered_content File "/usr/local/lib/python2.7/dist-packages/wkhtmltopdf/views.py" in rendered_content 123. delete=(not debug) File "/usr/local/lib/python2.7/dist-packages/wkhtmltopdf/views.py" in render_to_temporary_file 78. content = make_absolute_paths(content) File "/usr/local/lib/python2.7/dist-packages/wkhtmltopdf/utils.py" in make_absolute_paths 151. if not x['root'].endswith('/'):

Exception Type: AttributeError at /pdf/ Exception Value: 'NoneType' object has no attribute 'endswith'

环境:

请求方式:GET

Django Version: 1.6.2
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'south',
'rest_framework',
'cian',
'django_extensions',
'wkhtmltopdf')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')

这是 django-wkhtmltopd 错误的截图

enter image description here

最佳答案

发生此错误是因为 wkhtmltopdf 使用位于 settings.py 的 STATIC_ROOT 中的路径生成 pdf 文件。尝试为 STATIC_ROOT 分配一个值并进行测试。

STATIC_ROOT = 'path.to.your.static.directory'

关于python - 使用 wkhtmltopdf Django 生成 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24071290/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com