Skip to content

Instantly share code, notes, and snippets.

@groupdocs-cloud-gists
Last active September 23, 2022 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save groupdocs-cloud-gists/3678caccf47794c8394a9371f8ea21b1 to your computer and use it in GitHub Desktop.
Save groupdocs-cloud-gists/3678caccf47794c8394a9371f8ea21b1 to your computer and use it in GitHub Desktop.
How to Convert PDF to JPG Image in Node.js using REST API
// construct FileApi to download converted file
var fileApi = groupdocs_conversion_cloud.FileApi.fromConfig(config);
// create donwload file request
let request = new groupdocs_conversion_cloud.DownloadFileRequest("nodejs-testing/sample-file.jpg", myStorage);
// download file and response type Stream
fileApi.downloadFile(request)
.then(function (response) {
// save file in your system directory
fs.writeFile("H:\\groupdocs-cloud-data\\sample-file.jpg", response, "binary", function (err) { });
console.log("Expected response type is Stream: " + response.length);
})
.catch(function (error) {
console.log("Error: " + error.message);
});
// open file in IOStream from your system drive.
var resourcesFolder = 'H:\\groupdocs-cloud-data\\sample-file.pdf';
// read file
fs.readFile(resourcesFolder, (err, fileStream) => {
// construct FileApi
var fileApi = groupdocs_conversion_cloud.FileApi.fromConfig(config);
// create upload file request
var request = new groupdocs_conversion_cloud.UploadFileRequest("sample-file.pdf", fileStream, myStorage);
// upload file
fileApi.uploadFile(request)
.then(function (response) {
console.log("Expected response type is FilesUploadResult: " + response.uploaded.length);
})
.catch(function (error) {
console.log("Error: " + error.message);
});
});
// How to convert PDF file to JPG image format using Node.js
const convert = async () => {
const convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
const settings = new groupdocs_conversion_cloud.ConvertSettings();
settings.storageName = myStorage;
settings.filePath = "nodejs-testing/sample-file.pdf";
settings.format = "jpg";
settings.outputPath = "nodejs-testing/sample-file.jpg";
try {
// Create convert document request
const request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings);
await convertApi.convertDocument(request);
}
catch (err) {
throw err;
}
}
convert()
.then(() => {
console.log("Successfully converted PDF to JPG file format.");
})
.catch((err) => {
console.log("Error occurred while converting the PDF document:", err);
})

You can convert PDF file to JPG image file programmatically on the cloud. In this article, you will learn how to convert PDF to JPG format in Node.js using REST API.

The following topics are covered in this article:

  1. PDF to Image Converter REST API and Node.js SDK
  2. How to Convert PDF to JPG format using REST API in Node.js
  3. PDF to JPG Conversion using Advanced Options
  4. Convert PDF to JPG without using Cloud Storage
  5. Convert PDF to JPG and Add Watermark
// How to Convert PDF to JPG file image with watermark.
const convertwatermark = async () => {
// api initialization
const convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
// define convert settings
const settings = new groupdocs_conversion_cloud.ConvertSettings();
settings.storageName = myStorage;
settings.filePath = "nodejs-testing/sample-file.pdf";
settings.format = "jpg";
settings.outputPath = "nodejs-testing/sample-file.jpg";
// define watermark options
let watermark = new groupdocs_conversion_cloud.WatermarkOptions();
watermark.text = "This is a Sample watermark";
watermark.color = "Red";
watermark.width = 250;
watermark.height = 300;
watermark.top = 150;
watermark.left = 300;
watermark.rotationAngle = 45;
watermark.bold = true;
watermark.background = false;
// define PDF convert options
let convertOptions = new groupdocs_conversion_cloud.JpgConvertOptions()
convertOptions.watermarkOptions = watermark;
settings.convertOptions = convertOptions;
try {
// Create convert document request
const request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings);
await convertApi.convertDocument(request);
}
catch (err) {
throw err;
}
}
convertwatermark()
.then(() => {
console.log("watermark added to converted PDF to JPG image.");
})
.catch((err) => {
console.log("Error occurred while converting the PDF document:", err);
})
// How to turn PDF to JPG using Advanced Options
const convert_options = async () => {
const convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
const settings = new groupdocs_conversion_cloud.ConvertSettings();
settings.storageName = myStorage;
settings.filePath = "nodejs-testing/sample-file.pdf";
settings.format = "jpg";
convertOptions = new groupdocs_conversion_cloud.JpgConvertOptions()
convertOptions.grayscale = true;
convertOptions.fromPage = 1;
convertOptions.pagesCount = 1;
convertOptions.quality = 100;
convertOptions.rotateAngle = 90;
convertOptions.usePdf = false;
settings.convertOptions = convertOptions;
settings.outputPath = "nodejs-testing/sample-file.jpg";
try {
// Create convert document request
const request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings);
await convertApi.convertDocument(request);
}
catch (err) {
throw err;
}
}
convert_options()
.then(() => {
console.log("Converted PDF to JPG image with advanced options.");
})
.catch((err) => {
console.log("Error occurred while converting the PDF file:", err);
})
// How to Convert PDF to JPG without using Cloud Storage
const convertdirect = async () => {
// api initialization
const convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
// input file
let file = fs.readFileSync('H:\\groupdocs-cloud-data\\sample-file.pdf');
try {
// Create convert document request
const request = new groupdocs_conversion_cloud.ConvertDocumentDirectRequest("jpg", file);
// convert document
let result = await convertApi.convertDocumentDirect(request);
// save to the local path
fs.writeFile("H:\\groupdocs-cloud-data\\sample-file.jpg", result, "binary", function (err) { });
}
catch (err) {
throw err;
}
}
convertdirect()
.then(() => {
console.log("Directly converted PDF to JPG file format.");
})
.catch((err) => {
console.log("Error occurred while directly converting the PDF file:", err);
})
# Import Node.js SDK in your node application from http://api.groupdocs.cloud
global.groupdocs_conversion_cloud = require("groupdocs-conversion-cloud");
global.fs = require("fs");
// get clientId and clientSecret from https://dashboard.groupdocs.cloud (free registration is required).
global.clientId = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
global.clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
global.myStorage = "test-internal-storage";
const config = new groupdocs_conversion_cloud.Configuration(clientId, clientSecret);
config.apiBaseUrl = "https://api.groupdocs.cloud";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment