Convert PDF to Image using Javascript
Table of contents
- Introduction
- Prerequisites
- Code Example
- Return result to a callback url
- Configuration Options
- Upload by URL
- Using Authentication
- Further details
Introduction
The following tutorial shows you how to convert PDFs to images using a hosted JPedal cloud API. You can set up your own self-hosted JPedal microservice
Whilst the above service can be accessed with plain old HTTP requests, this tutorial uses our open source Javascript IDRCloudClient which provides a simple Javascript wrapper around the REST API.
Prerequisites
To add the client to your project use will need to add the file idrcloudclient.js to your project and include the following line to access it:
<script src="path/to/idrcloudclient.js" type="text/javascript"></script>
Code Example
Here is a basic code example to convert PDFs to images. Configuration options and advanced features can be found below.
var endpoint = 'https://my-self-hosted-service.com/' + IDRCloudClient.JPEDAL;
var parameters = {
input: IDRCloudClient.UPLOAD,
file: 'path/to/exampleFile.pdf',
settings: '{"mode":"convertToImages","format":"png"}'
}
function progressListener(e) {
console.log(JSON.stringify(e));
}
function failureListener(e) {
console.log(e);
console.log('Failed!');
}
function successListener(e) {
console.log(JSON.stringify(e));
console.log('Download URL: ' + e.downloadUrl);
}
IDRCloudClient.convert({
endpoint: endpoint,
parameters: parameters,
// The below are the available listeners
progress: progressListener,
success: successListener,
failure: failureListener
});
An example using the Javascript client can be found here.
Return result to a callback url
The JPedal Microservice accepts a callback url to send the status of a conversion on completion. Using a callback url removes the need to poll the service to determine when the conversion is complete.
The callback url can be provided to the parameters variable as shown below.
var parameters = {
input: IDRCloudClient.UPLOAD,
callbackUrl: 'http://listener.url',
file: 'path/to/exampleFile.pdf',
settings: '{"mode":"convertToImages","format":"png"}'
}
Configuration Options
The JPedal API accepts a stringified JSON object containing key value pair configuration options to customise your conversion. The settings should be added to the parameters array. A full list of the configuration options to convert PDFs to images can be found here.
settings: '{"key":"value","key":"value"}'
Upload by URL
As well as uploading a local file you can also provide a URL which the JPedal Microservice will download and then perform the conversion. To do this you should replace the input and file values in the parameters variable with the following.
input: IDRCloudClient.DOWNLOAD
url: 'http://exampleURL/exampleFile.pdf'
Using Authentication
If you have deployed your own JPedal Microservice that requires a username and password to convert PDFs to images, you will need to provide them with each conversion. These are provided by passing two variables named username and password to the convert method as shown below.
username: 'username',
password: 'password',
Further details
IDRCloudClient on GitHub
IDRCloudClient Online Example
JPedal Microservice API
JPedal Microservice Use