如何使用PDFTemplateResponse在我的基于类的视图中使用django-wkhtmltopdf添加页眉和页脚 [英] How to add headers and footers with django-wkhtmltopdf in my class based views with PDFTemplateResponse

查看:550
本文介绍了如何使用PDFTemplateResponse在我的基于类的视图中使用django-wkhtmltopdf添加页眉和页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一个带头文件的pdf,实际上我正在使用cygwin,我不知道我的代码是否有错,因为我以我的代码为例,这个例子使用django(wkhtmltopdf)创建PDF 。这是我的代码:



views.py

  from django。 view.generic import从wkhtmltopdf.views导入查看
导入PDFTemplateResponse

GenerateReportPdf(View):
def __init __(self):
self.template ='pdf_template.html '

def get(self,request):
...
response = PDFTemplateResponse(
request = request,
template = self.template,
filename ='report.pdf',
context = self.context,
show_content_in_browser = True,
cmd_options = {'margin-top':30,},
header_template ='header.html',

我的HTML代码可能很简单,它的如何使用PDF,而不是标题。

header.html:

  HTML> 
< head>
< / head>
< body>
< img src =http://www.someplaceintheworld/some.pngalt =some logo/>
< / body>
< / html>

我的urls.py:



<$ p $来自django.conf.urls导入模式的p> ,来自django.contrib.auth.decorators的url
导入login_required
从。导入视图

urlpatterns = patterns('',
url(r'^ $',login_required(views.GenerateReportPdf.as_view()),name ='index'),


解决方案

我在这里找到答案 https://github.com/wkhtmltopdf/wkhtmltopdf/issues/1645 只需要把doctype放到每个html 。

 <!DOCTYPE html> 
< html>
< head>
< / head>
< body>
< img src =http://www.someplaceintheworld/some.pngalt =some logo/>
< / body>
< / html>


I want to generate a pdf with headers, actually I'm working on cygwin andI don't know if something in my code is wrong or not, because I based my code in this examples Creating PDFs with django (wkhtmltopdf). This is my code:

views.py

from django.views.generic import View
from wkhtmltopdf.views import PDFTemplateResponse

GenerateReportPdf(View):
    def __init__(self):
        self.template = 'pdf_template.html'

    def get(self, request):
        ...
        response = PDFTemplateResponse(
                                       request=request,
                                       template=self.template,
                                       filename='report.pdf',
                                       context=self.context,
                                       show_content_in_browser=True,
                                       cmd_options={'margin-top': 30,},
                                       header_template='header.html',
                                       )

my html code may be simple but it shows something and I render a PDF successfuly but not the header.
header.html:

<html>
<head>
</head>
<body>
<img src="http://www.someplaceintheworld/some.png" alt="some logo" />
</body>
</html>

my urls.py:

from django.conf.urls import patterns, url
from django.contrib.auth.decorators import login_required
from . import views

urlpatterns = patterns('',
    url(r'^$', login_required(views.GenerateReportPdf.as_view()), name='index'),
)

解决方案

I found the answer here https://github.com/wkhtmltopdf/wkhtmltopdf/issues/1645 only need put the doctype to each html.

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<img src="http://www.someplaceintheworld/some.png" alt="some logo" />
</body>
</html>

这篇关于如何使用PDFTemplateResponse在我的基于类的视图中使用django-wkhtmltopdf添加页眉和页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