Display Documents on mobile, tablet and desktop

The world’s most flexible document viewing solution

Download Trial Request a quote

How to run JPDF2HTML5 Converter on the Oracle Cloud

This tutorial explains how to setup the IDRsolutions JPDF2HTML5 converter or the JPDFForms converter on a Cloud service with a simple example. Both products utilize the same api and will be interchangable in the web application that we will create. The only difference will be which jar file you will use. This application will let you upload a file, convert that file into a HTML page and then will provide a link so that you can see the HTML page. It will be using the JPDF2HTML5-trial.jar which you candownload here or the JPDFForms trial jar which you can request from here.

If you are new to cloud, you may want to read the introductory article on the Java PDF blog‘How to use the Oracle Java Cloud Service with the NetBeans IDE’ before you start.

What you will need to have done before you start:

Create a new project

First lets create a Web application. 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.

1

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

serverAndSetings

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 deploy. You can do this by right clicking the top-most node of your project and clicking Run.

 

The Oracle Java Cloud runs Java 7 but at the moment our application is set up for Java 8. The latest version of WebLogic (12.2.1) insists on using Java 8 so that is why we set up the domain with it. In NetBeans though we can change how individual projects are built and which libraries they use, so we can make the application Java 7.

Right click your Project’s top most node and select Properties. On the first screen you should see a Source/Binary Format section. Select JDK 1.7 here.

jpdfBinary7

You also have to change the libraries used to build the project. To do this select the Libraries tab and change the Java Platform to JDK 7.

jpdfLibrary7

Modify 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 PDF2HTML5converter and we will create that in the next step.

Your index file should look like this:

Create the servlet / PDF2HTML5converter

At the end of this tutorial I will post the full code for this class. Those of you who feel fairly competent with web applications can just skip down there to browse the code. Everyone else can use the code as a quick reference to make sure they are on the right track.

This 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 and name your servlet. Click Finish.

selectServlet

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.
 

@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).
@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.
 

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 generateOutput() because that method that will carry out the download, which will be put into the response for the client.

Find Server Domains

This web application will create a file on your server that we are going to view in the browser. In order to create the file we must be able to supply a relative path to where we can store the file to serve. This path will most likely be different for your local and remote servers. I say likely because you could have the same domain name for both your local and remote servers. This step is for those who either don’t know or have different domain names.

Local Domain

In my previous tutorial we created and added a server instance to our NetBeans IDE. You can find the domain name for this server by going to the Services window, right clicking your WebLogic server and selecting Properties. As you can see my local domain is tutorialDomain. On Windows you can see which domains you have by navigating to a similar directory to C:\Oracle\user_projects\domains if your wlserver is set up in the Oracle folder.

localDomain

Remote Domain/Administration Server Domain

Please do not get your remote WebLogic domain confused with your Identity domain. They are two seperate things and we want the WebLogic domain or as it is called in your Java Cloud – the Administration Server Domain. To find out your remote domain you will have to log into your services: https://cloud.oracle.com/en_US/sign-in. From there you need to find your Java Cloud Service and select the Service Console link on the right.

JCSService

Then click on your Java Cloud servers name (I highlighted it in yellow).

myServicesNameHighlighte

Finally you can see in the virtual machines section your Administration Server Domain. Mine is idrsolut_domain.

remoteDomain

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

Now that we have the domains we can start creating our methods.

Convert the data into a HTML page / convertPdf2Html5()

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 into a HTML 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 Jpdf2html5 jar OR your JPDFForms jar. Click Open and then OK.

5

This is the easiest method. We can use the example code fromhereto help us. Since we aren’t using any options we can just create the HTMLConversionOptions() and IDRViewerOptions() as new objects. The Converter’s convert method takes the byte[] and output file as parameters.

Display the link to output / generateOutput()

Create the response that will be sent back to the client’s browser with a link to our newly generated output.

Configure weblogic.xml

Weblogic.xml is the configuration file for your application. In here you can define servlet parameters, jsp definitions and other elements like the context root and virtual directory mapping. We are going to create a virtual directory mapping so that when we want to serve our created document the server will know where to find it. WebLogic servers have document roots (which is where you usually store files to serve) but if we use virtual directory mapping we can store files in multiple places. We are also going to add the context root which forms part of the URL which will be used to call your application.

WebLogic.xml was created when you created your web application. It will be in your Web Pages WEB-INF directory. I’m going to set the context root to the name of my application. You need to update your weblogic.xml so that it is something similar to this:

Deploy and run application

Now that all of your coding is complete it is time to test your application. Right click your project’s top-most node and click Run. You should be greeted with a HTML page like this:

htmlConverterPage

Choose your file and click the convertbutton. Please note that large PDF sizes may cause the server to time out. If this happens you will receive a Proxy Error with the reason: Error reading from remote server. The page below should be displayed if there are no errors.

7

Once you click that link you should see something similar to below but with your converted PDF.

8

Once you have the application locally you can change your fileOutputPath (in the servlet) to use your remote domain instead of your local one e.g.

Then just create your war and upload it to the Oracle cloud. I have covered these steps in my previous tutorial so if you need a refresher on creating your war or uploading just skip over there and follow steps 6 and 7.

Complete PDF2HTML5converter code

IDRSolutions Limited 1999-2016