Cloudinary Blog

Uploading, converting and generating thumbnails for PDF documents

How to Create PDF Thumbnails Automatically
Web applications that manage user data, commonly allow their users to upload such data as PDF documents. A common UI practice employed by such services is to show a thumbnail of the PDF cover letter for quick identification, and thumbnails of the different PDF pages for fast access.
 
Using Cloudinary, you can accomplish such tasks with ease. 
 
In this blog post we wanted to showcase some of Cloudinary’s powerful PDF (and multi-page TIFF) management capabilities.
 

Uploading PDF files

From a website’s perspective, PDF assets share many usability characteristics as regular image files, and so we treat them quite the same as images. You can simply use our standard upload API for uploading PDF files.

Ruby on Rails:
Cloudinary::Uploader.upload("SinglePageSample.pdf", :public_id => "single_page_pdf"
Python:
cloudinary.uploader.upload("SinglePageSample.pdf", public_id = 'single_page_pdf')
Node.js:
cloudinary.uploader.upload("SinglePageSample.pdf", function(result) { }, 
                           {public_id: 'single_page_pdf'})
PHP:
\Cloudinary\Uploader::upload("SinglePageSample.pdf", "public_id" => 'single_page_pdf')
 The uploaded PDF is available for downloading as-is through a fast CDN:
 

This means that if you allow your users to upload images in multiple formats, they could also upload PDFs.

Generating thumbnails

To generate images and thumbnails from your PDF file, you can simply use Cloudinary’s resize and crop transformations, as you would for regular images.

For example, the following URL converts a PDF into a PNG image, as a 200x250 pixels thumbnail:


 
Same example in Rails: 
<%= cl_image_tag("single_page_pdf.png", :width => 200, :height => 250, :crop => :fill) %>
You can also apply any of Cloudinary’s powerful transformations on your PDFs. For example, here is the same page, but this time rotated and converted to grayscale with a watermark added:
 

 
 
While you can generate any thumbnail your graphics design requires, you can also dynamically generate and deliver a full sized image version of your PDF. To control the image’s quality, you can state the density (DPI) used to convert the PDF to an image. Simply use the new 'density' transformation parameter (or 'dn' for URLs) with an integer value between 1 and 300 (the default is 150).
 
The following examples generate a low density and a high density image versions of the uploaded PDF:
 
 
Same example in Rails:
<%= cl_image_tag("single_page_pdf.png", :density => 50) %>
  

Multi-page support

In the examples above, we’ve shown how to generate an image version of the first page of the PDF. But what about multi-page documents
 
You’ll be happy to hear that Cloudinary allows you to generate images and thumbnails for any page of a multi-page document. You can also use the same APIs to manage multi-page TIFF files.
 
In order to extract a single page out of a document, use the new 'page' parameter (or 'pg' for URLs).
 
The following example delivers a 200x300 JPG thumbnail created from the second page of the 'multi_page_pdf' PDF file.
 
 
 
Same example in Rails:
<%= cl_image_tag("multi_page_pdf.jpg", :width => 200, :height => 250,
                 :crop => :fill, :page => 2) %>
In the following example we also reduced quality to 50% to generate a smaller file that can be downloaded faster.
 
 
 

Tips and tricks

Cloudinary offers many additional features in the PDF management department, and you can also mix and match these with all of Cloudinary’s existing image manipulation and delivery capabilities. Following are a few quick ideas.
 
Using Cloudinary, it is easy to extract a single page from a multi-page PDF while returning it as a new PDF:
 
You can also convert arbitrary images to PDFs. For example, consider the following image depicting grazing horses:
 
 
Simply change this image’s suffix to .PDF and Cloudinary will convert the image to PDF and return it as a PDF file.
 
Cloudinary can even programmatically tell you how many pages are available in an uploaded PDF. Our upload API returns this number as its 'pages' parameter:
{  
"public_id":"multi_page_pdf",
"version":1343305663,
"width":612,
"height":792,
"format":"pdf",
"pages":3,
"url":"https://res.cloudinary.com/demo/image/upload/v1343305663/multi_page_pdf.pdf",
...
}
You can also upload a PDF as JPG, storing only the JPG on Cloudinary. This will require less storage and make sure that the image is available immediately on upload, rather than created lazily on demand. The following code sample converts an uploaded PDF to JPG while uploading, before storing it in the cloud and generating additional transformed versions. 

PHP:
\Cloudinary\Uploader::upload("SinglePageSample.pdf", "public_id" => 'single_page_pdf', 
                             "format" => 'jpg')
As usual with Cloudinary, all generated images, documents and thumbnails are cached and delivered through thousands of worldwide CDN servers (currently leveraging Amazon CloudFront).
 
PDF upload, thumbnail creation and transformations, together with multi-page document support, are all available now for both of our free and paid plans. Click here to quickly set up an account if you don't already have one.
 
Additional resources about generating PDF files from images using PHP, JavaScript and how to build a PDF viewer.
 
 

Recent Blog Posts

Reimaging DAM--The Next-Gen DAM for Marketing & Dev

There are great digital asset management (DAM) products out there for uploading, storing, managing, organizing, and sharing digital assets. With Cloudinary's new end-to-end DAM solution, you can also upload and manage your assets efficiently, but the journey doesn't end there. It continues on to the development and delivery stages, so that your assets can be seamlessly manipulated, optimized, and delivered to create an engaging user experience that will in turn, increase conversion and loyalty.

Read more
Integrating Cloudinary with Forestry’s Media Library

At Forestry, we believe that there is a bright future for static HTML sites built with tools like Jekyll and Hugo. These tools can create sites that run well, and are easy to host and maintain, because they don’t require any server-side code.

Read more
Video Optimization With the HTML5 <video> Player

Lack of experience and compression knowhow can cause significant user-experience problems. For instance, on a major retail site, I recently ran into a 48 MB video-hero banner. Pulling out the video and encoding it as an H.264 MP4 reduces the size to 1.9 MB. So, despite the desire for more video content, developers have not yet caught up to best practices. How do we get the best of both worlds without creating a disaster like the one above?

Read more
Build a Facial Emotion Recognition Based Video Suggestion App

Developers are always looking for new and creative ways to deliver content that resonates with the way users feel. Often using the latest technical innovations the market has to offer such as Artificial Intelligence (AI) and Machine Learning (ML). What better way to demonstrate innovative uses of these technology in a consumer market than capturing expressions from your users and then serving content based on that expression!

Read more
Improve Customer Data Protection with GDPR Implementation

TL;DR

Yay! We've done it! Gold-Star for us! We've talked with all the people, made all the changes, paid all the lawyers and checked all the boxes. GDPR? ✅Done!

Not so fast. Of course, conforming to the GDPR regulations introduced in Europe is just the beginning. This is a process and a state of mind that must become part of our long-term cultural ethos.

Read more