How-To: How can I use the webservices of webPDF 7?

Minimum technical requirements

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

Light bulb image: guide and tutorial

This how-to was created for webPDF 7 and later versions. The use of webservice interfaces changed fundamentally with this version. For webPDF 6, you can find the tutorial here.

Webservices of webPDF 7

This example shows how to use webPDF webservices. The Converter webservice is used to convert a selected file into a PDF document. Java is used to call the webservice.

To follow this example on your PC, you need a locally installed webPDF, a JDK version 1.7 or higher, and IntelliJ (Community Edition is sufficient).

Setting up the project with IntelliJ

After setting up the software environment, start creating the Java project in IntelliJ. In the start screen, click Create new Project. If IntelliJ already has open projects, use File -> New -> Project....

Then select Java as the project type (1) and choose your installed JDK. You may need to add it via New... (2) by selecting the JDK root folder.

Project setup step 1

Click Next. In the next dialog, choose template Command Line App and click Next.

In the next dialog, enter all details as shown in the screenshot and click Finish.

IntelliJ opens the created project after confirming directory creation. Open the project view using View -> Tool Windows -> Project. The view should now look similar to this screenshot:

Project view screenshot

The IntelliJ project setup is complete.

Creating the required proxy classes with wsimport

The target webservice uses the SOAP protocol. To use it, you need matching proxy classes (client stubs). Generate them in this step.

Open the project folder in IntelliJ and right-click the src folder (1).

In the context menu, select Copy Path. Open a command prompt, type cd , paste the copied path, and press Enter. You are now in your project source folder. Run the following command (2). If you see an error, your JDK is likely missing from environment variables.

Remark:

If webPDF is not installed on the default port (8080), adjust this and future URLs accordingly.

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

You can then see the generated proxy classes in package de.webpdf.schema._1_0 (3).

Proxy class generation screenshot

The required proxy classes are now available.

Implementing the webservice request

Insert the complete sample code into the already created main method of class Main.

Remark:

When entering the following code, unknown classes may be highlighted in red. To resolve imports, place the cursor on the class and press Ctrl+Space (multiple imports) or Alt+Enter (single import).

URL converterUrl;
try {
// initialize URL
} catch (MalformedURLException ex) {
// error handling
}

A URL object is initialized here. Because the constructor can throw an exception, initialization is done in a try-catch block. If MalformedURLException occurs, an error message is shown and the program should return, because further execution without a URL object is not useful.

// Container for all parameters (webservice-specific and general)
Operation converterOperation = new Operation();

// Container for webservice-specific parameters
converterOperation.setConverter(new ConverterType());

ConverterService converterService = new ConverterService(converterUrl);
Converter converter = converterService.getConverterPort();

First, an Operation element is initialized as the container for all webPDF parameters. Then a ConverterType element is created and added to the operation. This is the minimum required setup for the converter webservice. The URL is used to initialize a ConverterService object, which contains all ports/endpoints of the converter webservice. The endpoint is initialized as a Converter object retrieved from the service.

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

// Execute the converter webservice with a DataHandler
DataHandler inputHandler = new DataHandler(new FileDataSource(inputFile));
DataHandler resultHandler = null;

try {
// execute webservice and process result
} catch (WebserviceException | IOException ex) {
// error handling
}

Here, two DataHandler objects are initialized. One wraps a File object for the file to be converted. The other receives the conversion result in the try-catch block. The conversion is performed via the execute method on endpoint converter (type Converter) using converterOperation and inputHandler. As a third parameter, a URL can be passed instead of a DataHandler.

// Optional converter execution with a URL
resultHandler = null;
try {
// execute webservice with URL
} catch (WebserviceException | IOException ex) {
// error handling
}

This optional code does the same as above, but uses a URL instead of a DataHandler for the input file.

Result screenshot

Run the program.

You have converted a Java file into a PDF file via a webPDF webservice.

Congratulations!

Appendix

Required imports 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 can find part 2 and part 3 of our technology series on webservices.