Render HTML to Pdf in Django Site

Render HTML to PDF in Django site

Try the solution from Reportlab.

Download it and install it as usual with python setup.py install

You will also need to install the following modules: xhtml2pdf, html5lib, pypdf with easy_install.

Here is an usage example:

First define this function:

import cStringIO as StringIO
from xhtml2pdf import pisa
from django.template.loader import get_template
from django.template import Context
from django.http import HttpResponse
from cgi import escape


def render_to_pdf(template_src, context_dict):
template = get_template(template_src)
context = Context(context_dict)
html = template.render(context)
result = StringIO.StringIO()

pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)
if not pdf.err:
return HttpResponse(result.getvalue(), content_type='application/pdf')
return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))

Then you can use it like this:

def myview(request):
#Retrieve data or whatever you need
return render_to_pdf(
'mytemplate.html',
{
'pagesize':'A4',
'mylist': results,
}
)

The template:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<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;
}
}
</style>
</head>
<body>
<div>
{% for item in mylist %}
RENDER MY CONTENT
{% endfor %}
</div>
<div id="footerContent">
{%block page_foot%}
Page <pdf:pagenumber>
{%endblock%}
</div>
</body>
</html>

Hope it helps.

Convert template html in django to pdf and download it

Try this.

...This document explains how to output PDF files dynamically using Django views. This is made possible by the excellent, open-source ReportLab Python PDF library.

https://docs.djangoproject.com/en/4.0/howto/outputting-pdf/

https://www.reportlab.com/dev/install/open_source_installation/

How to turn fully rendered html page using Django into PDF

Firstly, according to the Model-View-Controller paradigm, a downloadable PDF is a view - so review the Django PDF documentation.

If you are looking at including a Google map in the PDF, there are 3 options:

  1. Render on the client, have them print to PDF. This relies on the user actually knowing how to do this.
  2. Use a headless browser to pre-render the page using Javascript, return it to Django, then export as PDF. This will be difficult depending on the server.
  3. Use the Google Maps Static Map API to generate the image and use that in a tool like Pisa . This has fewer features than the full API, but is ideal for making printable static images. Do this.

As stated in the bottom of the Django Docs on generating PDFs, Pisa works by taking the HTML page and then creates a PDF from that, I've used it before and it means you can use Django templates to construct the page, then instead of returning it, run it through a HTML-to-PDF processor. You would need to just capture this generated PDF and attach it to an email as required.


Addendum to match the edit.

The above still stands, except for the part about PDFs being views. Regardless, the Static Maps API should give you the images need to include in the Raw HTML you transform into the PDF.

Rendering HTML to PDF with images included Django

fromHTML is used to get text from the HTML to form a PDF. Try using html2canvas js library instead.

<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.5.0-beta4/html2canvas.min.js"></script>

You can create a canvas image and add it to the PDF with code like this,

$(document).ready(function() {
$("#pdfDownloader").click(function() {

html2canvas($("#renderMe")).then(canvas => {
const contentDataURL = canvas.toDataURL('image/png')
let pdf = new jspdf('p', 'pt', 'a4', true); // A4 size page of PDF
var positionX = 0;
var positionY = 0;
pdf.addImage(contentDataURL, 'PNG', positionY, positionY, undefined, undefined)
pdf.save('thisMotion.pdf'); // Generated PDF
});

});
})

This will get you a PDF just like the HTML rendered on screen.

Python3 Django - HTML to PDF

You could use Weasyprint. You could easily render directly.

You could do something like that:

    html = HTML(string=htmlstring)
main_doc = html.render()
pdf = main_doc.write_pdf()
return HttpResponse(pdf, content_type='application/pdf')

To render your Django view to HTML, you could simply use the shortcut render_to_string(self.template_name, context, context_instance=RequestContext(self.request))

Be aware, when using This with a Synchronous Webserver/WSGI Server ALL requests will be blocked until the PDF is rendered. So consider using an ASYNC Worker.



Related Topics



Leave a reply



Submit