Legacy Pdfcrowd API v1 for Python
This is the documentation of the Python client library for the legacy Pdfcrowd API v1. We strongly recommend the new improved Pdfcrowd API v2 for new integrations.
Installation
Install the Pdfcrowd API client library for Python.
Getting Started
In the following examples, do not forget to replace "username" and "apikey" with your username and API key.
HTML to PDF Example Application
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("your_username", "your_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: {}'.format(why))
HTML to PDF in Django
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("your_username", "your_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"] = "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 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")
Error Handling
try: # .. # call the API except pdfcrowd.Error, why: # handle the error
API Reference
class pdfcrowd.Client
Provides access to the Pdfcrowd API from your Python applications.
Constructor
def __init__(self, username, apikey)
Conversion
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)
outstream can be any object having a
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.
Page Setup
def setPageWidth(self, value)
def setPageHeight(self, value)
def setPageMargins(self, top, right, bottom, left)
def setHorizontalMargin(self, value)
def setVerticalMargin(self, value)
Header and Footer
def setFooterHtml(self, html)
- %u - URL to convert.
- %p - The current page number.
- %n - Total number of pages.
def setFooterUrl(self, url)
def setHeaderHtml(self, html)
def setHeaderUrl(self, url)
def setHeaderFooterPageExcludeList(self, exclist)
Example: "1,-1" will not print the header and footer on the first and the last page.
def setPageNumberingOffset(self, offset)
Example: if set to "1" then the page numbering will start with 1 on the second page.
HTML options
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).
PDF options
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_FACING
def setPageMode(self, value)
FULLSCREEN- Full-screen mode.
def setInitialPdfZoomType(self, value)
FIT_WIDTHFIT_HEIGHTFIT_PAGE
def 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)
Watermark
def setWatermark(self, url, offet_x=0, offset_y=0)
def setWatermarkRotation(self, angle)
def setWatermarkInBackground(self, value)
Miscellaneous
def useSSL(self, use_ssl)
def numTokens(self)
def setMaxPages(self, npages)
def setFailOnNon200(self, value)
class pdfcrowd.Error
Derived from standard Exception class. It is thrown when an error
occurs.
Units
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".