Coder Social home page Coder Social logo

mpessas / pywkher Goto Github PK

View Code? Open in Web Editor NEW

This project forked from strayduy/pywkher

0.0 2.0 1.0 39 MB

Use wkhtmltopdf in your Python project on Heroku

Home Page: http://www.codetalk.rs/2012/08/26/pywkher-wkhtmltopdf-for-python-on-heroku.html

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%

pywkher's Introduction

pywkher

wkhtmltopdf is a command line program that converts HTML to PDF using the WebKit rendering engine, as provided by Qt. Because it uses WebKit, you can use wkhtmltopdf to generate screenshots, or generate PDFs from Django or Rails or whatever, and the resulting PDFs will look awesome.

The typical install process includes downloading and compiling Qt, followed by downloading and installing wkhtmltopdf.

But you can't really download and compile packages inside a Heroku dyno, so you need to vendor in any external binaries you might want to use (that aren't Python packages that compile themselves). This package provides a simple way to include and use a wkhtmltopdf binary that has been compiled for the Heroku (Cedar stack) dyno environment.

Installation

Install it in the usual way:

pip install pywkher

Usage

One easy way of using wkhtmltopdf in your Python program is to use the included generate_pdf command. The generate_pdf command takes either a URL or an HTML document and returns a Python NamedTemporaryFile object referencing the generated PDF (which will be stored in the Heroku instance's temporary directory).

Here's an example of using the generate_pdf command to render a Django template as a PDF and return the resulting PDF as part of the HTTP response:

from os.path import basename
from wsgiref.util import FileWrapper

from django.http import HttpResponse
from django.template import RequestContext
from django.template.loader import get_template

from pywkher import generate_pdf


def return_a_pdf(request):
  template = get_template('my_awesome_template.html')
  html = template.render(RequestContext(request))
  pdf_file = generate_pdf(html=html)
  response = HttpResponse(FileWrapper(pdf_file), content_type='application/pdf')
  response['Content-Disposition'] = 'attachment; filename=%s.zip' % basename(pdf_file.name)
  response['Content-Length'] = pdf_file.tell()
  pdf_file.seek(0)
  return response

Usage in Development

If you want to test your app locally, obviously the binary that's compiled for Heroku isn't going to work, unless your development or test environment closely mimics Heroku's. However, the generate function will look for an environment variable named WKHTMLTOPDF_CMD and will only use the internally-bundled wkhtmltopdf binary if that environment variable is not set.

Therefore, if you're on a Mac, do something like:

  • brew install wkhtmltopdf (Homebrew required for this)
  • export WKHTLMTOPDF_CMD=wkhtmltopdf
  • python my_python_program.py (just an example)

Acknowledgements

Brad Phelan generated the binary that's included here as part of his wkhtmltopdf-heroku gem. It's thanks to his efforts that I didn't have to go through the whole Heroku vulcan build process myself.

pywkher's People

Contributors

mpessas avatar strayduy avatar

Watchers

 avatar  avatar

Forkers

nanomonkey

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.