View all our code samples

Using Httplib2 in Python

Httplib2 is a comprehensive HTTP client library for Python. It provides support for features like caching, authentication, SSL/TLS, and proxy handling. httplib2 offers a higher-level API compared to Python's built-in http.client module and is commonly used for making HTTP requests and handling responses in Python applications.

We've implemented a code sample that you can re-use to convert your HTML documents to PDF, JPG, PNG or WEBP using PDFShift and Python:

import httplib2
import json
import base64

def convert(api_key, params, endpoint='pdf'):
    """
    We focused on making this function as simple as possible.
    Since PDFShift is a REST API, we just need to send a
    POST request to the endpoint by passing a set of custom parameters.
    
    Args:
        api_key (str): Your API key.
        params (dict): A dictionary containing the parameters
                       you want to send to the API.
        endpoint (str): The type of conversion you want to perform
                        (pdf, png, jpg, webp)
    
    Returns:
        bytes | dict: Either the binary content of the PDF
                      or a dictionary containing the details
                      when filename/webhook are passed
    """
    
    assert endpoint in ('pdf', 'png', 'jpg', 'webp')
    
    # Set the API URL
    url = f'https://api.pdfshift.io/v3/convert/{endpoint}'
    
    # Create an httplib2.Http instance
    http = httplib2.Http()
    
    # Set authorization header
    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'Basic ' + base64.b64encode(f'api:{api_key}'.encode('utf-8')).decode('utf-8')
    }
    
    # Convert params to JSON
    body = json.dumps(params)
    
    # Make the POST request
    response, content = http.request(url, method='POST', body=body, headers=headers)
    
    # Check for successful response
    if response.status >= 400:
        raise ValueError(f"Request failed with status code {response.status}: {content.decode('utf-8')}")
    
    # Decode the content
    decoded_content = content.decode('utf-8')
    
    # Return the response based on the presence of filename or webhook
    if 'filename' in params or 'webhook' in params:
        return json.loads(decoded_content)
    
    return decoded_content

Here's how you can use the above code:

binary = convert('sk_XXXXXXXXXXXXXX', {'source': 'https://en.wikipedia.org/wiki/REST'})
with open('result.pdf', 'wb') as f:
    f.write(binary)

We've tested this code with the latest version of Httplib2 and it's ready to be used in your project.

But if you were to encounter any bugs or issues while running it (or if you want to suggest changes to improve the code), please contact us and we'll be happy to help you out.