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:
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.
 

Recent Blog Posts

How the Right Tools and Training Drive SDR Success

Here at Cloudinary, I head a team of eight SDRs, who are responsible for creating the first impression potential customers have of our company’s brand. In just the first 10 months of 2017, our team of outbound SDRs have been responsible for sending more than 67,000 personalized emails and making more than 15,000 calls.

Read more
The JS video player that developers will love (How To)

It doesn't take a genius (or a statistician) to know that video represents a significant proportion of web and mobile content these days. But did you realize that in 2017, video will account for about 75% of all internet traffic and that 55% of people watch videos online every day? In fact, 52% of marketing professionals worldwide believe that video is the content type with the best ROI, with people spending up to 2.6x more time on pages with video than on those without.

Read more
 Beyond Drupal Media: Make Images and Video Fly with Cloudinary

Drupal is a very popular open source content management system (CMS) that has been deployed countless times by organizations and developers around the world. Drupal gained a reputation for being very flexible, powerful and robust in creating complex websites. With Drupal, you can create everything from plain websites, blogs and forums to ambitious enterprise systems.

Read more
Curbing Terrorist Content Online

Today, Cloudinary is proud to announce that it has joined The Global Internet Forum to Counter Terrorism (GIFCT), to help fight the spread of terrorist and violent extremist content on the Internet. The forum was established by Facebook, Microsoft, Twitter and YouTube in mid-2017. Cloudinary will contribute to the hash-sharing database, which all contributing companies can use to help identify and block terrorist related images and videos upon upload.

Read more
Introducing the complete video solution for developers

Videos in web sites and apps are starting to catch up with images in terms of popularity and they are a constantly growing part of the media strategy for most organizations. This means bigger challenges for developers who need to handle these videos in their web sites and mobile apps. Cloudinary's mission is to solve all developer needs around image and video management. In this blog post, we are excited to introduce Cloudinary's complete cloud-based video management solution for developers.

Read more
Getting Started with StencilJS

Stencil is basically a compiler, not necessarily a UI library. A compiler that transforms TSX (TypeScript + JSX) into self-contained custom components.

Before you start learning about the tool, it’s important to note that Stencil is not another heavy JavaScript framework you need to learn. If you have worked with Angular or React, or understand web components, then Stencil is worth a look.

Read more