Generate a PDF from Images

Information

PSPDFKit Server has been deprecated and replaced by PSPDFKit Document Engine. All PSPDFKit Server and PSPDFKit for Web Server-Backed licenses will work as before and be supported until 15 May 2024 (we will contact you about license migration). To start using Document Engine, refer to the migration guide. With Document Engine, you’ll have access to robust new capabilities (read the blog for more information).

This guide will take you through the process of generating a PDF using one or more images.

Using Document Conversion

ℹ️ Note: This requires you to license Image Documents.

Your first option for converting an image to a PDF is to upload it directly to PSPDFKit for Web Server-Backed. This works if you only want to create a PDF from a single image. The following example illustrates this using a PNG file. To create a new document from a PNG file, POST its contents to /api/documents:

Request

curl -X POST http://localhost:5000/api/documents \
  -H "Authorization: Token token=<API token>" \
  -F file=@/path/to/image.png
POST /api/documents HTTP/1.1
Content-Type: multipart/form-data; boundary=customboundary
Authorization: Token token=<API token>

--customboundary
Content-Disposition: form-data; name="file"; filename="image.png"
Content-Type: image/png

<PNG data>
--customboundary--

Response

HTTP/1.1 200 OK
Content-Type: application/json

{
  "data": {
    "document_id": ...,
    "errors": [],
    "sourcePdfSha256": ...,
    "title": "..."
  }
}

The image will automatically be converted and can be downloaded as a PDF using the returned document ID.

Using PDF Generation

ℹ️ Note: This requires you to license PDF Generation.

Your second option is to use PDF Generation. This allows you to put as many images as you want into a single PDF by adding the images to an HTML page and uploading it to PSPDFKit Server.

Document Content

You can arrange your images in the HTML like in the following example:

<!DOCTYPE html>
<html>
  <body>
    <img src="my-image-1.png" width="100%">
    <img src="my-image-2.png" width="100%">
  </body>
</html>

PDF Generation

Next, send the above HTML and the images you want to include to Server for generation. Use the /api/documents endpoint, sending the PDF Generation schema with the HTML file from above. Make sure you also supply the images here:

curl -X POST http://localhost:5000/api/documents \
  -H 'Authorization: Token token=<API token>' \
  -F document_id=image-document \
  -F generation='{
    "html": "page.html",
    "assets": [
      "my-image-1.png",
      "my-image-2.png"
    ]
  }' \
  -F page.html=@/path/to/page.html \
  -F my-image-1.png=@/path/to/my-image-1.png \
  -F my-image-2.png=@/path/to/my-image-2.png
POST /api/documents HTTP/1.1
Authorization: Token token=<API token>
Content-Type: multipart/form-data; boundary=customboundary

--customboundary
Content-Disposition: form-data; name="document_id";

image-document
--customboundary
Content-Disposition: form-data; name="generation";
Content-Type: application/json

{
  "html": "page.html",
  "assets": [
    "my-image-1.png",
    "my-image-2.png"
  ]
}
--customboundary
Content-Disposition: form-data; name="page.html" filename="page.html";
Content-Type: text/html

<HTML data>
--customboundary
Content-Disposition: form-data; name="my-image-1.png" filename="my-image-1.png";
Content-Type: text/html

<PNG data>
--customboundary
Content-Disposition: form-data; name="my-image-2.png" filename="my-image-2.png";
Content-Type: text/html

<PNG data>
--customboundary

After performing the above curl command, you can view the generated document in the Server dashboard at http://localhost:5000/dashboard/documents/image-document

A PDF showing two images