Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Split PDF Documents using a REST API in Node.js
global.clientId = "112f0f38-9dae-42d5-b4fc-cc84ae644972";
global.clientSecret = "16ad3fe0bdc39c910f57d2fd48a5d618";
global.myStorage = "";
const configuration = new groupdocs_merger_cloud.Configuration(clientId, clientSecret);
configuration.apiBaseUrl = "https://api.groupdocs.cloud";
// initialize api
let fileApi = groupdocs_merger_cloud.FileApi.fromConfig(configuration);
let folderApi = new groupdocs_merger_cloud.FolderApi(configuration);
// retrieve list of files
var listRequest = new groupdocs_merger_cloud.GetFilesListRequest("Output", myStorage);
let res = await folderApi.getFilesList(listRequest);
res.value.forEach(element => {
// download files
var request = new groupdocs_merger_cloud.DownloadFileRequest(element.name, myStorage);
fileApi.downloadFile(request)
.then(function (response) {
// save image file in working directory
fs.writeFile("C:\\Files\\merger\\downloads\\" + element.name, response, "binary", function (err) { });
console.log("Expected response type is Stream: " + response.length);
})
.catch(function (error) {
console.log("Error: " + error.message);
});
});
// api initialization
let documentApi = groupdocs_merger_cloud.DocumentApi.fromKeys(clientId, clientSecret);
let fileInfo = new groupdocs_merger_cloud.FileInfo();
fileInfo.filePath = "ten-pages.pdf";
// define split options
let options = new groupdocs_merger_cloud.SplitOptions();
options.fileInfo = fileInfo;
options.outputPath = "Output";
options.startPageNumber = 3;
options.endPageNumber = 7;
options.mode = groupdocs_merger_cloud.SplitOptions.ModeEnum.Pages;
// create split request
let splitRequest = new groupdocs_merger_cloud.SplitRequest(options);
// split document
let result = await documentApi.split(splitRequest);
result.documents.forEach(element => {
console.log("Document splitted: " + element.path);
});
// api initialization
let documentApi = groupdocs_merger_cloud.DocumentApi.fromKeys(clientId, clientSecret);
// provide input file path
let fileInfo = new groupdocs_merger_cloud.FileInfo();
fileInfo.filePath = "ten-pages.pdf";
// define split options
let options = new groupdocs_merger_cloud.SplitOptions();
options.fileInfo = fileInfo;
options.outputPath = "Output";
options.pages = [3, 6, 8];
options.mode = groupdocs_merger_cloud.SplitOptions.ModeEnum.Intervals;
// create split request
let splitRequest = new groupdocs_merger_cloud.SplitRequest(options);
// split document
let result = await documentApi.split(splitRequest);
result.documents.forEach(element => {
console.log("Document splitted: " + element.path);
});
// api initialization
let documentApi = groupdocs_merger_cloud.DocumentApi.fromKeys(clientId, clientSecret);
// provide input file path
let fileInfo = new groupdocs_merger_cloud.FileInfo();
fileInfo.filePath = "ten-pages.pdf";
// define split options
let options = new groupdocs_merger_cloud.SplitOptions();
options.fileInfo = fileInfo;
options.outputPath = "Output";
options.pages = [1, 3];
options.mode = groupdocs_merger_cloud.SplitOptions.ModeEnum.Pages;
// create split request
let splitRequest = new groupdocs_merger_cloud.SplitRequest(options);
// split document
let result = await documentApi.split(splitRequest);
result.documents.forEach(element => {
console.log("Document Path: " + element.path);
});
// construct FileApi
let fileApi = groupdocs_merger_cloud.FileApi.fromConfig(configuration);
let file = 'C:\\Files\\merger\\ten-pages.pdf';
// read files one by one
fs.readFile(file, (err, fileStream) => {
// create upload file request
let request = new groupdocs_merger_cloud.UploadFileRequest("ten-pages.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