Cloudinary Blog

Uploading, converting and generating thumbnails for PDF documents

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:
Copy to clipboard
Cloudinary::Uploader.upload("SinglePageSample.pdf", :public_id => "single_page_pdf"
Python:
Copy to clipboard
cloudinary.uploader.upload("SinglePageSample.pdf", public_id = 'single_page_pdf')
Node.js:
Copy to clipboard
cloudinary.uploader.upload("SinglePageSample.pdf", function(result) { }, 
                           {public_id: 'single_page_pdf'})
PHP:
Copy to clipboard
\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: 
Copy to clipboard
<%= 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:
Copy to clipboard
<%= 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:
Copy to clipboard
<%= 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:
Copy to clipboard
{  
"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:
Copy to clipboard
\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.
 

Recent Blog Posts

Generate Waveform Images from Audio with Cloudinary

This is a reposting of an article written by David Walsh. Check out his blog HERE!
I've been working a lot with visualizations lately, which is a far cry from your normal webpage element interaction coding; you need advanced geometry knowledge, render and performance knowledge, and much more. It's been a great learning experience but it can be challenging and isn't always an interest of all web developers. That's why we use apps and services specializing in complex tasks like Cloudinary: we need it done quickly and by a tool written by an expert.

Read more
Make All Images on Your Website Responsive in 3 Easy Steps

Images are crucial to website performance, but most still don't implement responsive images. It’s not just about fitting an image on the screen, but also making the the image size relatively rational to the device. The srcset and sizes options, which are your best hit are hard to implement. Cloudinary provides an easier way, which we will discuss in this article.

Read more

The Future of Audio and Video on the Web

By Prosper Otemuyiwa
The Future of Audio and Video on the Web

Web sites and platforms are becoming increasingly media-rich. Today, approximately 62 percent of internet traffic is made up of images, with audio and video constituting a growing percentage of the bytes.

Read more

Embed Images in Email Campaigns at Scale

By Sourav Kundu
Embed Images in Email Campaigns at Scale

tl;dr

Cloudinary is a powerful image hosting solution for email marketing campaigns of any size. With features such as advanced image optimization and on-the-fly image transformation, backed by a global CDN, Cloudinary provides the base for a seamless user experience in your email campaigns leading to increased conversion and performance.

Read more
Build the Back-End For Your Own Instagram-style App with Cloudinary

Github Repo

Managing media files (processing, storage and manipulation) is one of the biggest challenges we encounter as practical developers. These challenges include:

A great service called Cloudinary can help us overcome many of these challenges. Together with Cloudinary, let's work on solutions to these challenges and hopefully have a simpler mental model towards media management.

Read more

Build A Miniflix in 10 Minutes

By Prosper Otemuyiwa
Build A Miniflix in 10 Minutes

Developers are constantly faced with challenges of building complex products every single day. And there are constraints on the time needed to build out the features of these products.

Engineering and Product managers want to beat deadlines for projects daily. CEOs want to roll out new products as fast as possible. Entrepreneurs need their MVPs like yesterday. With this in mind, what should developers do?

Read more