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: sys.stderr.write('Pdfcrowd Error: {}\n'.format(why)) 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: sys.stderr.write('Pdfcrowd Error: {}\n'.format(why)) 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: sys.stderr.write('Pdfcrowd Error: {}\n'.format(why)) 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: sys.stderr.write('Pdfcrowd Error: {}\n'.format(why)) 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: sys.stderr.write('Pdfcrowd Error: {}\n'.format(why)) 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: sys.stderr.write('Pdfcrowd Error: {}\n'.format(why)) 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: sys.stderr.write('Pdfcrowd Error: {}\n'.format(why)) 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: sys.stderr.write('Pdfcrowd Error: {}\n'.format(why)) 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: sys.stderr.write('Pdfcrowd Error: {}\n'.format(why)) 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 of the debug log print('Debug log url: {}'.format(client.getDebugLogUrl())) # print the number of conversion credits remaining in your account print('Remaining credit count: {}'.format(client.getRemainingCreditCount())) # print the number of credits used for the conversion print('Consumed credit count: {}'.format(client.getConsumedCreditCount())) # print the unique identifier for the conversion 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
import pdfcrowd import sys try: # create the 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 write 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
import pdfcrowd import sys try: # create the 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 write 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
import pdfcrowd import sys try: # create the 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 write 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
import pdfcrowd import sys try: # create the 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 write 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
import urllib.parse from django.http import HttpResponse from django.views.decorators.http import require_POST import pdfcrowd # the recommended method is POST @require_POST def convert(request): try: # create the API client instance client = pdfcrowd.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d') # configure the conversion client.setOutputFormat('png') # set HTTP response headers response = HttpResponse(content_type='image/png') response['Cache-Control'] = 'max-age=0' response['Accept-Ranges'] = 'none' response['Content-Disposition'] = "attachment; filename*=UTF-8''" + urllib.parse.quote('example.png', safe='') # run the conversion and write the result into the output stream client.convertUrlToStream('http://www.example.com', response) return response except pdfcrowd.Error as why: # send the error in the HTTP response return HttpResponse(why.getMessage(), status=why.getCode(), content_type='text/plain')
import urllib.parse from django.http import HttpResponse from django.views.decorators.http import require_POST import pdfcrowd # the recommended method is POST @require_POST def convert(request): try: # create the API client instance client = pdfcrowd.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d') # configure the conversion client.setOutputFormat('png') # set HTTP response headers response = HttpResponse(content_type='image/png') response['Cache-Control'] = 'max-age=0' response['Accept-Ranges'] = 'none' response['Content-Disposition'] = "attachment; filename*=UTF-8''" + urllib.parse.quote('MyLayout.png', safe='') # run the conversion and write the result into the output stream client.convertFileToStream('/path/to/MyLayout.html', response) return response except pdfcrowd.Error as why: # send the error in the HTTP response return HttpResponse(why.getMessage(), status=why.getCode(), content_type='text/plain')
import urllib.parse from django.http import HttpResponse from django.views.decorators.http import require_POST import pdfcrowd # the recommended method is POST @require_POST def convert(request): try: # create the API client instance client = pdfcrowd.HtmlToImageClient('demo', 'ce544b6ea52a5621fb9d55f8b542d14d') # configure the conversion client.setOutputFormat('png') # set HTTP response headers response = HttpResponse(content_type='image/png') response['Cache-Control'] = 'max-age=0' response['Accept-Ranges'] = 'none' response['Content-Disposition'] = "attachment; filename*=UTF-8''" + urllib.parse.quote('HelloWorld.png', safe='') # run the conversion and write the result into the output stream client.convertStringToStream('<html><body><h1>Hello World!</h1></body></html>', response) return response except pdfcrowd.Error as why: # send the error in the HTTP response return HttpResponse(why.getMessage(), status=why.getCode(), content_type='text/plain')
import urllib.parse from flask import Flask, Response import pdfcrowd app = Flask(__name__) # the recommended method is POST @app.route('/', methods=['POST']) def convert(): 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'); # send the result and set HTTP response headers response = Response(image, mimetype='image/png') response.headers['Cache-Control'] = 'max-age=0' response.headers['Accept-Ranges'] = 'none' response.headers['Content-Disposition'] = "attachment; filename*=UTF-8''" + urllib.parse.quote('example.png', safe='') return response except pdfcrowd.Error as why: # send the error in the HTTP response return Response(why.getMessage(), status=why.getCode(), mimetype='text/plain')
import urllib.parse from flask import Flask, Response import pdfcrowd app = Flask(__name__) # the recommended method is POST @app.route('/', methods=['POST']) def convert(): 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'); # send the result and set HTTP response headers response = Response(image, mimetype='image/png') response.headers['Cache-Control'] = 'max-age=0' response.headers['Accept-Ranges'] = 'none' response.headers['Content-Disposition'] = "attachment; filename*=UTF-8''" + urllib.parse.quote('MyLayout.png', safe='') return response except pdfcrowd.Error as why: # send the error in the HTTP response return Response(why.getMessage(), status=why.getCode(), mimetype='text/plain')
import urllib.parse from flask import Flask, Response import pdfcrowd app = Flask(__name__) # the recommended method is POST @app.route('/', methods=['POST']) def convert(): 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>'); # send the result and set HTTP response headers response = Response(image, mimetype='image/png') response.headers['Cache-Control'] = 'max-age=0' response.headers['Accept-Ranges'] = 'none' response.headers['Content-Disposition'] = "attachment; filename*=UTF-8''" + urllib.parse.quote('HelloWorld.png', safe='') return response except pdfcrowd.Error as why: # send the error in the HTTP response return Response(why.getMessage(), status=why.getCode(), mimetype='text/plain')