Image to PDF API - Python SDK

Convert various image formats to PDF in Python using the Pdfcrowd API v2. The API is easy to use and the integration takes only a couple of lines of code.

Installation

Install the client library from PyPI
 $ pip install pdfcrowd

You can learn more about other install options here.

Authentication

Authentication is needed in order to use the Pdfcrowd API. The credentials used for accessing the API are your Pdfcrowd username and the API key. You can sign up for the Pdfcrowd API here.

Examples

Convert a local PNG file to a PDF file
import pdfcrowd
import sys

try:
    # create the API client instance
    client = pdfcrowd.ImageToPdfClient('your_username', 'your_apikey')

    # run the conversion and write the result to a file
    client.convertFileToFile('/path/to/logo.png', 'logo.pdf')
except pdfcrowd.Error as why:
    # report the error
    sys.stderr.write('Pdfcrowd Error: {}\n'.format(why))

    # handle the exception here or rethrow and handle it at a higher level
    raise
Convert a local PNG file to in-memory PDF
import pdfcrowd
import sys

try:
    # create the API client instance
    client = pdfcrowd.ImageToPdfClient('your_username', 'your_apikey')

    # create output file for conversion result
    output_file = open('logo.pdf', 'wb')

    # run the conversion and store the result into a pdf variable
    pdf = client.convertFile('/path/to/logo.png')

    # write the pdf the into the output file
    output_file.write(pdf)

    # close the output file
    output_file.close()
except pdfcrowd.Error as why:
    # report the error
    sys.stderr.write('Pdfcrowd Error: {}\n'.format(why))

    # handle the exception here or rethrow and handle it at a higher level
    raise
Convert a local PNG file and write the resulting PDF to an output stream
import pdfcrowd
import sys

try:
    # create the API client instance
    client = pdfcrowd.ImageToPdfClient('your_username', 'your_apikey')

    # create output stream for conversion result
    output_stream = open('logo.pdf', 'wb')

    # run the conversion and write the result into the output stream
    client.convertFileToStream('/path/to/logo.png', output_stream)

    # close the output stream
    output_stream.close()
except pdfcrowd.Error as why:
    # report the error
    sys.stderr.write('Pdfcrowd Error: {}\n'.format(why))

    # handle the exception here or rethrow and handle it at a higher level
    raise
Convert url with PNG file to a PDF file
import pdfcrowd
import sys

try:
    # create the API client instance
    client = pdfcrowd.ImageToPdfClient('your_username', 'your_apikey')

    # run the conversion and write the result to a file
    client.convertUrlToFile('http://pdfcrowd.com/static/images/logo.png', 'logo.pdf')
except pdfcrowd.Error as why:
    # report the error
    sys.stderr.write('Pdfcrowd Error: {}\n'.format(why))

    # handle the exception here or rethrow and handle it at a higher level
    raise
Convert url with PNG file to in-memory PDF
import pdfcrowd
import sys

try:
    # create the API client instance
    client = pdfcrowd.ImageToPdfClient('your_username', 'your_apikey')

    # create output file for conversion result
    output_file = open('logo.pdf', 'wb')

    # run the conversion and store the result into a pdf variable
    pdf = client.convertUrl('http://pdfcrowd.com/static/images/logo.png')

    # write the pdf the into the output file
    output_file.write(pdf)

    # close the output file
    output_file.close()
except pdfcrowd.Error as why:
    # report the error
    sys.stderr.write('Pdfcrowd Error: {}\n'.format(why))

    # handle the exception here or rethrow and handle it at a higher level
    raise
Convert url with PNG file and write the resulting PDF to an output stream
import pdfcrowd
import sys

try:
    # create the API client instance
    client = pdfcrowd.ImageToPdfClient('your_username', 'your_apikey')

    # create output stream for conversion result
    output_stream = open('logo.pdf', 'wb')

    # run the conversion and write the result into the output stream
    client.convertUrlToStream('http://pdfcrowd.com/static/images/logo.png', output_stream)

    # close the output stream
    output_stream.close()
except pdfcrowd.Error as why:
    # report the error
    sys.stderr.write('Pdfcrowd Error: {}\n'.format(why))

    # handle the exception here or rethrow and handle it at a higher level
    raise
