Skip to content

Instantly share code, notes, and snippets.

@groupdocs-cloud-gists
Last active January 21, 2022 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groupdocs-cloud-gists/8b1c7d8db9b9addc93824af9385edab4 to your computer and use it in GitHub Desktop.
Save groupdocs-cloud-gists/8b1c7d8db9b9addc93824af9385edab4 to your computer and use it in GitHub Desktop.
Extract Images from PDF Files using a REST API in Node.js
// This code example demonstrates how to add your cliend ID and Secret in the code.
global.clientId = '659fe7da-715b-4744-a0f7-cf469a392b73';
global.clientSecret = 'b377c36cfa28fa69960ebac6b6e36421';
global.myStorage = "";
const configuration = new groupdocs_parser_cloud.Configuration(clientId, clientSecret);
configuration.apiBaseUrl = "https://api.groupdocs.cloud";
// This code example demonstrates how to download images from the cloud using Node.js
// Construct necessory api instances
var folderApi = groupdocs_parser_cloud.FolderApi.fromConfig(configuration);
var fileApi = groupdocs_parser_cloud.FileApi.fromConfig(configuration);
// Get files list request
var filesListRequest = new groupdocs_parser_cloud.GetFilesListRequest("parser/images/sample_pdf/", myStorage);
// Get files list
var filesList = await folderApi.getFilesList(filesListRequest);
for (var count = 0; count < filesList.value.length; count++) {
// Download file request
let request = new groupdocs_parser_cloud.DownloadFileRequest(filesList.value[count].path, myStorage);
// Download file
let response = await fileApi.downloadFile(request);
// Save file to the folder on disk
fs.writeFile("C:\\Files\\parser\\images\\" + filesList.value[count].name, response, "binary", function (err) { });
console.log(response);
}
// This code example demonstrates how to extract all the images from a PDF in Node.js.
//Api initialization
let parseApi = groupdocs_parser_cloud.ParseApi.fromConfig(configuration);
// Input file path
let fileInfo = new groupdocs_parser_cloud.FileInfo();
fileInfo.filePath = "sample.pdf";
// define image options
let options = new groupdocs_parser_cloud.ImagesOptions();
options.fileInfo = fileInfo;
// Image request
let request = new groupdocs_parser_cloud.ImagesRequest(options);
// Extract images
let result = await parseApi.images(request);
// Show results
let images = result.images;
images.forEach(image => {
console.log("Image path in storage: " + image.path);
console.log("Download url: " + image.downloadUrl);
console.log("Image format: " + image.fileFormat + ". Page index: " + image.pageIndex);
});
// This code example demonstrates how to extract images from a document attached in a PDF in Node.js.
// Api initialization
let parseApi = groupdocs_parser_cloud.ParseApi.fromConfig(configuration);
// Input file path
let fileInfo = new groupdocs_parser_cloud.FileInfo();
fileInfo.filePath = "PDF_with_Attachment.pdf";
fileInfo.password = "password";
// Define image options
let options = new groupdocs_parser_cloud.ImagesOptions();
options.fileInfo = fileInfo;
// Container item
options.ContainerItemInfo = new groupdocs_parser_cloud.ContainerItemInfo();
options.ContainerItemInfo.relativePath = "template-document.pdf";
// Image request
let request = new groupdocs_parser_cloud.ImagesRequest(options);
// Extract images
let result = await parseApi.images(request);
// Show results
let images = result.images;
images.forEach(image => {
console.log("Image path: " + image.path);
console.log("Image format: " + image.fileFormat + ". Page index: " + image.pageIndex);
});
// This code example demonstrates how to extract images from specific pages of a PDF in Node.js.
//Api initialization
let parseApi = groupdocs_parser_cloud.ParseApi.fromConfig(configuration);
// Input file path
let fileInfo = new groupdocs_parser_cloud.FileInfo();
fileInfo.filePath = "sample.pdf";
// Define image options
let options = new groupdocs_parser_cloud.ImagesOptions();
options.fileInfo = fileInfo;
options.startPageNumber = 1; // Start page number
options.countPagesToExtract = 1; // Total pages
// Image request
let request = new groupdocs_parser_cloud.ImagesRequest(options);
// Extract images
let result = await parseApi.images(request);
// Show results
let pages = result.pages;
pages.forEach(page => {
console.log("Page index: " + page.pageIndex);
page.images.forEach(image => {
console.log("Download url: " + image.downloadUrl);
console.log("Image format: " + image.fileFormat + ". Page index: " + image.pageIndex);
});
});
// This code example demonstrates how to upload a PDF to the cloud in node.js
// Construct FileApi
let fileApi = groupdocs_parser_cloud.FileApi.fromConfig(configuration);
// Input file path
let resourcesFolder = 'C:\\Files\\Parser\\sample.pdf';
fs.readFile(resourcesFolder, (err, fileStream) => {
// Upload file request
let request = new groupdocs_parser_cloud.UploadFileRequest("sample.pdf", fileStream, myStorage);
// Upload file
fileApi.uploadFile(request);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment