How to make a render generated html page to pdf in Django?

Can't have 3 day to find the adequate solution for translating html to pdf in django, so that it looked adequately.

1) the Decision on the APi pdfcrowd

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


Knocks all css/static objects turns out poorly. In the end, does not work.

2) the Decision on the reportlab - actually have a canvas to draw the whole pdf file. Normal solution, but in my case it is not suitable because we need a solution for generating a large number of different pdf.

3) the Decision on Pisa - and no more of it. The service was closed, the packages are removed, and the creators are now doing other things.

4) the Decision on PDFKit/wkhtmltopdf - take for their work, the url/file/string - but I have not found a way to pass them in the page render, so he worked and saved to pdf.

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) 

 #Somehow it is necessary to fasten pdfkit?


===

Is there a way to make it work 4 way, or I missed something?
July 2nd 19 at 16:55
5 answers
July 2nd 19 at 16:57
Last year on one of their projects was faced with this task. I shoveled a lot and came to the conclusion that there is not a single library that would renderil HTML to PDF with 100% accuracy. The result is used xhtml2pdf (Pisa heir), killing a little time on the specifics of his work. I think we should look towards the heir xhtml2pdf - WeasyPrint.
July 2nd 19 at 16:59
But you can specify styles for print and for these styles to generate PDF. Correctly I understand you ?
I support. Typeset for print - looks the same everywhere. - elenora_Carroll commented on July 2nd 19 at 17:02
a possible example of code to make it clearer? - Shanie_Armstrong98 commented on July 2nd 19 at 17:05
: What kind of example you give if we have no example of your site.
https://www.smashingmagazine.com/2011/11/how-to-se...
Here with pictures. - Kayley.Lueilwitz62 commented on July 2nd 19 at 17:08
:
Terras @Terras
a possible example of code to make it clearer?

I typeset in millimeters. Only the mini came out the same on screen and on the printer. But there are many subtleties. Google on css media print. - elenora_Carroll commented on July 2nd 19 at 17:11
July 2nd 19 at 17:01
Last time I solved this problem a little differently, to prepare a special template for js to pdfmake.org/playground.html there he pdf is generated on the client side.
July 2nd 19 at 17:03
Hello everyone, about 3 years ago, also had to solve such a problem, solved it using xhtml2pdf. As is the case now with this package, I can not say.
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-index', slug=slug)


Template parent pdf.html



 <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.5 em;
}

</style>


the <div>
 {{ mylist|safe }}
</div>
 <div id="footerContent">
 {%block page_foot%}
 Page <pdf:pagenumber>
{%endblock%}
</pdf:pagenumber></div>


The template in which to pass data entries1.html
{% extends "pdf.html" %}

{% block title %}{{ title }}{% endblock %}

{% block content %}
 {{ test|safe }}
{% endblock %}

{%block page_foot%}
 page {{block.super}}
{%endblock%}
Closed. - elenora_Carroll commented on July 2nd 19 at 17:06
July 2nd 19 at 17:05

Find more questions by tags DjangoPDF