Developers/Python SDK

Python SDK for FlexiDoc

The official Python library for FlexiDoc API. Convert documents between PDF and HTML formats with just a few lines of code.

Installation
$ pip install flexidoc
PyPI versionPython versionsDownloadsLicense

Why Use Our Python SDK?

Easy Installation

Install with pip and get started in seconds

Async Support

Built-in async/await support for high-performance applications

Type Hints

Full type annotations for better IDE support and code quality

Open Source

MIT licensed with active community and regular updates

Quick Start Guide

Basic Usage

from flexidoc import FlexiDoc

# Initialize the client
client = FlexiDoc('your-api-key')

# Convert PDF to HTML
with open('document.pdf', 'rb') as file:
    result = client.pdf_to_html(file)
    print(result.html)

# Convert HTML to PDF
pdf = client.html_to_pdf('<h1>Hello World</h1>')
with open('output.pdf', 'wb') as f:
    f.write(pdf)

Code Examples

Authentication

from flexidoc import FlexiDoc

# Using API key
client = FlexiDoc('your-api-key')

# Using environment variable (recommended)
# Set FLEXIDOC_API_KEY in your environment
client = FlexiDoc()

# Using custom base URL (for enterprise)
client = FlexiDoc(
    api_key='your-api-key',
    base_url='https://api.your-domain.com'
)

Store your API key securely using environment variables. Never commit API keys to version control.

PDF to HTML Conversion

from flexidoc import FlexiDoc

client = FlexiDoc('your-api-key')

# Basic conversion
with open('document.pdf', 'rb') as file:
    result = client.pdf_to_html(file)
    
# With options
result = client.pdf_to_html(
    file,
    options={
        'semantic': True,
        'extract_images': True,
        'preserve_layout': True,
        'output_format': 'clean'  # or 'semantic', 'minimal'
    }
)

# Access results
print(result.html)
print(result.metadata)
print(result.images)  # List of extracted images

# Save to file
result.save('output.html')

HTML to PDF Conversion

from flexidoc import FlexiDoc

client = FlexiDoc('your-api-key')

# From HTML string
pdf = client.html_to_pdf('<h1>Hello World</h1>')

# From URL
pdf = client.html_to_pdf_from_url('https://example.com')

# With options
pdf = client.html_to_pdf(
    html_content,
    options={
        'format': 'A4',
        'margin': {'top': '1cm', 'bottom': '1cm'},
        'header': '<div>Page {page} of {pages}</div>',
        'footer': '<div>© 2024 Company</div>',
        'wait_for': 'networkidle0',
        'media_type': 'print'
    }
)

# Save to file
with open('output.pdf', 'wb') as f:
    f.write(pdf)

Batch Processing

from flexidoc import FlexiDoc, BatchJob
import asyncio

client = FlexiDoc('your-api-key')

# Create batch job
batch = BatchJob('Monthly Report Processing')

# Add multiple conversions
for file_path in glob.glob('reports/*.pdf'):
    batch.add_conversion(
        source=file_path,
        operation='pdf-to-html',
        destination=f'output/{os.path.basename(file_path)}.html'
    )

# Start batch processing
job = client.batch.create(batch)

# Monitor progress
async def monitor_progress():
    async for update in job.progress_stream():
        print(f"Progress: {update.percentage}%")
        print(f"Completed: {update.completed}/{update.total}")

asyncio.run(monitor_progress())

Error Handling

from flexidoc import FlexiDoc, FlexiDocError, RateLimitError
import time

client = FlexiDoc('your-api-key')

try:
    result = client.pdf_to_html(file)
except RateLimitError as e:
    # Handle rate limiting
    print(f"Rate limited. Retry after {e.retry_after} seconds")
    time.sleep(e.retry_after)
    result = client.pdf_to_html(file)
except FlexiDocError as e:
    # Handle other API errors
    print(f"API Error: {e.message}")
    print(f"Error Code: {e.code}")
except Exception as e:
    # Handle unexpected errors
    print(f"Unexpected error: {str(e)}")

Requirements & Compatibility

System Requirements

  • Python 3.8 or higher
  • Works on Windows, macOS, and Linux
  • No additional system dependencies

Dependencies

  • requests >= 2.28.0
  • aiohttp >= 3.8.0 (for async)
  • pydantic >= 2.0.0

Resources & Support

Documentation

Comprehensive API reference and guides

Read the docs →

GitHub

Source code, issues, and contributions

View repository →

Examples

Sample projects and use cases

Browse examples →

Ready to Start Building?

Get your API key and start converting documents with Python