wsclient for Java
The webPDF wsclient library for Java provides a simplified and optimized API for implementing a client to use the webPDF server.
It strictly complies to the "Generalized Usage Pattern".
| Language | Java |
| Repository | GitHub |
| Protocols | SOAP, REST |
Download
The wsclient library can be downloaded from Maven Central, by adding the following dependency to your Maven project:
<!-- https://mvnrepository.com/artifact/net.webpdf/webpdf-wsclient -->
<dependency>
<groupId>net.webpdf</groupId>
<artifactId>webpdf-wsclient</artifactId>
<version>9.0.0</version>
</dependency>
Usage
Using the wsclient a request to the webPDF Converter webservice could be as simple as:
- REST
- SOAP
// Initialize the session context:
SessionContext sessionContext = new SessionContext(WebServiceProtocol.REST, new URL("http://localhost:8080/webPDF/"));
// (Advanced options such as the configuration of a client truststore, a TLS-context and client side proxies are available.)
// Select an AuthProvider for the session:
AuthProvider authProvider = new AnonymousAuthProvider();
// Establish a session with the webPDF server:
try (RestSession<RestDocument> session = SessionFactory.createInstance(sessionContext, authProvider)) {
// Use the document manager to upload/download/delete/rename and access the history of documents in your session:
RestDocument restDocument = session.getDocumentManager().uploadDocument(new File("./files/lorem-ipsum.docx"));
// Select the webservice you want to call:
ConverterRestWebService<RestDocument> webService = session.createWebServiceInstance(WebServiceType.CONVERTER);
// Parameterize the webservice call. (Using predefined objects, that preselect and suggest which values may be set where):
webService.getOperationParameters().setPages("1-4");
webService.getOperationParameters().setEmbedFonts(true);
// Execute the webservice call and receive a reference to the result document:
RestDocument resultDocument = webService.process(restDocument);
// Download the result document to your local file system:
resultDocument.download(new File("./result/converter_rest.pdf"))
} catch (ResultException ex) {
// Handle server fail states using actual Exceptions, without having to parse the server´s responses yourself:
String msg = ex.getMessage();
Error error = ex.getClientError();
int errorCode = ex.getErrorCode();
Throwable cause = ex.getCause();
}
// Initialize the session context:
SessionContext sessionContext = new SessionContext(WebServiceProtocol.SOAP, new URL("http://localhost:8080/webPDF/"));
// (Advanced options such as the configuration of a client truststore, a TLS-context and client side proxies are available.)
// Select an AuthProvider for the session:
AuthProvider authProvider = new AnonymousAuthProvider();
// Establish a session with the webPDF server:
try (SoapSession<SoapDocument> session = SessionFactory.createInstance(sessionContext, authProvider)) {
// Select the webservice you want to call:
ConverterWebService<SoapDocument> webService = session.createWebServiceInstance(WebServiceType.CONVERTER);
// Parameterize the webservice call. (Using predefined objects, that preselect and suggest which values may be set where):
webService.getOperationParameters().setPages("1-4");
webService.getOperationParameters().setEmbedFonts(true);
// Select the source document:
SoapDocument sourceDocument = session.createDocument(new File("./files/lorem-ipsum.docx"));
// Execute the webservice call and receive a reference to the result document:
try (SoapDocument targetDocument = webService.process(sourceDocument)) {
// Download the result document to your local file system:
targetDocument.writeResult(new File("./result/converter_soap.pdf"));
}
} catch (ResultException ex) {
// Handle server fail states using actual Exceptions, without having to parse the server´s responses yourself:
String msg = ex.getMessage();
Error error = ex.getClientError();
int errorCode = ex.getErrorCode();
Throwable cause = ex.getCause();
}
Documentation and examples
You can find further documentation and usage examples in the project´s GitHub wiki.