How to add headers and footers with django-wkhtmltopdf in my class based views with PDFTemplateResponse

我的梦境 提交于 2019-12-23 04:58:07

问题


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'),
)

回答1:


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>


来源:https://stackoverflow.com/questions/33512009/how-to-add-headers-and-footers-with-django-wkhtmltopdf-in-my-class-based-views-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!