Dynamic PDF Generation API Endpoint

Version 2.197 (Release Notes ↗)

Description

The PDF Generation API empowers you to create professional PDF documents, including invoices and complex documents, directly from your code. Utilizing markdown, plain text, or rich HTML with images, links, and structured content, you can generate PDFs dynamically and on-demand. This eliminates the need for external libraries, mitigating potential security risks. PDFGEN, our SDK-free REST API, provides a secure and efficient solution for all your PDF generation needs. For mass PDF generation, please refer to the Rich PDF Creation Page.

HTTP Methods

POST

HTTP Parameters

Fields Type Description
input String | Blob Raw content in an appropriate format for parsing, rendering, and printing on the PDF document to be generated. Supported input formats as of this release are: markdown, html, or plaintext.
format String | Format format of the raw input contents. Supported input formats as of this release are: markdown, html, or plaintext.
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
blob Boolean By default, returns a JSON Object with the generated PDF document link. If true, returns the raw PDF binary instead.

POST Request Body

Use when submitting a POST request instead of GET.

Allowed Content-Types:

  • multipart/form-data
  • application/json

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. The object contains a link to the generated PDF document stored directly on your AWS S3 bucket, which you registered through the PixLab Console ↗, but only if the optional blob parameter is not set. If the blob parameter is set to true, the generated PDF document is returned directly. The JSON response body includes the following fields:

Fields Type Description
status Integer Status code 200 indicates success. Any other code indicates failure.
link URL Link to the generated PDF document stored directly on your AWS S3 bucket, which you registered through the PixLab Console ↗.
id String Unique media identifier.
error String Error message when status ≠ 200.

Code Samples


# Programmatically Generate PDF document from markdown or HTML input
import requests
import json

# Replace with your actual PixLab API key
api_key = "YOUR_PIXLAB_API_KEY" # Get yours from https://console.pixlab.io/

# Markdown formatted invoice
markdown_text = """
# Invoice

## To:

Acme Corp.
123 Main St.
Anytown, USA

## From:

Your Company
456 Oak Ave.
Anytown, USA

## Date:

2024-01-26

## Invoice Number:

INV-2024-001

## Items:

| Item          | Quantity | Price    |
|---------------|----------|----------|
| Widget        | 2        | $10.00   |
| Gadget        | 1        | $25.00   |
| Service       | 1        | $50.00   |

## Subtotal:

$85.00

## Tax:

$5.00

## Total:

$90.00

## Notes:

Thank you for your business!
"""

# API endpoint to programmatically generate PDFs from markdown or HTML input
api_endpoint = "https://api.pixlab.io/pdfgen"

# Request parameters
payload = {
    "key": api_key,
    "input": markdown_text,
    "format": "markdown"
}

# Convert payload to JSON
headers = {'Content-Type': 'application/json'}

# Make the POST request
try:
    response = requests.post(api_endpoint, headers=headers, data=json.dumps(payload))

    # Check for successful response
    if response.status_code == 200:
        data = response.json()
        if data.get("status") == 200:
            pdf_link = data.get("link")
            print(f"PDF generated successfully.  Link: {pdf_link}")
        else:
            print(f"Error generating PDF: {data.get('error')}")
    else:
        print(f"Request failed with status code: {response.status_code}")
        print(response.text)  # Print the response content for debugging

except requests.exceptions.RequestException as e:
    print(f"An error occurred: {e}")
← Return to API Endpoint Listing