====== Generating PDF within django using wkhtmltopdf ====== I need to generate some PDF reports within django. I have been looking for library for pdf printing but did not find open source (free) one with features I needed. AS I already have view which generates html I was looking for tool which can transform html to pdf. So I decide to use [[http://code.google.com/p/wkhtmltopdf/|wkhtmltopdf]] tool which miss one required feature (repeating table header at every new page) but have good looking generated documents. For installation of wkhtmltopdf please see official instructions. I have used static version (it is bigger but has better features). In django I have used following code for generating pdf. This could give you idea how to do it yourself. #my functions to get data and to render html data = get_offer_data(id) html = offer_html(request, data) #preparing wkhtmltopdf command to run using pipes #WKHTML2PDF_COMMAND is string set in project settings # an looks like /path/to/command #Popen has little confusing way to accept command arguments #Last two "-" are importnat as this is the way to tell #wkhtmltopdf to use pipes for input and output #This is how this command will look in shell # >wkhtmltopdf --footer-right [page]/[toPage] --print-media-type --encoding UTF-8 - - wkhtml2pdf = subprocess.Popen((WKHTML2PDF_COMMAND, "--footer-right", "[page]/[toPage]", "--print-media-type", "--encoding", "UTF-8", "-", "-"), stdin=subprocess.PIPE, stdout=subprocess.PIPE) wkdata = wkhtml2pdf.communicate(html.encode('utf8')) pdf = wkdata[0]; response = HttpResponse(mimetype='application/pdf') response['Content-Disposition'] = 'attachment; filename=Offer_' + data['offer'].number + '.pdf' response.write(pdf) return response :!: If you have long tables (which are extending over more pages) be careful as they will probably be broken between pages.