This page serves as a guide for using the Pdfcrowd API to convert PDF into images in Python applications.
The API is designed for easy use and straightforward integration.
Below are Python examples to help you quickly get started with the API.
Explore our additional examples for more insights.
For a multi-page PDF, the API returns a ZIP archive containing an image for
each page. For a single-page PDF, the output is directly an image (e.g., PNG).
To determine the output format, use isZippedOutput().
You can enforce the use of a ZIP archive by using setForceZip().
importpdfcrowdimportsystry:# create the API client instanceclient=pdfcrowd.PdfToImageClient('demo','ce544b6ea52a5621fb9d55f8b542d14d')# If unsure whether the input is a single-page file, enforce ZIP outputclient.setOutputFormat('png')client.setForceZip(True)# run the conversion and write the result to a fileclient.convertFileToFile('/path/to/invoice.pdf','invoice.zip')exceptpdfcrowd.Erroraswhy:sys.stderr.write('Pdfcrowd Error: {}\n'.format(why))raise
importpdfcrowdimportsystry:# create the API client instanceclient=pdfcrowd.PdfToImageClient('demo','ce544b6ea52a5621fb9d55f8b542d14d')# If unsure whether the input is a single-page file, enforce ZIP outputclient.setOutputFormat('png')client.setForceZip(True)# run the conversion and write the result to a fileclient.convertUrlToFile('https://pdfcrowd.com/static/pdf/apisamples/invoice.pdf','invoice.zip')exceptpdfcrowd.Erroraswhy:sys.stderr.write('Pdfcrowd Error: {}\n'.format(why))raise
importpdfcrowdimportsystry:# create the API client instanceclient=pdfcrowd.PdfToImageClient('demo','ce544b6ea52a5621fb9d55f8b542d14d')# If unsure whether the input is a single-page file, enforce ZIP outputclient.setOutputFormat('png')client.setForceZip(True)# run the conversion and write the result to a fileclient.convertRawDataToFile(open('/path/to/hello_world.pdf','rb').read(),'invoice.zip')exceptpdfcrowd.Erroraswhy:sys.stderr.write('Pdfcrowd Error: {}\n'.format(why))raise
Authentication
To access the API, you will need to use your Pdfcrowd username and API
key. For initial testing, you may use the following demo credentials without
registering:
Username:demo
API key:ce544b6ea52a5621fb9d55f8b542d14d
To obtain your personal API credentials, consider starting a
free API trial
or purchasing the API license.
Error Handling
It is recommended that you implement error handling to catch errors the API
may return. Effective error handling is vital as it ensures application
stability and provides clearer diagnostics. See the example code below for
guidance on implementing error handling, and refer to this list of
status codes for more information.
try:# call the API exceptpdfcrowd.Erroraswhy:# print the errorsys.stderr.write('Pdfcrowd Error: {}\n'.format(why))# print the error codesys.stderr.write('Pdfcrowd Error Code: {}\n'.format(why.getCode()))# print the error messagesys.stderr.write('Pdfcrowd Error Message: {}\n'.format(why.getMessage()))
Troubleshooting
If you are receiving an error, refer to the
API Status Codes for more information.
Utilize
setDebugLog() and getDebugLogUrl()
to obtain detailed information about the conversion process, including
load errors, load times, browser console output, etc.