Вы можете разделить свои файлы шаблонов для разных частей страницы.Например, у вас может быть базовый html, который содержит общие шаблоны, и у вас также будет ваш table.html
, содержащий данные, которые вы хотите преобразовать в pdf.
Вы можете использовать include
встроенныйШаблон тега, чтобы использовать его в другом шаблоне: см. документы здесь .Вы также можете проверить extends
templatetag, что очень удобно здесь .
Примерно так:
<code># base.html
{% include 'table.html' %}
# views.py
def render_to_pdf(request):
template_path = 'table.html' # Here is the template you want to convert
context = {'myvar': 'this is your template context'}
# Create a Django response object, and specify content_type as pdf
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="report.pdf"'
# find the template and render it.
template = get_template(template_path)
html = template.render(Context(context))
# create a pdf
pisaStatus = pisa.CreatePDF(
html, dest=response, link_callback=link_callback)
# if error then show some funy view
if pisaStatus.err:
return HttpResponse('We had some errors <pre>' + html + '
') ответ на запрос
Высм. создание PDF в xhtml2pdf документах здесь