Reputation: 1156
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
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
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
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:
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