Skip to content
sea edited this page May 22, 2023 · 39 revisions

The webPDF server provides a broad palette of webservices and parameters - too many to describe the interface for each of them here. But you can find a description of all webservices in our user manual. You might find our parameter documentation helpful as well.

The following are basic usage examples that call the "Converter" webservice of a webPDF server via SOAP and REST:

Usage via REST

// Prepare the session´s context (Each new Session should receive a fresh SessionContext instance.)
SessionContext sessionContext = new SessionContext(WebServiceProtocol.REST,
   new URL("http://localhost:8080/webPDF/"));
// Select an AuthProvider for the session (Each new Session should receive a fresh AuthProvider instance.)
AuthProvider authProvider = new AnonymousAuthProvider();

// Prepare the session by selecting the webPDF REST interface
// Be aware: The session should be closed at the end, which will cause the logout and termination of the session.
// In this example this is done using a "try-with-resources" statement.
try (RestSession<RestDocument> session = SessionFactory.createInstance(sessionContext, authProvider)) {
   // Prepare a webservice call for a webPDF webservice (CONVERTER in this example)
   ConverterRestWebService<RestDocument> webService = session.createWebServiceInstance(WebServiceType.CONVERTER);

   // Upload the source file to the Server and receive a reference to the document.
   RestDocument restDocument = session.getDocumentManager().uploadDocument(new File("./files/lorem-ipsum.docx"));
   // Or use the shortcut
   RestDocument restDocument = session.uploadDocument(new File("./files/lorem-ipsum.docx"));

   // Parameterize the webservice call
   webService.getOperationParameters().setPages("1-4");
   webService.getOperationParameters().setEmbedFonts(true);

   OperationPdfa pdfa = new OperationPdfa();
   webService.getOperationParameters().setPdfa(pdfa);
   OperationConvertPdfa convertPdfa = new OperationConvertPdfa();
   pdfa.setConvert(convertPdfa);
   convertPdfa.setLevel(OperationConvertPdfa.LevelEnum._3B);
   convertPdfa.setErrorReport(OperationConvertPdfa.ErrorReportEnum.MESSAGE);

   // 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
   File target = new File("./result/converter_rest.pdf");
   try (OutputStream fileOutputStream = new FileOutputStream(target)) {
      session.getDocumentManager().downloadDocument(resultDocument, fileOutputStream);
   }
   // Or use the shortcut
   resultDocument.downloadDocument(target);
} catch (ResultException ex) {
   // Handle the exception as you see fit.
}

Currently only the UrlConverter webservice does not require a source document, you can use the unparameterized "process" method to execute it:

// ...
try (RestSession<RestDocument> session = SessionFactory.createInstance(sessionContext, authProvider)) {
   UrlConverterRestWebService<RestDocument> webService = session.createWebServiceInstance(WebServiceType.URLCONVERTER);
   // ...
   RestDocument resultDocument = webService.process();
}
// ...

Usage via SOAP

// Prepare the session´s context (Each new Session should receive a fresh SessionContext instance.)
SessionContext sessionContext = new SessionContext(WebServiceProtocol.SOAP,
   new URL("http://localhost:8080/webPDF/"));
// Select an AuthProvider for the session (Each new Session should receive a fresh AuthProvider instance.)
AuthProvider authProvider = new AnonymousAuthProvider();

// Prepare the session by selecting the webPDF SOAP interface
// Be aware: The session should be closed at the end, which will cause the logout and termination of the session.
// In this example this is done using a "try-with-resources" statement.
try (SoapSession<SoapDocument> session = SessionFactory.createInstance(sessionContext, authProvider)) {
   // Prepare a webservice call for a webPDF webservice (CONVERTER in this example)
   ConverterWebService<SoapDocument> webService = session.createWebServiceInstance(WebServiceType.CONVERTER);

   // Parameterize the webservice call
   webService.getOperationParameters().setPages("1-4");
   webService.getOperationParameters().setEmbedFonts(true);

   webService.getOperationParameters().setPdfa(new PdfaType());
   webService.getOperationParameters().getPdfa().setConvert(new PdfaType.Convert());
   webService.getOperationParameters().getPdfa().getConvert().setLevel(PdfaLevelType.LEVEL_3B);
   webService.getOperationParameters().getPdfa().getConvert().setErrorReport(PdfaErrorReportType.MESSAGE);

   // Select the source document
   SoapDocument sourceDocument = session.createDocument(new File("./files/lorem-ipsum.docx"));
   // Execute the webservice call and download the result to the target file
   try (SoapDocument targetDocument = webService.process(sourceDocument)) {
      // A SoapDocument contains handlers for both the source and the result document, for this reason the
      // "sourceDocument" and "targetDocument" are identical for SOAP.
      // You can now write the result to a file or OutputStream.
      targetDocument.writeResult(new File("./result/converter_soap.pdf"));

      // SoapDocuments should always be closed - it is recommended to wrap SoapDocuments in
      // a try-with-resources statement.
   }
} catch (ResultException ex) {
   // Handle the exception as you see fit.
}

Currently only the UrlConverter webservice does not require a source document, you can use the unparameterized "process" method to execute it:

// ...
try (SoapSession<SoapDocument> session = SessionFactory.createInstance(sessionContext, authProvider)) {
   UrlConverterWebService<SoapDocument> webService = session.createWebServiceInstance(WebServiceType.URLCONVERTER);
   // ...
   try (SoapDocument resultDocument = webService.process()) {
      // SoapDocuments should always be closed - it is recommended to wrap SoapDocuments in
      // a try-with-resources statement.
   }
}
// ...

Further usage examples

You can find further examples here.

Clone this wiki locally