The Ultimate Java PDF Library SDK

Seamless PDF Integration for Java

Download Trial Purchase

How to run the JPedal PDF to Image Converter on the Layershift Jelastic Cloud

This tutorial explains how to setup the JPedal PDF to Image Converter on the Jelastic Cloud service with a simple example. It will guide you through how to create a working web application. This application will let you upload a PDF file, convert each page of that file into a PNG file and then will download a zip containing those PNG’s to your computer. It will be using the JPedal-trial.jar which you candownload here.

What you will need to have done before you start:

Create a new project

First lets create a Web application in NetBeans. Right click in your Projects window and select New Project. Then choose the Java Web categories Web Application project. Click Next and give the project a meaningful name like PDF2ImageConverter.

1

On the Server and Settings screen you should select your Tomcatserver that you created in the previous tutorial. Java EE should be set to version 7. Select Finish.

serverAndSettings

You should get a project that’s structure resembles this:

projectStructure

At this point you can run your application to ensure it is set up and will correctly run locally. You can do this by right clicking the top-most node of your project and clicking Run.

Create index.jsp

Index.jsp lives in the Web Pages directory, which is where files should be placed if you want clients to be able to access them via their web browsers. Index.jsp will display a HTML form on the client’s computer, which will let you select a file and upload it to a servlet. The servlet works on the server’s machine and does all of the heavy work so that the client machine can just sit back and wait for it’s file to be converted. If you have an index.html then delete this so your index.jsp is always used as the default web page upon starting your application.
 
You also need to add a form with two elements: File and Submit. Your file should be given an ID as “pdfFile”. This is important because later on the servlet will use this ID to extract the file from the form request.
 
The form will post to a servlet called PDF2ImageConverter and we will create that in the next step.

Your index file should look like this:

You can test your applicaion locally now (just right-click and select Run) and your form should appear allowing you to select a file. The convert button will return an error page though as we have not created the servlet yet.

Create the servlet / PDF2ImageConverter

At the end of this tutorial I will post the full code for this class without the majority of my comments. You can use the code as a quick reference to make sure you are on the right track. The needed imports are also included in the bottom example.
 
Your servlet should be created and stored in the Source Packages directory since it is essentially a Java class. To create this right click your Source Packages directory, go to the New menu and select the Other menu item. This will open the New File wizard. Choose from the Web category a Servlet file type. Click Next,name your servlet “PDF2ImageConverter” and clickFinish.

selectServletFileType

As you can see servlets have some predefined methods like doPost() and processRequest(). When the servlet receives a request via the post method it’s doPost() method is called. This method calls processRequest() and passes in the HttpServletRequest and a HttpServletResponse. We can get any data that was passed along with the HttpServletRequest e.g. a file and similarly attach data to the HttpServletResponse e.g. HTML.
 
@WebServlet(urlPatterns = {“/yourServletName”}) should already be supplied. It is used to specify that the servlet is available at the specified URL pattern. This is how your index.jsp can locate your servlet when sending the form.
@MultipartConfig is needed to tell the servlet to expect requests conforming to the multipart/form-data MIME type (so that requests containing a file can be retrieved).
 

This class has 3 jobs so we will make 3 methods and call them in the processRequest method. Your new ProcessRequest() should look like below. We pass the request into getFile() because we need the request in order to extract the file from it. Similarlyresponse is passed into downloadFile() because that method that will carry out the download, which will be put into the response for the client.

Copy the PDF file to a local file on the server / getFile()

In your servlet we are going to add the first method which will copy the file to the server. You should be aware that different servers have different file systems. In this example we are using a Tomcat server both locally and on the Jelastic cloud. Don’t forget to add your imports too.

Convert the data into a PNG file / convertPdf2Image()

Before we can start this method we need to add the jar to the project library. If we don’t then we can’t use the functions and classes we need to convert the file. To add the jar, right click your project’s top most node and select Properties. Look down the category tree on the left and select Libraries. Then select the Add JAR/Folder button and navigate to your JPedal-trial.jar. Click Open and then OK.

jpedal5

We are using the simplest conversion with no options. We can use the example code fromhereto help us. This code creates a BufferedImage to hold the created PNG pages. ConvertPagesToImage() takes the byte[] and every page is converted in the for loop. ImageIO then writes the BufferedImage to the file we created earlier.

Download file onto the client’s computer / downloadFile()

We will create the downloadFile() method now. This will return a zipped folder to the client’s browser, which the browser will automatically download. We will do this by creating a byte[] from the zipped content of the output folder. We will carry out this functionality in another method called zipFiles().
 
ZipFiles() will create a byte[] by using a variety of InputStreams,OutputStreams and a buffer. The InputStreams are used when we want to read data in and the OutputStreams are used when we want the data to be written to another object. The buffer is used as a placeholder. We also need an int (length) which will tell the OutputStream how many bytes to read.

Test Locally

You can test your application locally by right-clicking the top most and selecting Run.

RunLocally

A new tab should then open. Once you have selected a file and clicked the convert button, a zipped folder should be downloaded by your browser containing your converted pdf.

localTest

Deploy to Jelastic cloud

Once you have your application working locally, it is time to deploy this application to the cloud. This is fairly simple to do once you have got your Jelastic plugin set up and your environment created (which I covered in my previous tutorial).

Firstly, you need to right click your applications parent node and select the Properties menu. From there select the Run category and change your Server to the environment we created on the cloud. Click OK.

projectProperties

Then right click your projects parent node and select Deploy.This will upload your web application to your Jelastic cloud.

deploy

Once your application is uploaded it will appear under your Cloud node in your Services window. You may have to refresh your Jelastic node (Right-clicking and selecting Refresh) for it to appear.

JelasticUploadedApp

Run your application

To run your application you need to right click it in your Services window and select Open App in browser.

runApp

A new tab should open up displaying your application running on your Tomcat environment on your cloud.

remoteapp

Complete PDF2ImageConverter code

IDRSolutions Limited 1999-2016