NewConnect Claude, Gemini, ChatGPT, and other AI agents to API Ninjas via our MCP server →

Image to PDF API

The Image to PDF API converts JPG, PNG and other images to PDF. Send one image or up to 20, as uploads or links, and get back one PDF with one image per page, in the order you sent them. JPG, PNG, WebP, GIF, TIFF and BMP are supported, and every frame of a GIF or multi-page TIFF becomes a page.

Pages can match each image or use a standard paper size (A4, Letter or Legal) with margins, in portrait or landscape. By default images keep their full quality: JPGs are copied into the PDF unchanged and PNGs are stored without loss. Set quality to compress them into a smaller PDF.

Use it to turn scans, receipts, photos and screenshots into a PDF for sharing, printing, archiving or e-signature.

Each page counts as 10 API calls against your monthly quota. Requests that fail are not counted, and the X-Api-Calls-Charged response header shows how many API calls each request used. See pricing.

Converting phone photos or scans in JPG? See the JPG to PDF API. To go the other way, the PDF to Image API and PDF to JPG API turn PDF pages into images, and the Image to Text API reads the text in an image.

Available endpoint:

At a Glance

InputJPG, PNG, WebP, GIF, TIFF or BMP: uploads (about 4 MB in total), raw body, or up to 20 links (25 MB in total)
OutputOne PDF, as base64 in JSON or as a PDF file
Pages per requestUp to 20
Page sizeFit to image, A4, Letter or Legal; margins up to 50 mm
Image qualityKept as is, or compressed with quality
API calls10 per page in the PDF

Page Sizes

page_sizePage
fitThe size of each image (default)
a4210 x 297 mm
letter8.5 x 11 in
legal8.5 x 14 in

Related APIs

https://api.api-ninjas.com/v1/imagetopdf

Converts one or more images (JPG, PNG, WebP, GIF, TIFF or BMP) to one PDF, one image per page. Returns the PDF as base64 in JSON, or as a raw PDF file. Accepts GET or POST: use POST to upload images, or GET with url.

Parameters

Send the images as file uploads (multipart form data), as url links, or send one image as the raw request body with its Content-Type, e.g. image/jpeg. The other parameters can be query parameters or multipart form fields.

  • file  optional

    An image, as a multipart form upload. Repeat file to add more images: each image becomes a page, in the order sent, and each frame of a GIF or TIFF becomes a page. Up to about 4 MB in total.

  • url  optional

    A public http:// or https:// link to an image. Repeat url for more images: up to 20, and 25 MB in total. Uploaded files come first, then the url images. Works with GET or POST.

  • page_size  optional

    fit (default): each page is the size of its image, at the resolution stored in the file. a4, letter or legal: every page has that size, and each image is scaled to fit inside the margins, keeping its proportions.

  • orientation  optional

    auto (default): landscape pages for landscape images and portrait pages for portrait images. portrait or landscape: every page. Applies only with a4, letter or legal.

  • margin  optional

    White margin around each image, in millimeters, from 0 to 50. Default 0.

  • quality  optional

    From 1 to 100. When set, each image is re-encoded as a JPG at this quality to make the PDF smaller, and transparent areas become white. Default: not set, so images are kept as they are: JPGs are copied into the PDF unchanged and PNGs are stored without loss.

Headers

  • X-Api-Key  required

    API Key associated with your account.

  • Accept  optional

    application/pdf returns the raw PDF file instead of JSON.

  • Content-Type  optional

    The image type, e.g. image/jpeg or image/png, when the request body is the image itself. Multipart uploads set their own content type.

Response

The /v1/imagetopdf endpoint returns a JSON object with the following fields, or an error if the request is unsuccessful (see errors).

  • page_count

    Number of pages in the PDF.

  • size

    Size of the PDF in bytes.

  • pdf

    The PDF file, base64-encoded.

With Accept: application/pdf, the response body is the PDF file itself (Content-Type: application/pdf). Every successful response has an X-Api-Calls-Charged header with the number of API calls the request counted.

Limits

  • Up to 20 pages per request (images, plus the extra frames of GIF and TIFF files).
  • The PDF can be up to about 4 MB. For larger PDFs, set quality (for example quality=75) or send fewer images per request.
  • Uploads up to about 4 MB in total; images from url up to 25 MB in total.
  • Images up to 120 megapixels.

