ivan7707
ivan7707

Reputation: 1156

How to turn fully rendered html page using Django into PDF

I have created an app that uses a Google maps api to geocode an address and add markers to a map. It also has some interaction with Postgis in my views. Everything works well there.

What I have having trouble with is how to create a PDF from the fully rendered HTML file so that all of the context data and google maps can be rendered into a pdf. I need the pdf to be emailed by the person who is interacting with the web app to do the geocoding. So, the end result is to pdf the results and attach to email.

I have looked at Reportlab, and it can only create pdfs through a view (works great for that, but does not work for my needs).

I have also looked at wkhtmltopdf, at first glance it seems that it can only be rendered from a view, unless I am missing something.

Q: Is it possible to create a pdf from a rendered webpage (that will include the Google maps that are created client side)? A simple example would be much appreciated, if it is possible.

Thanks

edit: Adding workflow to try to clarify where the issue is

  1. On the index page there is a textbox where someone would add an address on submit, there is a Google geocoder function that sends the latitude and longitude in the form data back to the view
  2. In the view, the latitude and longitude is used to find 3 closest locations (in my Postgis database) and is rendered to the HTML result page
  3. On the result page, 3 separate Google maps are created using Google maps API

I need to turn the fully rendered web page into a PDF. Reportlab (for example) is called after point 2, before the webpage is rendered in point 3, so it cannot create a PDF that would include those 3 Google maps.

Upvotes: 1

Views: 3306

Answers (2)

Daniel F
Daniel F

Reputation: 340

Look into if HTML2Canvas (http://html2canvas.hertzen.com/) will work on your page. If so, you can then export the canvas as a png and then send if to your server.

Your server could convert that png to a pdf and then return that pdf, so the user would be prompted to download it.

Another option is xhtml2pdf (http://www.xhtml2pdf.com/), but it might well not work depending on how the map is rendered. It is worth a try though.

Upvotes: 1

user764357
user764357

Reputation:

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.

Upvotes: 2

Related Questions