URL converter with wsclient library

Now we show you how to convert webpages to PDF with the URL-converter Webservice.

Important note:

The following coding example is based on the use of the webPDF wsclient library. In order to understand and apply the example, the following blog post should be considered first:

webPDF and Java: very easy with the “wsclient” library

Webservice call

At the beginning you should already have created a REST- or SOAP-session and thus be able to create either an UrlConverterWebService object (for a SOAP session) by calling the WebserviceFactory..

UrlConverterWebService urlConverterWebService =
    WebServiceFactory.createInstance(
        session, WebServiceType.URLCONVERTER
    );

..or an UrlConverterRestWebService object (for a REST session).

UrlConverterRestWebService urlConverterWebService =
    WebServiceFactory.createInstance(
        session, WebServiceType.URLCONVERTER
    );

Finally, either a RestDocument or a SoapDocument object is passed to this WebService object by calling the method setDocument().

Webservice Parameters

Just to get changing access to a document, it is often necessary to give the current open and/or permission password of the document to the webservice call. You can do this directly at the created urlConverterWebService object:

urlConverterWebService.getPassword().setOpen("password");
urlConverterWebService.getPassword().setPermission("password");

(If the document does not have an appropriate password protection, you can skip this point.)

URL Converter Webservice

The URL Converter Webservice is an endpoint of your webPDF server that allows you to convert web pages into PDF documents.

You retrieve the UrlConverterType object from your urlConverterWebService object as follows to pass further parameters to it:

UrlConverterType urlConverter = urlConverterWebService.getOperation();

The following parameters can be set on the urlConverter object:

url (default value: „“)

The URL of the page to be converted.

urlConverter.setUrl("https://www.webpdf.de");

The objekt „page“

The website is displayed on a number of pages of the PDF document. To determine the dimensions of these pages, add a PageType object to the urlConverter object:

PageType page = new PageType();
urlConverter.setPage(page);

The following parameters can be set for the Page object:

width (default value: 210)

Sets the width of the pages using the selected metrics.

page.setWidth(800);

height (default: 297)

Sets the height of the pages using the selected metrics.

page.setHeight(16);

bottom (default: 20)

Defines the bottom margin of the page.

page.setBottom(10);

top (default: 20)

Defines the upper margin of the page.

page.setTop(10);

left (default value: 20)

Defines the left margin.

page.setLeft(10);

right (default value: 20)

Sets the right margin.

page.setRight(10);

metrics (default value: „mm“)

The unit of measurement in which the dimensions are to be given. The following values can be set here:

  • mm = millimetre
  • px = Pixel
page.setMetrics(MetricsType.MM);

The objekt „basicAuth“

If authentication is required to access the URL, you can pass the necessary logon data in this object. To do this, add an HttpBasicAuthType object to the urlConverter object:

HttpBasicAuthType basicAuth = new HttpBasicAuthType();
urlConverter.setBasicAuth(basicAuth);

The following parameters can be set for the BasicAuth object:

username (default value: „“)

Specifies the username to be used for authentication.

basicAuth.setUserName("username");

password (default value: „“)

Specifies the password to be used for authentication.

basicAuth.setPassword("password");

The „proxy“ object

If access to the URL is only possible via a proxy server, you can pass on the necessary information in this object. Add a HttpProxyType object to the object urlConverter:

HttpProxyType proxy = new HttpProxyType();
urlConverter.setProxy(proxy);

The following parameters can be set for the Proxy object:

username (default value: „“)

Specifies the username to be used for authentication on the proxy server.

basicAuth.setUserName("username");

password (default value: „“)

Specifies the password to be used for authentication on the proxy server.

basicAuth.setPassword("password");

adress (default value: „“)

Defines the address of the proxy server.

proxy.setAddress("Proxy Adresse");

port (default value: 0)

Defines the port of the proxy server.

proxy.setPort(8080);

Webservice-call example

Here you will find a more detailed example of our entire webservice-call (for addressing the SOAP interface):

try (
    // Setup of a session with the webPDF server (here SOAP):
    SoapSession session = SessionFactory.createInstance(
        WebServiceProtocol.SOAP,
        new URL("http://localhost:8080/webPDF/")
    );
    // Make available the document to be processed 
   // and the file to which the result is to be written:
    SoapDocument soapDocument = new SoapDocument(
        new File("Pfad des Quelldokuments").toURI(),
        new File("Pfad des Zieldokuments")
    )
) {
    // Selection of the webservice via a factory:
    UrlConverterWebService urlConverterWebService =
        WebServiceFactory.createInstance(
            session, WebServiceType.URLCONVERTER
        );
    urlConverterWebService.setDocument(soapDocument);
    urlConverterWebService.getPassword().setOpen("password");
    urlConverterWebService.getPassword().setPermission("password");

    UrlConverterType urlConverter = urlConverterWebService.getOperation();

    urlConverter.setUrl("https://www.webpdf.de");

    PageType page = new PageType();
    urlConverter.setPage(page);

    page.setMetrics(MetricsType.MM);
    page.setWidth(800);
    page.setHeight(600);
    page.setTop(10);
    page.setRight(15);
    page.setBottom(10);
    page.setLeft(15);

    HttpBasicAuthType basicAuth = new HttpBasicAuthType();
    urlConverter.setBasicAuth(basicAuth);

    basicAuth.setUserName("username");
    basicAuth.setPassword("password");

    HttpProxyType proxy = new HttpProxyType();
    urlConverter.setProxy(proxy);

    proxy.setUserName("username");
    proxy.setPassword("password");
    proxy.setAddress("Proxy Adresse");
    proxy.setPort(8080);

    // execution.
    urlConverterWebService.process();
} catch (ResultException | MalformedURLException ex) {
    // For the evaluation of possible errors, the 
   // wsclient library provides corresponding methods:
}
  • More information about the URL-converter parameter structure and error-codes can be found in our documentation (here without examples for use with the wsclient library).
  •  Please also note: All parameters are preset with certain default values. If a default value is specified and does not differ from your desired value, then it is not necessary to set this parameter.

More coding examples for webservices that you can use with the ws-client library can be found here.