API Calls

Each page in the PDF counts as 10 API calls: a PDF made from 3 images counts as 30. Requests that fail are not counted. When your remaining quota does not cover every page, the request returns an error and nothing is counted.

Live Demo

Live demo · no API key required
Form data and query parameters
file
Selected file
Using the sample photo until you choose a file.
page_size

Get a free API key to get started — no credit card required.

Headers
X-Api-KeyLog in or sign up to get your API key
POST
https://api.api-ninjas.com/v1/imagetopdf
Sample response

Press Send to convert the image and preview the PDF here.

{
"page_count": 1,
"size": 70108,
"pdf": "JVBERi0xLjMKJeLjz9MKMSAwIG9iago8PAogICAgL0NyZWF0..."
}

The live demo converts one image up to 2 MB. No API key is needed to try it.

Code Examples

# Combine two images into one PDF and save it curl -X POST "https://api.api-ninjas.com/v1/imagetopdf?page_size=a4&margin=10" \ -H "X-Api-Key: YOUR_API_KEY" \ -H "Accept: application/pdf" \ -F "file=@photo.jpg" \ -F "file=@scan.png" \ --output images.pdf

Errors

An invalid request returns HTTP 400 with a JSON body holding a readable error message, and is not counted against your quota. Authentication and quota errors use the standard format described in error codes.

CauseExample error
No image in the requestSend images as file (multipart form uploads) or as url (links to the images).
A file that is not a supported imageEach file must be a JPG, PNG, WebP, GIF, TIFF or BMP image.
More than 20 imagesSend at most 20 images per request.
More than 20 pages, e.g. from a long GIFThe PDF would have 24 pages; the most per request is 20.
A PDF over about 4 MBThe PDF would be 5.3 MB; the most one response can return is 4 MB. Send fewer or smaller images, or set quality (for example quality=75) to compress them.
A page_size other than fit, a4, letter or legalpage_size must be fit, a4, letter or legal.
A margin out of rangemargin must be between 0 and 50 millimeters.
An image over 120 megapixelsAn image is too large (over 120 megapixels).

Frequently Asked Questions

  • Yes, but you must have a premium subscription. Commercial use of the Image to PDF API is not permitted on the free tier. See our pricing page to choose a plan that fits your needs.
  • Sign up for a free account to instantly get your API key, then pass it in the X-Api-Key header on every request. The documentation above includes ready-to-run examples in cURL, Python, JavaScript, Node.js, and more.
  • Your API requests will simply be denied once you hit your monthly quota — you will never be charged more than the plan you signed up for. To increase your quota, you can upgrade your plan any time on our pricing page.
  • The API responds with a standard HTTP status code and a JSON error message describing the problem. See our error codes reference for the full list of codes and how to resolve each one, or contact support if you need help.
  • Send the images to /v1/imagetopdf as multipart uploads named file (repeat it for each image), as url links, or send one image as the raw request body. The response is JSON with the PDF base64-encoded in pdf; add the header Accept: application/pdf to get the PDF file itself. For JPG photos, see the JPG to PDF API.
  • Yes. Send up to 20 images in one request and you get one PDF with one image per page, in the order you sent them. Each frame of a GIF or multi-page TIFF becomes its own page, and counts toward the 20 pages.
  • JPG, PNG, WebP, GIF, TIFF and BMP, in any mix. Transparent PNGs keep their transparency unless you set quality, which places them on white.
  • No, not by default: JPGs are copied into the PDF unchanged and PNGs are stored without loss, so the pages look exactly like your images. Set quality (1 to 100) only when you want a smaller PDF; every image is then compressed again as a JPG at that quality.
  • Set page_size to a4, letter or legal and add margin in millimeters, e.g. /v1/imagetopdf?page_size=a4&margin=10. Each image is scaled to fit inside the margins and keeps its proportions. orientation=auto (the default) turns the page to match each image; portrait or landscape fixes it.
  • Each page in the PDF counts as 10 API calls, so a PDF made from 3 images counts as 30. Requests that fail are not counted, and the X-Api-Calls-Charged response header shows the count for each request. To read text from images instead, see the Image to Text API.