Skip to content
sea edited this page Jun 2, 2023 · 7 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 REST:

Usage

// 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();

try {
   // 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.
   let session: RestSession<RestDocument> = await SessionFactory.createInstance(sessionContext, authProvider);
   // Prepare a webservice call for a webPDF webservice (CONVERTER in this example)
   let webService: ConverterWebService<RestDocument> = session.createInstance(session, WebServiceTypes.CONVERTER);

   // Upload the source file to the Server and receive a reference to the document.
   let filename: string = "lorem-ipsum.docx";
   let file: any = fs.readFileSync("./files/lorem-ipsum.docx");
   let restDocument: RestDocument = await session.getDocumentManager().uploadDocument(file, filename);
   // Or use the shortcut
   let restDocument: RestDocument = await session.uploadDocument(file, filename);

   // Parameterize the webservice call
   webService.setOperationParameters(
      Converter.fromJson({
         pages: "1-4",
         embedFonts: true,
         pdfa: {
            convert: {
               level: PdfaLevel._3b,
               errorReport: PdfaErrorReport.Message
            }
         }
      } as ConverterInterface)
   );

   // Execute the webservice call and receive a reference to the result document
   let resultDocument: RestDocument | undefined = await webService.process(uploadedFile);

   // Download the result document to your local file system
   let downloadedFile: Buffer = await resultDocument!.downloadDocument();
   fs.writeFileSync("./result/converter_rest.pdf", downloadedFile);

   await session.close();
} catch (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 {
   let session: RestSession<RestDocument> = await SessionFactory.createInstance(sessionContext, authProvider);
   let webService: UrlConverterWebService<RestDocument> = session.createWebServiceInstance(WebServiceTypes.URLCONVERTER);
   // ...
   let resultDocument: RestDocument | undefined = await webService.process();
}
// ...

Further usage examples

You can find further examples here.