HTML to Image / Python Examples

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.

Basic examples
Template rendering examples
Django examples
Flask examples

Basic examples

Webpage to PNG file

import pdfcrowd
import sys

try:
    # Create an API client instance.
    client = pdfcrowd.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d')

    # Configure the conversion.
    client.setOutputFormat('png')

    # Run the conversion and save the result to a file.
    client.convertUrlToFile('http://www.example.com', 'example.png')

except pdfcrowd.Error as why:
    sys.stderr.write('PDFCrowd Error: {}\n'.format(why))
    raise

Webpage to in-memory PNG

import pdfcrowd
import sys

try:
    # Create an API client instance.
    client = pdfcrowd.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d')

    # Configure the conversion.
    client.setOutputFormat('png')

    # Run the conversion and store the result in 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:
    sys.stderr.write('PDFCrowd Error: {}\n'.format(why))
    raise

Webpage to PNG stream

import pdfcrowd
import sys

try:
    # Create an 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 to the output stream.
    client.convertUrlToStream('http://www.example.com', output_stream)

    # Close the output stream.
    output_stream.close()

except pdfcrowd.Error as why:
    sys.stderr.write('PDFCrowd Error: {}\n'.format(why))
    raise

HTML file to PNG file

import pdfcrowd
import sys

try:
    # Create an API client instance.
    client = pdfcrowd.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d')

    # Configure the conversion.
    client.setOutputFormat('png')

    # Run the conversion and save the result to a file.
    client.convertFileToFile('/path/to/MyLayout.html', 'MyLayout.png')

except pdfcrowd.Error as why:
    sys.stderr.write('PDFCrowd Error: {}\n'.format(why))
    raise

HTML file to in-memory PNG

import pdfcrowd
import sys

try:
    # Create an API client instance.
    client = pdfcrowd.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d')

    # Configure the conversion.
    client.setOutputFormat('png')

    # Run the conversion and store the result in 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:
    sys.stderr.write('PDFCrowd Error: {}\n'.format(why))
    raise

HTML file to PNG stream

import pdfcrowd
import sys

try:
    # Create an 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 to the output stream.
    client.convertFileToStream('/path/to/MyLayout.html', output_stream)

    # Close the output stream.
    output_stream.close()

except pdfcrowd.Error as why:
    sys.stderr.write('PDFCrowd Error: {}\n'.format(why))
    raise

HTML string to PNG file

import pdfcrowd
import sys

try:
    # Create an API client instance.
    client = pdfcrowd.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d')

    # Configure the conversion.
    client.setOutputFormat('png')

    # Run the conversion and save the result to a file.
    client.convertStringToFile('<html><body><h1>Hello World!</h1></body></html>', 'HelloWorld.png')

except pdfcrowd.Error as why:
    sys.stderr.write('PDFCrowd Error: {}\n'.format(why))
    raise

HTML string to in-memory PNG

import pdfcrowd
import sys

try:
    # Create an API client instance.
    client = pdfcrowd.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d')

    # Configure the conversion.
    client.setOutputFormat('png')

    # Run the conversion and store the result in 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:
    sys.stderr.write('PDFCrowd Error: {}\n'.format(why))
    raise

HTML string to PNG stream

import pdfcrowd
import sys

try:
    # Create an 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 to 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:
    sys.stderr.write('PDFCrowd Error: {}\n'.format(why))
    raise

Get info about the current conversion

import pdfcrowd
import sys

try:
    # Create an API client instance.
    client = pdfcrowd.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d')

    # Configure the conversion.
    client.setOutputFormat('png')
    client.setDebugLog(True)

    # Run the conversion and save the result to a file.
    client.convertFileToFile('/path/to/MyLayout.html', 'MyLayout.png')
    
    # print URL pointing to the debug log for this request.
    print('Debug log url: {}'.format(client.getDebugLogUrl()))
    
    # print Number of conversion credits remaining in your account.
    print('Remaining credit count: {}'.format(client.getRemainingCreditCount()))
    
    # print Number of credits consumed for this conversion.
    print('Consumed credit count: {}'.format(client.getConsumedCreditCount()))
    
    # print Unique identifier assigned to this conversion job.
    print('Job id: {}'.format(client.getJobId()))
    
    # print Size of the output data in bytes.
    print('Output size: {}'.format(client.getOutputSize()))

except pdfcrowd.Error as why:
    sys.stderr.write('PDFCrowd Error: {}\n'.format(why))
    raise

Template rendering examples

Create Image from JSON data

import pdfcrowd
import sys

try:
    # Create an API client instance.
    client = pdfcrowd.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d')

    # Configure the conversion.
    client.setOutputFormat('png')
    client.setDataString("""{
            "name": "World",
            "product": "Pdfcrowd API"
        }""")

    # Run the conversion and save the result to a file.
    client.convertStringToFile('Hello {{ name }} from {{ product }}', 'output.pdf')

except pdfcrowd.Error as why:
    sys.stderr.write('PDFCrowd Error: {}\n'.format(why))
    raise

Create Image from XML data

import pdfcrowd
import sys

try:
    # Create an API client instance.
    client = pdfcrowd.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d')

    # Configure the conversion.
    client.setOutputFormat('png')
    client.setDataString("""<?xml version="1.0" encoding="UTF-8"?>
        <data>
          <name>World</name>
          <product>Pdfcrowd API</product>
        </data>""")

    # Run the conversion and save the result to a file.
    client.convertStringToFile('Hello {{ data.name }} from {{ data.product }}', 'output.pdf')

except pdfcrowd.Error as why:
    sys.stderr.write('PDFCrowd Error: {}\n'.format(why))
    raise

Create Image from YAML data

import pdfcrowd
import sys

try:
    # Create an API client instance.
    client = pdfcrowd.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d')

    # Configure the conversion.
    client.setOutputFormat('png')
    client.setDataString("""name: World
product: Pdfcrowd API""")

    # Run the conversion and save the result to a file.
    client.convertStringToFile('Hello {{ name }} from {{ product }}', 'output.pdf')

except pdfcrowd.Error as why:
    sys.stderr.write('PDFCrowd Error: {}\n'.format(why))
    raise

Create Image from CSV data

import pdfcrowd
import sys

try:
    # Create an API client instance.
    client = pdfcrowd.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d')

    # Configure the conversion.
    client.setOutputFormat('png')
    client.setDataString("""name,product
World,Pdfcrowd API""")

    # Run the conversion and save the result to a file.
    client.convertStringToFile('Hello {{ name }} from {{ product }}', 'output.pdf')

except pdfcrowd.Error as why:
    sys.stderr.write('PDFCrowd Error: {}\n'.format(why))
    raise

Django examples

Flask examples