admin管理员组

文章数量:1400974

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.

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.

Share Improve this question asked Jun 26, 2016 at 5:36 Vikram MurthyVikram Murthy 3236 silver badges19 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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

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.")

         # 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_.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./pricing/api/

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.

本文标签: javascriptConvert Django template to pdfStack Overflow