This page contains various examples of using the HTML to Image API in Python. The examples are complete and fully functional. Read more about how to convert HTML to Image in Python.
import pdfcrowd import sys try: # create the API client instance client = pdfcrowd.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d') # configure the conversion client.setOutputFormat('png') # run the conversion and write the result to a file client.convertUrlToFile('http://www.example.com', 'example.png') 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.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d') # configure the conversion client.setOutputFormat('png') # run the conversion and store the result into the "image" variable image = client.convertUrl('http://www.example.com') # at this point the "image" variable contains PNG raw data and # can be sent in an HTTP response, saved to a file, etc. 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.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d') # configure the conversion client.setOutputFormat('png') # create an output stream for the conversion result output_stream = open('example.png', 'wb') # run the conversion and write the result into the output stream client.convertUrlToStream('http://www.example.com', 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.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d') # configure the conversion client.setOutputFormat('png') # run the conversion and write the result to a file client.convertFileToFile('/path/to/MyLayout.html', 'MyLayout.png') 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.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d') # configure the conversion client.setOutputFormat('png') # run the conversion and store the result into the "image" variable image = client.convertFile('/path/to/MyLayout.html') # at this point the "image" variable contains PNG raw data and # can be sent in an HTTP response, saved to a file, etc. 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.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d') # configure the conversion client.setOutputFormat('png') # create an output stream for the conversion result output_stream = open('MyLayout.png', 'wb') # run the conversion and write the result into the output stream client.convertFileToStream('/path/to/MyLayout.html', 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.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d') # configure the conversion client.setOutputFormat('png') # run the conversion and write the result to a file client.convertStringToFile('<html><body><h1>Hello World!</h1></body></html>', 'HelloWorld.png') 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.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d') # configure the conversion client.setOutputFormat('png') # run the conversion and store the result into the "image" variable image = client.convertString('<html><body><h1>Hello World!</h1></body></html>') # at this point the "image" variable contains PNG raw data and # can be sent in an HTTP response, saved to a file, etc. 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.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d') # configure the conversion client.setOutputFormat('png') # create an output stream for the conversion result output_stream = open('HelloWorld.png', 'wb') # run the conversion and write the result into the output stream client.convertStringToStream('<html><body><h1>Hello World!</h1></body></html>', 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.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d') # configure the conversion client.setOutputFormat('png') client.setDebugLog(True) # run the conversion and write the result to a file client.convertFileToFile('/path/to/MyLayout.html', 'MyLayout.png') # 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
import pdfcrowd import sys try: # create the API client instance client = pdfcrowd.HtmlToPdfClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d') # configure the conversion client.setDataString("""{ "name": "World", "product": "Pdfcrowd API" }""") # run the conversion and write the result to a file client.convertStringToFile('Hello {{ name }} from {{ product }}', 'output.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.HtmlToPdfClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d') # configure the conversion client.setDataString("""<?xml version="1.0" encoding="UTF-8"?> <data> <name>World</name> <product>Pdfcrowd API</product> </data>""") # run the conversion and write the result to a file client.convertStringToFile('Hello {{ data.name }} from {{ data.product }}', 'output.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.HtmlToPdfClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d') # configure the conversion client.setDataString("""name: World product: Pdfcrowd API""") # run the conversion and write the result to a file client.convertStringToFile('Hello {{ name }} from {{ product }}', 'output.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.HtmlToPdfClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d') # configure the conversion client.setDataString("""name,product World,Pdfcrowd API""") # run the conversion and write the result to a file client.convertStringToFile('Hello {{ name }} from {{ product }}', 'output.pdf') except pdfcrowd.Error as why: # report the error sys.stderr.write('Pdfcrowd Error: {}\n'.format(why)) # rethrow or handle the exception raise