Web Renderer

FREEMIUM
By petadata | Updated il y a un mois | Business Software
Health Check

N/A

Back to All Tutorials (5)

Convert HTML to PDF by using Python

In this tutorial we will cover how to implement asynchronous approach of Web Renderer. You only need to send HTML content to SubmitPDFFromHTMLTask or SubmitImageFromHTMLTask API methods to receive task identifier as response. There is also SubmitPDFFromUrlTask and SubmitImageFromUrlTask methods to render PDF or Image from public Web Uri.

After receiving task identifier you need to check task status by calling GetRenderingTaskStatus method. You need to call same method after a few seconds delay as long as task status equals to “Waiting”. Once you receive “Completed” from GetRenderingTaskStatus method you can call DownloadResult method to download final file. You can use the Python implementation below.

import requests
import time


def submit_pdf_from_html_task(htmlContent, uri, rapid_api_key):
    multipart_form_data = {
        'uri': (None, uri),
        'useCompression': (None, 'false'),
        'pageLabel': (None, 'Home Page'),
        'messageLabel': (None, 'Initial Capture'),
        'browserHeight': (None, '768'),
        'browserWidth': (None, '1024'),
        'htmlContent': (None, htmlContent)
    }

    headers = {
        'X-RapidAPI-Key': rapid_api_key
    }

    response = requests.post('https://web-renderer.p.rapidapi.com/SubmitPDFFromHTMLTask',
                             files=multipart_form_data, headers=headers)
    if response.status_code != 200:
        raise Exception('Cannot create task')

    return response.text


def get_rendering_task_status(task_id, rapid_api_key):
    params = {
        'taskId': task_id
    }

    headers = {
        'X-RapidAPI-Key': rapid_api_key
    }

    response = requests.get('https://web-renderer.p.rapidapi.com/GetRenderingTaskStatus',
                            headers=headers, params=params)
    if response.status_code != 200:
        raise Exception("Cannot check task status")

    return response.text


def download_result(task_id, rapid_api_key):
    params = {
        'taskId': task_id
    }

    headers = {
        'X-RapidAPI-Key': rapid_api_key
    }

    response = requests.get('https://web-renderer.p.rapidapi.com/DownloadResult', headers=headers,
                            params=params)
    if response.status_code != 200:
        raise Exception('Cannot download file')

    return response.content


if __name__ == '__main__':
    rapid_api_key = '<YOUR RAPIDAPI KEY HERE>'
    html_content = '<HTML CONTENT TO RENDER>'
    uriOfHtmlContent = '<URI OF HTML CONTENT>'
    task_id = submit_pdf_from_html_task(html_content, uriOfHtmlContent, rapid_api_key)
    retry_count = 0
    while retry_count < 100:
        retry_count += 1
        time.sleep(5)
        status = get_rendering_task_status(task_id, rapid_api_key)
        if status == 'Completed':
            file_bytes = download_result(task_id, rapid_api_key)
            with open('html_from_html.pdf', mode='wb') as binary_pdf:
                binary_pdf.write(file_bytes)
            break
        elif status == 'Waiting':
            continue
        elif status == 'Failed':
            raise Exception('Cannot render html')
        else:
            raise Exception('Invalid status')