Tutorial: How to use webPDF’s webservices

Minimum technical requirements

  • Java version: 7
  • webPDF version: 6
  • wsclient version: 1

This tutorial uses a simple example to show how you can use the webservices of webPDF. In it you will be working with the “Converter” webservice to convert a selected file to a PDF document. The Java programming language is used to access the webservice. You will need to have webPDF installed locally, a JDK of version 1.7 or higher, and the IntelliJ IDE (the Community Edition is adequate for this purpose) in order to run this example on your PC.

Setting up the project with IntelliJ

Once you have installed the software environment, you can begin creating the Java project in IntelliJ. Click ‘Create New Project‘ in the starting view after you’ve started the program, or click ‘File‘->’New‘->’Project…‘ in the main view if you have already had projects opened with IntelliJ.

Then select ‘Java‘ as the project type (1) and your installed JDK, which you may have to add using the ‘New …‘ button (2) (do this by selecting the Home Directory for JDK in your file system).

1.01.1

Now click ‘Next‘. The dialog box that appears will ask if you want to use a template to create your project. Select the ‘Command Line App‘ template and click ‘Next‘.

1.01

In the next dialog box, enter all the information as it appears in the following screen shot and then click ‘Finish‘.

1.02

IntelliJ will open the new project after asking if it should create the directory. Open the project view in the menu bar by clicking ‘View‘->‘Tool Windows‘->’Project‘. The view should resemble the following screen shot.

1.03

The project is now set up for IntelliJ.

Generating the required proxy classes using wsimport

The webservice you are activating works with the SOAP protocol. But in order to use it, you must have the correct proxy classes (client stubs), which you will generate in this step.

Open the project folder in IntelliJ and right-click the “src” folder (1). Select ‘Run cmd shell‘ in the menu that opens.

Enter and execute the following command in the opened prompt window (2) (if an error message appears then your JDK is probably not set within the environment variables).

Note:

If you have not installed webPDF in the prescribed port (8080), then you must make the necessary adjustments for this and other URLs used in the future.

wsimport -Xnocompile -s . http://localhost:8080/webPDF/Converter?WSDL

Thereafter you can see the generated proxy classes directly within the “net.webpdf.converter” package (3).

2.00.2

The required proxy classes have been generated.

Implementing the web service request

The entire example code is then entered into the main method of the previously created main class. Now you can begin creating the method content.

Note:

When you enter the following code, all the unknown classes will be marked in red type. To resolve this class with the appropriate import statements, place the cursor on the class and press Ctrl+Space (if multiple imports are possible) or Alt+Enter (if only one import is possible).

URL url = null;
        try {
            url = new URL("https://localhost:8080/webPDF/Converter?WSDL");
        } catch (MalformedURLException ex) {
            System.err.println(ex.getMessage());
            return;
        }

Here is where a URL object is initialized. Because the constructor can throw an exception, the initialization takes place in a try/catch block, which catches the exception and displays a message when a MalformedURLException occurs. In this case the program would otherwise be ended immediately with “return” since it would be pointless to further execute it without the URL object.

QName qname = new QName("http://converter.webpdf.net/", "ConverterService");
      ConverterService service = new ConverterService(url, qname);
      Converter endpoint = service.getConverterPort();

This is followed by a QName object being initialized. This is necessary to correctly resolve the XML namespaces in WSDL. An object of the type ConverterService is initialized with the URL and the QName and which contains all of the converter web service’s ports and current endpoints. The endpoint is then initialized in the form of a Converter object by being requested by the service. Endpoints represent the final communications interface.

DataHandler fileContent = new DataHandler(new FileDataSource(new   File("./src/net/webpdf/converter/Converter.java")));
DataHandler result = null;
  try {
  result = endpoint.convertDocument(new ConverterOptions(),        fileContent);
      } catch (ConverterException_Exception ex) {
           System.out.println("Exception while converting");
           System.err.println(ex.getMessage());
           return;
      }

Here is where two DataHandler objects are initialized. One takes over a File object which references the file ‘C:\ConverterExample\src\net\webpdf\converter\Converter.java’ that is to be converted. The other receives the result of the conversion in the try/catch block. The conversion is executed by invoking the ‘convertDocument’ method at the endpoint (the ‘endpoint’ variable of type Converter). In so doing, a raw object of the ConverterOptions type is specified as the parameter, and with which the Converter could have been given options. We will discuss more about this in future examples.

try {
            result.writeTo(new FileOutputStream(new File("./ConverterResult.pdf")));
        } catch (IOException ex) {
            System.err.println(ex.getMessage());
        }

In the last step, the file contained in the DataHandler object is written in your project directory using a FileOutputStream object.

The desired PDF file should be in the project folder once the program has finished running.

3-eng

Congratulations! You have just used a webPDF web service to convert a Java file to a PDF file.

Attachment:

Imports required for the class:

import net.webpdf.converter.Converter;
import net.webpdf.converter.ConverterException_Exception;
import net.webpdf.converter.ConverterOptions;
import net.webpdf.converter.ConverterService;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

You can find our technical webservices for webPDF 7.0 here…