Convert an in-memory PNG to a PDF file
import pdfcrowd
import sys

try:
    # create the API client instance
    client = pdfcrowd.ImageToPdfClient('your_username', 'your_apikey')

    # run the conversion and write the result to a file
    client.convertRawDataToFile(open('/path/to/logo.png', 'rb').read(), 'logo.pdf')
except pdfcrowd.Error as why:
    # report the error
    sys.stderr.write('Pdfcrowd Error: {}\n'.format(why))

    # handle the exception here or rethrow and handle it at a higher level
    raise
Convert an in-memory PNG to in-memory PDF
import pdfcrowd
import sys

try:
    # create the API client instance
    client = pdfcrowd.ImageToPdfClient('your_username', 'your_apikey')

    # create output file for conversion result
    output_file = open('logo.pdf', 'wb')

    # run the conversion and store the result into a pdf variable
    pdf = client.convertRawData(open('/path/to/logo.png', 'rb').read())

    # write the pdf the into the output file
    output_file.write(pdf)

    # close the output file
    output_file.close()
except pdfcrowd.Error as why:
    # report the error
    sys.stderr.write('Pdfcrowd Error: {}\n'.format(why))

    # handle the exception here or rethrow and handle it at a higher level
    raise
Convert an in-memory PNG and write the resulting PDF to an output stream
import pdfcrowd
import sys

try:
    # create the API client instance
    client = pdfcrowd.ImageToPdfClient('your_username', 'your_apikey')

    # create output stream for conversion result
    output_stream = open('logo.pdf', 'wb')

    # run the conversion and write the result into the output stream
    client.convertRawDataToStream(open('/path/to/logo.png', 'rb').read(), output_stream)

    # close the output stream
    output_stream.close()
except pdfcrowd.Error as why:
    # report the error
    sys.stderr.write('Pdfcrowd Error: {}\n'.format(why))

    # handle the exception here or rethrow and handle it at a higher level
    raise
Get info about the current conversion
import pdfcrowd
import sys

try:
    # create the API client instance
    client = pdfcrowd.ImageToPdfClient('your_username', 'your_apikey')

    # configure the conversion
    client.setDebugLog(True)

    # run the conversion and write the result to a file
    client.convertFileToFile('/path/to/logo.png', 'logo.pdf')
    
    # print URL to the debug log
    print('Debug log url: {}'.format(client.getDebugLogUrl()))
    
    # print the number of available conversion credits in your account
    print('Remaining credit count: {}'.format(client.getRemainingCreditCount()))
    
    # print the number of credits consumed by the conversion
    print('Consumed credit count: {}'.format(client.getConsumedCreditCount()))
    
    # print the unique ID of the conversion
    print('Job id: {}'.format(client.getJobId()))
    
    # print the size of the output in bytes
    print('Output size: {}'.format(client.getOutputSize()))
except pdfcrowd.Error as why:
    # report the error
    sys.stderr.write('Pdfcrowd Error: {}\n'.format(why))

    # handle the exception here or rethrow and handle it at a higher level
    raise

Tips & Tricks

API Reference - class ImageToPdfClient

Conversion from an image to PDF.

Constructor

def __init__(self, user_name, api_key)
Constructor for the Pdfcrowd API client.
Parameter Description Default
user_name
Your username at Pdfcrowd.
api_key
Your API key.

 

Conversion Input

def convertUrl(self, url)
Convert an image.
Parameter Description Default
url
The address of the image to convert.
The supported protocols are http:// and https://.
Returns
  • byte[] - Byte array containing the conversion output.
def convertUrlToStream(self, url, out_stream)
Convert an image and write the result to an output stream.
Parameter Description Default
url
The address of the image to convert.
The supported protocols are http:// and https://.
out_stream
The output stream that will contain the conversion output.
def convertUrlToFile(self, url, file_path)
Convert an image and write the result to a local file.
Parameter Description Default
url
The address of the image to convert.
The supported protocols are http:// and https://.
file_path
The output file path.
The string must not be empty.
def convertFile(self, file)
Convert a local file.
Parameter Description Default
file
The path to a local file to convert.
The file can be either a single file or an archive (.tar.gz, .tar.bz2, or .zip).
The file must exist and not be empty.
Returns
  • byte[] - Byte array containing the conversion output.
