def pdf(request):
if request.POST:
chatvs = Competirors.objects.get(id = int(request.POST.get('competitor', '')))
staff = Manager.objects.get(id = int(request.POST.get('manager', '')))
business = Field.objects.get(id = int(request.POST.get('filed', '')))
business = business.link.all().order_by('?')[0:3]
context = {
"chatvs" : chatvs,
"staff" : staff,
"business" : business,
}
tmpl = get_template('marketing/pdf.html', )
html = tmpl.render(context)
# create an API client instance
client = pdfcrowd.Client()
# convert a web page and store the generated PDF to a variable
pdf = client.convertHtml(html)
# set HTTP response headers
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="log.pdf"'
response["Cache-Control"] = "max-age=0"
response["Accept-Ranges"] = "none"
# send the generated PDF
response.write(pdf)
return response
def pdf(request):
if request.POST:
chatvs = Competirors.objects.get(id = int(request.POST.get('competitor', '')))
staff = Manager.objects.get(id = int(request.POST.get('manager', '')))
business = Field.objects.get(id = int(request.POST.get('filed', '')))
business = business.link.all().order_by('?')[0:3]
context = {
"chatvs" : chatvs,
"staff" : staff,
"business" : business,
}
tmpl = get_template('marketing/pdf.html', )
html = tmpl.render(context)
#Как-то тут надо прикрутить pdfkit?
from django.template.loader import get_template
from django.template import Context, RequestContext
import xhtml2pdf.pisa as pisa
import cStringIO as StringIO
import cgi
def render_to_pdf(template_src, context_dict, name_file):
template = get_template(template_src)
context = Context(context_dict)
html = template.render(context)
result = StringIO.StringIO()
pdf = pisa.CreatePDF(StringIO.StringIO(html.encode("UTF-8")), result, encoding='UTF-8')
if not pdf.err:
# return http.HttpResponse(result.getvalue(), mimetype='application/pdf')
response = HttpResponse(result.getvalue(), content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="' + name_file + '.pdf"'
return response
return http.HttpResponse(('We had some errors<pre>%s</pre>' % cgi.escape(html)))
def ajax_post(request, slug):
if request.POST:
query_text = request.POST["data"].encode("UTF-8")
title = Document.objects.get(slug=slug).title
return render_to_pdf('documents/entries1.html', {
'pagesize': 'A4',
'title': title,
'test': query_text
}, slug)
else:
return redirect('documents-create', slug=slug)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My Title</title>
<style type="text/css">
@page {
size: {{ pagesize }};
margin: 1cm;
@frame footer {
-pdf-frame-content: footerContent;
bottom: 0cm;
margin-left: 9cm;
margin-right: 9cm;
height: 1cm;
}
}
@font-face {
font-family: "Open Sans";
src: url("//mozorg.cdn.mozilla.net/media/fonts/OpenSans-Regular-webfont.eot?#iefix") format("embedded-opentype"), url("//mozorg.cdn.mozilla.net/media/fonts/OpenSans-Regular-webfont.woff") format("woff"), url("//mozorg.cdn.mozilla.net/media/fonts/OpenSans-Regular-webfont.ttf") format("truetype"), url("//mozorg.cdn.mozilla.net/media/fonts/OpenSans-Regular-webfont.svg#svgFontName") format("svg");
}
@font-face {
font-family: "Open Sans";
font-weight: 600;
src: url("//mozorg.cdn.mozilla.net/media/fonts/OpenSans-Semibold-webfont.eot?#iefix") format("embedded-opentype"), url("//mozorg.cdn.mozilla.net/media/fonts/OpenSans-Semibold-webfont.woff") format("woff"), url("//mozorg.cdn.mozilla.net/media/fonts/OpenSans-Semibold-webfont.ttf") format("truetype"), url("//mozorg.cdn.mozilla.net/media/fonts/OpenSans-Semibold-webfont.svg#svgFontName") format("svg");
}
@font-face {
font-family: "Open Sans";
font-weight: bold;
src: url("//mozorg.cdn.mozilla.net/media/fonts/OpenSans-Bold-webfont.eot?#iefix") format("embedded-opentype"), url("//mozorg.cdn.mozilla.net/media/fonts/OpenSans-Bold-webfont.woff") format("woff"), url("//mozorg.cdn.mozilla.net/media/fonts/OpenSans-Bold-webfont.ttf") format("truetype"), url("//mozorg.cdn.mozilla.net/media/fonts/OpenSans-Bold-webfont.svg#svgFontName") format("svg");
}
@font-face {
font-family: "Open Sans Light";
src: url("//mozorg.cdn.mozilla.net/media/fonts/OpenSans-Light-webfont.eot?#iefix") format("embedded-opentype"), url("//mozorg.cdn.mozilla.net/media/fonts/OpenSans-Light-webfont.woff") format("woff"), url("//mozorg.cdn.mozilla.net/media/fonts/OpenSans-Light-webfont.ttf") format("truetype"), url("//mozorg.cdn.mozilla.net/media/fonts/OpenSans-Light-webfont.svg#svgFontName") format("svg");
}
body {
color: #484848;
font-family: 'Open Sans',Arial,Helvetica,sans-serif;
font-size: 14px;
text-align: left;
line-height: 1.5em;
}
</style>
</head>
<body>
<div>
{{ mylist|safe }}
</div>
<div id="footerContent">
{%block page_foot%}
Page <pdf:pagenumber>
{%endblock%}
</div>
</body>
</html>
{% extends "pdf.html" %}
{% block title %}{{ title }}{% endblock %}
{% block content %}
{{ test|safe }}
{% endblock %}
{%block page_foot%}
я страница {{block.super}}
{%endblock%}