0

my application needs to mail out reports to clients and hence i needed an effective method to convert the dynamic template into a pdf report (including images generated via chart.js). I have tried pdfkit but it needs a URL (on which it most likely performs a GET, but then the template generates a report after a few AJAX calls, so the GET is going to just return the plain vanilla page with some filters) and it DOESN'T include images (which i am guessing i can solve by converting the chart image into a png using dataToURL and saving on the server).

The only option i see here is to save all the data, generated dynamically, along with the html tags and recreate the file on the server and then convert to pdf. I am sure there's a better solution. Apologies if this appears basic, but i am not a programmer by profession.

Vikram Murthy
  • 293
  • 6
  • 17

3 Answers3

5

Django has a few options for outputting PDFs, most flexible of which is ReportLab.

However, to just render a Django template to PDF while passing context data, Weasyprint/xhtml2pdf are dead simple. Below is a view an example using the earlier xhtml2pdf library. It's a standard Django view.

To be clear, all of these libraries take a Django template, render it, and return a PDF. There's limitations (Pisa, for example, has only a handful of CSS parameters it can render). Regardless, take a look at these three; At least one will do exactly what you need.

from django_xhtml2pdf.utils import generate_pdf

def myview(request):
    resp = HttpResponse(content_type='application/pdf')
    dynamic_variable = request.user.some_special_something
    context = {'some_context_variable':dynamic_variable}
    result = generate_pdf('my_template.html', file_object=resp, context=context)
    return result
Ian Price
  • 7,416
  • 2
  • 23
  • 34
  • 1
    Thanks Vikram! I use Pisa in production all over the place, it's a very reliable solution. If this answer helped, please vote up or mark as the answer, thanks! – Ian Price Jun 26 '16 at 15:58
  • 2
    Sure Ian ..don't have enough repo to vote up but i ticked the "tick" mark at your answer ..i hope that means i accept this as the answer ! – Vikram Murthy Jun 26 '16 at 17:31
1

You can use a paid library i.e pdfcrowd, which converts a webpage into pdf. Like this..

First install-

pip install pdfcrowd

Then use the library -

import pdfcrowd
from django.http import HttpResponse

def generate_pdf_view(request):
    try:
        # create an API client instance
        client = pdfcrowd.Client("username", "apikey")

        # convert a web page and store the generated PDF to a variable
        pdf = client.convertURI("http://www.yourwebpage.com")

         # set HTTP response headers
        response = HttpResponse(mimetype="application/pdf")
        response["Cache-Control"] = "max-age=0"
        response["Accept-Ranges"] = "none"
        response["Content-Disposition"] = "attachment; filename=google_com.pdf"

        # send the generated PDF
        response.write(pdf)
    except pdfcrowd.Error, why:
        response = HttpResponse(mimetype="text/plain")
        response.write(why)
    return response

You can get the username and APIKEY by signup here-http://pdfcrowd.com/pricing/api/

Ashok Joshi
  • 428
  • 4
  • 13
  • Thanks Ashok but i am bootstrapping my venture and the pricing is a little heavy on me pocket (given the number of reports i ll have to be printing) ..appreciate the help ! – Vikram Murthy Jun 26 '16 at 14:43
0

Option A: Scraping

You could use something like PhantomJS or CasperJS to navigate and scrape the HTML page.

Option B: Generation

You could use something like PyPDF as suggested here.

Which option is better?

Scraping saves you from having to maintain two templates. With Generation you get more control by the fact that you're writing specifically for PDF and that you implicitly have two templates.

Anthony Astige
  • 1,919
  • 1
  • 13
  • 18
  • Thanks Anthony .. i am veering towards B :) given that Ian below also points to something similar ..appreciate the help – Vikram Murthy Jun 26 '16 at 14:44