def convertFileToStream(self, file, out_stream)
Convert a local file and write the result to an output stream.
Parameter Description Default
file
The path to a local file to convert.
The file can be either a single file or an archive (.tar.gz, .tar.bz2, or .zip).
The file must exist and not be empty.
out_stream
The output stream that will contain the conversion output.
def convertFileToFile(self, file, file_path)
Convert a local file and write the result to a local file.
Parameter Description Default
file
The path to a local file to convert.
The file can be either a single file or an archive (.tar.gz, .tar.bz2, or .zip).
The file must exist and not be empty.
file_path
The output file path.
The string must not be empty.
def convertRawData(self, data)
Convert raw data.
Parameter Description Default
data
The raw content to be converted.
Returns
  • byte[] - Byte array with the output.
def convertRawDataToStream(self, data, out_stream)
Convert raw data and write the result to an output stream.
Parameter Description Default
data
The raw content to be converted.
out_stream
The output stream that will contain the conversion output.
def convertRawDataToFile(self, data, file_path)
Convert raw data to a file.
Parameter Description Default
data
The raw content to be converted.
file_path
The output file path.
The string must not be empty.

 

Image operations

Settings used for operations with images.

def setResize(self, resize)
Resize the image.
Parameter Description Default
resize
The resize percentage or new image dimensions.
100%
Returns
  • ImageToPdfClient - The converter object.
Examples:
  • setResize("200%")
  • setResize("1024x768")
def setRotate(self, rotate)
Rotate the image.
Parameter Description Default
rotate
The rotation specified in degrees.
0
Returns
  • ImageToPdfClient - The converter object.

 

Miscellaneous

def setDebugLog(self, debug_log)
Turn on the debug logging. Details about the conversion are stored in the debug log. The URL of the log can be obtained from the getDebugLogUrl method.
Parameter Description Default
debug_log
Set to True to enable the debug logging.
False
Returns
  • ImageToPdfClient - The converter object.
def getDebugLogUrl(self)
Get the URL of the debug log for the last conversion.
Returns
  • string - The link to the debug log.
def getRemainingCreditCount(self)
Get the number of conversion credits available in your account.
The returned value can differ from the actual count if you run parallel conversions.
The special value 999999 is returned if the information is not available.
Returns
  • int - The number of credits.
def getConsumedCreditCount(self)
Get the number of credits consumed by the last conversion.
Returns
  • int - The number of credits.
def getJobId(self)
Get the job id.
Returns
  • string - The unique job identifier.
def getOutputSize(self)
Get the size of the output in bytes.
Returns
  • int - The count of bytes.

 

API Client Options

def setUseHttp(self, use_http)
Specifies if the client communicates over HTTP or HTTPS with Pdfcrowd API.
Parameter Description Default
use_http
Set to True to use HTTP.
False
Returns
  • ImageToPdfClient - The converter object.
def setUserAgent(self, user_agent)
Set a custom user agent HTTP header. It can be usefull if you are behind some proxy or firewall.
Parameter Description Default
user_agent
The user agent string.
pdfcrowd_python_client/4.3.3 (http://pdfcrowd.com)
Returns
  • ImageToPdfClient - The converter object.
def setProxy(self, host, port, user_name, password)
Specifies an HTTP proxy that the API client library will use to connect to the internet.
Parameter Description Default
host
The proxy hostname.
port
The proxy port.
user_name
The username.
password
The password.
Returns
  • ImageToPdfClient - The converter object.
def setRetryCount(self, retry_count)
Specifies the number of retries when the 502 HTTP status code is received. The 502 status code indicates a temporary network issue. This feature can be disabled by setting to 0.
Parameter Description Default
retry_count
Number of retries wanted.
1
Returns
  • ImageToPdfClient - The converter object.

 

Error Handling

try: 
    # call the API 
except pdfcrowd.Error as why: 
    # print error
    sys.stderr.write('Pdfcrowd Error: {}\n'.format(why))

    # print just error code
    sys.stderr.write('Pdfcrowd Error Code: {}\n'.format(why.getCode()))

    # print just error message
    sys.stderr.write('Pdfcrowd Error Message: {}\n'.format(why.getMessage()))

    # or handle the error by your way

Troubleshooting