How can I use the webservices of webPDF 7?

Minimum technical requirements

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

(This tutorial is created for webPDF 7 and following versions. The use of the webservice interfaces has changed fundamentally with this version. For webPDF 6 you can find this tutorial here.)

Webservices of webPDF 7

This example shows how to use the webservices of webPDF. The webservice “Converter” is used to convert a selected file into a PDF document. The Java programming language is used to control the webservice. To follow this example on your PC you need a locally installed webPDF, a JDK with version 1.7 or higher and the IDE IntelliJ (the Community Edition is sufficient for this purpose).

Setup of the project with IntelliJ

After you have set up the software environment, start creating the Java project in IntelliJ. After starting it, click on ‘Create new Project’ in the start view or, if you had already opened projects with IntelliJ, click on ‘File’->’New’->’Project…’ in the menu bar of the main view.

Then select ‘Java’ as project type(1) and select your installed JDK, which you probably need to add with ‘New …’(2) (you will need to select the main folder of the JDK in your file system).

how-to-webservices-webpdf-7_1011

Then click on ‘Next’. In the next dialog you will be asked if you want to use a template to create your project. Select the template ‘Command Line App’ and click on ‘Next’.

how-to-webservices-webpdf-7_101

In the next dialog enter all details as shown on the following screenshot and click on ‘Finish’.

how-to-webservices-webpdf-7_1021

Then IntelliJ will open the created project after asking if the directory may be created. Open the project view in the menu bar via ‘View’->’Tool Windows’->’Project’. The view should now look similar to the following screenshot:

how-to-webservices-webpdf-7_1031

The project for IntelliJ is set up.

Creation of the required proxy classes with wsimport

The web service to be addressed works with the SOAP protocol. To use this protocol, we need the appropriate proxy classes (“client stubs”). You will generate these in this step. Open the project folder in IntelliJ and right-click on the “src” folder(1).

In the opened selection select “Copy Path”. Now open the command prompt and type “cd “ and paste the path from the clipboard behind it. Press the Enter key and you are in the source folder of your project. There you insert the following command and execute it(2)(if you get an error message your JDK is probably not listed in the environment variables).

Remark:
If you have not installed webPDF on the default port (8080), you have to consider and adjust this in this URL and in future used URLs

wsimport -Xnocompile -s . http://localhost:8080/webPDF/soap/converter?wsdl -extension

Then you can see the created proxy classes directly in the package “de.webpdf.schema._1_0″(3).

how-to-webservices-webpdf-7_10121

The required proxy classes have been created.

Implementation of the webservice request

The entire sample code is inserted into the already created “main” method of the “Main” class. Now start creating the method content.

Remark:
If you enter the following code, the unknown classes will be marked with red letters. To resolve this class with corresponding “Import statements”, place the cursor on the class and press “Ctrl+Space” (if more imports are possible) or “Alt+Enter” (if only one import is possible).

URL converterUrl;
try {
    //wsdl URL if you use a different port you have to adjust the URL
    converterUrl = new URL("https://localhost:8080/webPDF/soap/converter?wsdl");
} catch (MalformedURLException ex) {
    System.err.println(ex.getMessage());
    return;
}

A URL object is initialized here. Since the constructor can throw an exception, the initialization takes place in a try-catch-block, which catches the malformedURLException when it occurs and outputs a message. Furthermore, in this case the program would be terminated with “return”, since further execution without the URL object would be pointless.

//Container for all parameters(Webservice specific and general - See documentation)
Operation converterOperation = new Operation();
//Container for webservice specific parameters(Must be inserted!)
converterOperation.setConverter(new ConverterType());

ConverterService converterService = new ConverterService(converterUrl);

Converter converter = converterService.getConverterPort();

First, an operation element is initialized, which is the container for all webPDF parameters. Then a ConverterType element is created and added to the operation. This is the minimum specification needed to use the converter webservice. The URL initializes an object of type ConverterService. This includes all ports, or current endpoints, of the converter Web service. The endpoint is then initialized in the form of a converter object by being queried by the service. Endpoints represent the final communication interface.

//File to convert
File inputFile = new File("src/de/webpdf/schema/_1_0/soap/converter/Converter.java");

//Executing the Converter Webservice with a Data Handler
DataHandler inputHandler = new DataHandler(new FileDataSource(inputFile));
DataHandler resultHandler = null;
try {
    resultHandler = converter.execute(converterOperation, inputHandler, null);
    try (FileOutputStream fos = new FileOutputStream(new File("./ConverterFileContentResult.pdf"))) {
        resultHandler.writeTo(fos);
    }
} catch (WebserviceException | IOException ex) {
    System.err.println(ex.getMessage());
    return;
}

Here 2 DataHandler objects are initialized, one takes over a file object, the interface de.webpdf.schema._1_0.soap.converter.Converter into your src-folder, which is to be converted and the other gets in the Try-Catch block the result of the conversion assigned. The conversion is executed by calling the method ‘execute’ at the endpoint (the variable ‘converter’ of type Converter). The operation object converterOperation and the DataHandler inputHandler with the file to be converted are passed as parameters. With the third possible parameter, the value zero is passed; here, as an alternative to the DataHandler, a URL could be passed that refers to the file to be converted. The result is stored on your computer using FileOutputStream. The conversion is performed at this point.

//Executing the Converter Web Service with a URL
resultHandler = null;
try {
    resultHandler = converter.execute(converterOperation, null, inputFile.toURI().toURL().toString());
    try (FileOutputStream fos = new FileOutputStream(new File("./ConverterFileUrlResult.pdf"))) {
        resultHandler.writeTo(fos);
    }
} catch (WebserviceException | IOException ex) {
    System.err.println(ex.getMessage());
}

This code is optional and does the same as the code described above with the difference that a URL is used instead of a DataHandler for the file to be converted.

how-to-webservices-webpdf-7_3031

Now run the program

You have converted a JAVA file to a PDF file via a webPDF webservice.

Congratulations!

Attachment:
Imports required for the class

import de.webpdf.schema._1_0.operation.ConverterType;
import de.webpdf.schema._1_0.operation.Operation;
import de.webpdf.schema._1_0.soap.converter.Converter;
import de.webpdf.schema._1_0.soap.converter.ConverterService;
import de.webpdf.schema._1_0.soap.converter.WebserviceException;

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

Here you find part 2 and 3 of our technology series Webservices…