This is the documentation of the Python client library for the Pdfcrowd HTML to PDF API. Here are some sample PDFs generated by the API:
|
|
|
|
|
| Status report | JavaScript vector chart | Wikipedia page | Invoice | Newsletter |
Integration with your application is a matter of a few minutes:
$ pip install pdfcrowdOr you can download pdfcrowd-2.4.zip and run
$ unzip pdfcrowd-2.4.zip $ cd pdfcrowd-2.4/ $ python setup.py install
You can also check out the source on Github: pdfcrowd-python
In the following examples, do not forget to replace "username" and "apikey" with your username and API key.
The following code shows how to convert a web page, raw HTML code, and a local HTML file:
import pdfcrowd try: # create an API client instance client = pdfcrowd.Client("username", "apikey") # convert a web page and store the generated PDF into a pdf variable pdf = client.convertURI('http://www.google.com') # convert an HTML string and save the result to a file output_file = open('html.pdf', 'wb') html="<head></head><body>My HTML Layout</body>" client.convertHtml(html, output_file) output_file.close() # convert an HTML file output_file = open('file.pdf', 'wb') client.convertFile('/path/to/MyLayout.html', output_file) output_file.close() except pdfcrowd.Error, why: print 'Failed:', why
The following code shows how to generate PDF from a web page in a Django view function:
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.google.com") # set HTTP response headers response = HttpResponse(mimetype="application/pdf") response["Cache-Control"] = "no-cache" 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 also convert raw HTML code, just use the convertHtml() method instead of convertURI():
pdf = client.convertHtml("<head></head><body>My HTML Layout</body>")
The API lets you also convert a local HTML file:
pdf = client.convertFile("/path/to/MyLayout.html")
try: # .. # call the API except pdfcrowd.Error, why: # handle the error
Provides access to the Pdfcrowd API from your Python applications.
def __init__(self, username, apikey)
def convertHtml(self, html, outstream=None)
write(str) method.
If outstream is not provided then the return value is a string
containing the created PDF.
def convertFile(self, fpath, outstream=None)
write(str) method.
If outstream is not provided then the return value is a string
containing the created PDF.
def convertURI(self, url, outstream=None)
write(str) method.
If outstream is not provided then the return value is a string
containing the created PDF.
def setPageWidth(self, value)
def setPageHeight(self, value)
def setPageMargins(self, top, right, bottom, left)
def setHorizontalMargin(self, value)
def setVerticalMargin(self, value)
def setFooterHtml(self, html)
def setFooterUrl(self, url)
def setHeaderHtml(self, html)
def setHeaderUrl(self, url)
def setHeaderFooterPageExcludeList(self, exclist)
def setPageNumberingOffset(self, offset)
def enableImages(self, value)
def enableBackgrounds(self, value)
def setHtmlZoom(self, value)
def enableJavaScript(self, value)
def enableHyperlinks(self, value)
def setDefaultTextEncoding(self, value)
utf-8.
def usePrintMedia(self, value)
print CSS media type
is used (if available).
def setEncrypted(self, value)
def setUserPassword(self, pwd)
def setOwnerPassword(self, pwd)
def setNoPrint(self, value)
def setNoModify(self, value)
def setNoCopy(self, value)
def setPageLayout(self, value)
SINGLE_PAGECONTINUOUSCONTINUOUS_FACINGdef setPageMode(self, value)
FULLSCREEN - Full-screen mode.def setInitialPdfZoomType(self, value)
FIT_WIDTHFIT_HEIGHTFIT_PAGEdef setInitialPdfExactZoom(self, value)
def setPdfScalingFactor(self, value)
def setPageBackgroundColor(self, value)
def setTransparentBackground(self, value)
body {background-color:rgba(255,255,255,0.0);}
def setAuthor(self, author)
def setWatermark(self, url, offet_x=0, offset_y=0)
def setWatermarkRotation(self, angle)
def setWatermarkInBackground(self, value)
def useSSL(self, use_ssl)
def numTokens(self)
def setMaxPages(self, npages)
def setFailOnNon200(self, value)
Derived from standard Exception class. It is thrown when an error
occurs.
Page dimensions and margins can be specified in inches (in), millimeters (mm),
centimeters (cm), or points (pt). If no units are specified, points are
assumed. Examples: "210mm", "8.5in".