PDF to Image API Endpoint

Version 2.197 (Release Notes ↗)

Description

The PDF to image endpoint allows you to convert a PDF document into high-resolution JPEG or PNG images using a single, SDK-free REST API. This process converts one page at a time, ensuring optimal efficiency and image quality. For dynamic, on-demand generation of complex PDFs, such as invoices, from your programming environment using Markdown or HTML input, utilize the PDGGEN API endpoint.

HTTP Methods

GET, POST

HTTP Parameters

Fields Type Description
src URL Input PDF URL. If you want to upload your PDF directly from your app, submit a multipart/form-data POST request.
key String Your PixLab API Key ↗. You can also embed your key in the WWW-Authenticate: HTTP header and omit this parameter.

Optional

Fields Type Description
pagenumber Integer Target page number to convert. If omitted, the first PDF page (index 0) is extracted. If the page number is out of range, the last page is converted.
export String Output image format (e.g., JPEG or PNG).
blob Boolean By default, returns a JSON Object with the output image link. If true, returns the image binary directly.

POST Request Body

Use when submitting a POST request instead of GET.

Allowed Content-Types:

  • multipart/form-data
  • application/json

Use multipart/form-data for direct PDF uploads (see REST API code samples). For JSON, the PDF must be hosted elsewhere.

HTTP Response

The endpoint returns application/json if the optional blob parameter is not set.

This API endpoint returns a JSON Object after each call only if the optional blob parameter is not set. Otherwise, the converted image is returned directly. The following fields are included in the JSON response body:

Fields Type Description
status Integer Status code 200 indicates success. Any other code indicates failure.
link URL Link to the image output stored on our high-performance pixlab.xyz storage server. You can configure custom S3 storage (see your dashboard ↗ for setup instructions).
id String Unique media identifier.
error String Error message when status ≠ 200.

Code Samples


import requests
from typing import Dict, Any

def convert_pdf_to_image(
    pdf_url: str,
    export_format: str = 'jpeg',
    api_key: str = 'PIXLAB_API_KEY'
) -> None:
    """Convert a PDF document to JPEG/PNG image using PixLab API."""
    try:
        response = requests.get(
            'https://api.pixlab.io/pdftoimg',
            params={
                'src': pdf_url,
                'export': export_format,
                'key': api_key
            },
            timeout=10
        )
        response.raise_for_status()
        data: Dict[str, Any] = response.json()
        
        if data.get('status') != 200:
            print(f"Error: {data.get('error', 'Unknown error')}")
        else:
            print(f"Link to the image output: {data['link']}")
            
    except requests.exceptions.RequestException as e:
        print(f"Request failed: {e}")
    except ValueError as e:
        print(f"Invalid JSON response: {e}")

if __name__ == '__main__':
    convert_pdf_to_image(
        pdf_url='https://www.getharvest.com/downloads/Invoice_Template.pdf'
    )
← Return to API Endpoint Listing