How to convert PDF to JPG using Python

Table of contents

    Convert PDFs to JPG images for web integration, thumbnails, and image processing workflows. This tutorial demonstrates programmatic PDF-to-JPG conversion using Python and Nutrient API for applications requiring image output from PDF documents.
    How to convert PDF to JPG using Python
    Summary

    This tutorial shows how to convert PDFs to JPG images using Nutrient’s Python API. You’ll create a free account, get API credentials, and write a Python script to convert documents. The API also supports merging, watermarking, and page manipulation before conversion.

    Convert PDF files to JPG images using Nutrient’s PDF-to-JPG Python API. The free plan includes 200 credits. Different operations use different amounts of credits, so your document count will vary. Create a free account(opens in a new tab) to get your API key.

    Why convert PDF to JPG?

    Converting PDF files to JPG is essential for workflows requiring image formats. Common use cases include:

    • Web optimization — Convert PDFs to JPG for faster page loading and better web compatibility, especially for document previews and galleries.
    • Thumbnail generation — Create preview images for document management systems, file browsers, and content libraries.
    • Image processing pipelines — Extract pages as images for OCR, computer vision analysis, or machine learning workflows.
    • Social media sharing — Convert documents to shareable image formats for platforms that don’t support PDF embedding.
    • Archive and preview — Generate lightweight previews of PDF documents without requiring full PDF rendering capabilities.

    The PDF-to-JPG API automates this process in your workflow.

    Nutrient DWS Processor API

    Converting PDF to JPG is one of 30+ operations available through our PDF API tools. Combine PDF-to-JPG conversion with other tools for complex workflows:

    Your account includes access to all PDF API tools.

    Step 1 — Creating a free account on Nutrient

    Go to our website(opens in a new tab) to create your free account.

    Free account Nutrient API

    After creating your account, you’ll see your plan overview.

    You start with 200 credits and access to all PDF API tools.

    Step 2 — Obtaining the API key

    After verifying your email, get your API key from the dashboard. Click API keys in the left menu to view your keys.

    Convert PDF to JPG Python API Key

    Copy the Live API key for the PDF-to-JPG API.

    Step 3 — Setting up folders and files

    Create a folder called pdf_to_jpg and open it in a code editor. This tutorial uses VS Code. Create two folders inside pdf_to_jpg: input_documents and processed_documents.

    Copy your PDF file to the input_documents folder and rename it to document.pdf. Use our demo document if needed.

    In the root folder, pdf_to_jpg, create a file called processor.py for your code.

    Your folder structure:

    pdf_to_jpg
    ├── input_documents
    | └── document.pdf
    ├── processed_documents
    └── processor.py

    Step 4 — Writing the code

    Open the processor.py file and paste the code below into it:

    import requests
    import json
    response = requests.request(
    'POST',
    'https://api.nutrient.io/build',
    headers = {
    'Authorization': 'Bearer YOUR_API_KEY_HERE'
    },
    files = {
    'document': open('input_documents/document.pdf', 'rb')
    },
    data = {
    'instructions': json.dumps({
    'parts': [
    {
    'file': 'document'
    }
    ],
    'output': {
    'type': 'image',
    'format': 'jpg',
    'dpi': 500
    }
    })
    },
    stream = True
    )
    if response.ok:
    with open('processed_documents/image.jpg', 'wb') as fd:
    for chunk in response.iter_content(chunk_size=8096):
    fd.write(chunk)
    else:
    print(response.text)
    exit()

    Make sure to replace YOUR_API_KEY_HERE with your API key.

    Code explanation

    Import the requests and json dependencies. Create the instructions for the API call.

    Use the requests module to make the API call. Store the result in the processed_documents folder.

    Output

    Run the code:

    Terminal window
    python3 processor.py

    After execution, you’ll find image.jpg in the processed_documents folder.

    The folder structure becomes:

    pdf_to_jpg
    ├── input_documents
    | └── document.pdf
    ├── processed_documents
    | └── image.jpg
    └── processor.py

    Additional resources

    Explore more ways to work with Nutrient API:

    Conclusion

    You learned how to convert PDFs to JPG images using our Python API.

    Integrate these functions into your existing applications. The same API token works for other operations like merging documents and adding watermarks. Sign up(opens in a new tab) for a free trial.

    FAQ

    Does the API preserve PDF quality when converting to JPG?

    Yes. The API maintains high image quality during conversion. You can control output quality using the dpi parameter (shown as 500 in the example). Higher DPI values create larger files with better quality. Standard web images use 72–150 DPI, while print requires 300+ DPI.

    What Python version is required for the Nutrient API?

    Python 3.6+ is required. The tutorial uses the requests library (install with pip install requests). The json module is built into Python. No additional PDF processing libraries, like PyPDF2 or pdf2image, are needed.

    Does Nutrient provide a Python client library for easier integration?

    Yes. The Nutrient Python client provides a higher-level interface for API operations. While this tutorial uses direct HTTP requests with the requests library for transparency, the Python client simplifies authentication, error handling, and response parsing for production applications.

    Jonathan D. Rhyne

    Jonathan D. Rhyne

    Co-Founder and CEO

    Jonathan joined PSPDFKit in 2014. As Co-founder and CEO, Jonathan defines the company’s vision and strategic goals, bolsters the team culture, and steers product direction. When he’s not working, he enjoys being a dad, photography, and soccer.

    Explore related topics

    FREE TRIAL Ready to get started?