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.
pip install pdfcrowd
Learn more about other install options.
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.
Sign up for a Free Trial
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)) # rethrow or handle the exception raise
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 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)) # rethrow or handle the exception raise
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)) # rethrow or handle the exception raise
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)) # rethrow or handle the exception raise
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 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)) # rethrow or handle the exception raise
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)) # rethrow or handle the exception raise
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)) # rethrow or handle the exception raise
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 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)) # rethrow or handle the exception raise
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)) # rethrow or handle the exception raise
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)) # rethrow or handle the exception raise
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 in your way
Conversion from an image to PDF. All setter methods return ImageToPdfClient object unless otherwise specified.
def __init__(self, user_name, api_key)
user_name
Your username at Pdfcrowd.
|
api_key
Your API key.
|
def convertUrl(self, url)
url
The address of the image to convert.
The supported protocols are http:// and https://.
|
def convertUrlToStream(self, url, out_stream)
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)
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)
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.
|
def convertFileToStream(self, file, out_stream)
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)
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)
data
The raw content to be converted.
|
def convertRawDataToStream(self, data, out_stream)
data
The raw content to be converted.
|
out_stream
The output stream that will contain the conversion output.
|
def convertRawDataToFile(self, data, file_path)
data
The raw content to be converted.
|
file_path
The output file path.
The string must not be empty.
|
Settings used for operations with images.
def setResize(self, resize)
resize
The resize percentage or new image dimensions.
Default: 100%
|
setResize("200%")
setResize("1024x768")
def setRotate(self, rotate)
rotate
The rotation specified in degrees.
Default: 0
|
def setDebugLog(self, debug_log)
debug_log
Set to True to enable the debug logging.
Default: False
|
def getDebugLogUrl(self)
def getRemainingCreditCount(self)
def getConsumedCreditCount(self)
def getJobId(self)
def getOutputSize(self)
def setTag(self, tag)
tag
A string with the custom tag.
|
def setHttpProxy(self, http_proxy)
http_proxy
The value must have format DOMAIN_OR_IP_ADDRESS:PORT.
|
setHttpProxy("myproxy.com:8080")
setHttpProxy("113.25.84.10:33333")
def setHttpsProxy(self, https_proxy)
https_proxy
The value must have format DOMAIN_OR_IP_ADDRESS:PORT.
|
setHttpsProxy("myproxy.com:443")
setHttpsProxy("113.25.84.10:44333")
def setUseHttp(self, use_http)
use_http
Set to True to use HTTP.
Default: False
|
def setUserAgent(self, user_agent)
user_agent
The user agent string.
Default: pdfcrowd_python_client/4.11.0 (http://pdfcrowd.com)
|
def setProxy(self, host, port, user_name, password)
host
The proxy hostname.
|
port
The proxy port.
|
user_name
The username.
|
password
The password.
|
def setRetryCount(self, retry_count)
retry_count
Number of retries wanted.
Default: 1
|