WebViewer Server API Wrapper

We offer a script file in the tools section of our WebViewer Server download package called WVSApiWrapper.js, this script allows the use of WebViewer Server using the extended API. This script currently provides functionality for PDF fetching and thumbnail fetching with future features planned.

This wrapper can be used with your WebViewer Server without the need for WebViewer. We have written it as a single Javascript file with no dependencies to allow ease of deployment.

Usage

JavaScript

1<script src="./WVSApiWrapper.js"></script>
2<script>
3 var wvs = WebViewerServer({
4 serverUrl: "http://<HOST_IP_ADDRESS or localhost>:8090"
5 });
6
7 wvs.getPDF({ uri: 'http://<HOST_IP_ADDRESS or localhost>:8090/myfile.pdf' }).then(function (pdfLink) {
8 window.open(pdfLink, "theFrame");
9 });
10</script>
11
12<html>
13 <iframe name="theFrame" width="1000" height="1000" ></iframe>
14</html>

Available Functions

WebViewerServer

Constructs a server object to communicate with WebViewer Server.

Parameters

options

object

An object containing options for the server.

serverUrl

string

The address to your WebViewer Server. Required.

Returns

The server object containing all callable functions for the Extended API wrapper.

Example

JavaScript

1const server = WebViewerServer({
2 serverUrl: 'http://pdftron.com/myserver'
3});

WebViewerServer.getPDF

Generates a PDF on the server from the passed URL. Returns a link to the completed PDF.

Parameters

options

object

An object containing options for the getPdfFromUrl call. The following are possible arguments:

uri

string

The uri of the document to generate a PDF from. Overwritten if the file argument is passed.

file

object

A file object for a document to generate a PDF from. Overwrites the uri argument if passed.

pdfa

boolean

If set to true, conversions will be done according to the PDFA format. This feature is only available when using the uri argument.

linearize

boolean

If set to true, the file returned will be linearized.

xfdf

string

Can be set to XFDF that should be merged with the file. Expects UTF-8 encoded XFDF data as a string.

customHeaders

object

The custom headers will be used when the server fetches the document requested through the uri argument. They should be passed as a JSON object.

JavaScript

1options: {
2 customHeaders: { Cookie: 'test=123', MyHeader: '123' }
3}

extension

string

The format of the document passed. Only required if the uri passed does not have the correct document extension.

cacheKey

string

The cacheKey to lock the cache to for this document. If not passed, caching is handled through normal mechanisms.

onProgress

function

A callback function that returns current progress on the job.

Returns

A promise which resolves to the completed URL for the PDF or rejects with an error.

Example

JavaScript

1function onprog(data) {
2 console.log(data);
3 // will print current progress
4 // { type: 'fetch' or 'upload', loaded: current progress, total: 'total'}
5}
6server.getPDF({
7 uri: 'http://pdftron.com/sample.docx,
8 extension: 'docx',
9 onProgress: onprog,
10}).then(uri => {
11 console.log(uri);
12 // do something with file link
13}).then(e => {
14 console.log(e);
15});
16// now we do the same, except with a file directly on the local system
17// get file from input element
18const selectedFile = document.getElementById('input').files[0];
19server.getPDF({
20 file: selectedFile
21 onProgress: onprog,
22}).then(uri => {
23 console.log(uri);
24 // do something with file link
25}).catch(e => {
26 console.log(e);
27});

WebViewerServer.getThumb

Generates a thumb (image render) of a PDF on the server from the passed URL or file object.

Parameters

options*

object

An object containing options for the getPdfFromUrl call. The following are possible arguments:

uri

string

The uri of the document to generate a PDF from. Overwritten if the file argument is passed.

file

object

A file object for a document to generate a PDF from. Overwrites the uri argument if passed.

onProgress

function

A callback function that returns current progress on the job.

customHeaders

object

The custom headers will be used when the server fetches the document requested through the uri argument. They should be passed as a JSON object.

JavaScript

1options: {
2 customHeaders: {Cookie: 'test=123', MyHeader: '123'}
3}

cacheKey

string

The cacheKey to lock the cache to for this document. If not passed, caching is handled through normal mechanisms.

size

string

The desired size of the rendered image. Allows for large (1280p) medium (640p) small 320p. Defaults to large.

page

number

The desired page to render of the PDF into an image. Page numbers are indexed starting at 0. Defaults to 0.

Returns

A promise which resolves to the completed URL for the image or rejects with an error.

Example

JavaScript

1function onprog(data) {
2 console.log(data);
3 // will print current progress
4 // { type: 'fetch' or 'upload', loaded: current progress, total: 'total'}
5}
6server.getThumb({
7 uri: 'http://pdftron.com/sample.docx,
8 extension: 'docx',
9 onProgress: onprog,
10}).then(uri => {
11 console.log(uri);
12 // do something with file link
13}).then(e => {
14 console.log(e);
15});
16// now we do the same, except with a file directly on the local system
17// get file from input element
18const selectedFile = document.getElementById('input').files[0];
19server.getThumb({
20 file: selectedFile
21 onProgress: onprog,
22}).then(function(uri) {
23 console.log(uri);
24 // do something with file link
25}).catch(function(e) {
26 console.log(e);
27});

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales