Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert PDF to Editable Word Document using a REST API in Node.js
You can easily convert any of your PDF documents into editable Word documents programmatically. This conversion will let you update the contents of your PDF documents using Microsoft Word. As a Node.js developer, you can convert PDF to Word documents (DOC or DOCX) programmatically on the cloud. This article will be focusing on how to convert PDF to an editable Word document using a REST API in Node.js.
The following code examples are provided here:
1. Cloud API Configurations
2. Upload PDF File to the cloud
3. Convert PDF to Editable Word using a REST API in Node.js
4. Convert Specific Pages of PDF to DOCX in Node.js
5. Convert Range of Pages from PDF to DOCX in Node.js
6. PDF to Word Conversion with Advanced Options
7. Convert PDF to Word without using Cloud Storage
8. Download converted DOCX file
global.clientId = "112f0f38-9dae-42d5-b4fc-cc84ae644972";
global.clientSecret = "16ad3fe0bdc39c910f57d2fd48a5d618";
global.myStorage = "";
const config = new groupdocs_conversion_cloud.Configuration(clientId, clientSecret);
config.apiBaseUrl = "https://api.groupdocs.cloud";
// read file from local disk
let file = fs.readFileSync('C:\\Files\\sample.pdf');
// create convert document direct request
let request = new groupdocs_conversion_cloud.ConvertDocumentDirectRequest("docx", file);
// convert document directly
let result = await convertApi.convertDocumentDirect(request);
// save file in working dorectory
fs.writeFile("C:\\Files\\Conversion\\sample_direct.docx", result, "binary", function (err) { });
console.log("Document converted: " + result.length);
// initialize api
let convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
// define convert settings
let settings = new groupdocs_conversion_cloud.ConvertSettings();
settings.filePath = "sample.pdf"; // input file path on the cloud
settings.format = "docx"; // output format
settings.outputPath = "output"; // output file folder on the cloud
// create convert document request
let request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings);
// convert document
let result = await convertApi.convertDocument(request);
console.log("Document converted successfully: " + result[0].url);
// initialize api
let convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
// define convert settings
let settings = new groupdocs_conversion_cloud.ConvertSettings();
settings.filePath = "sample.pdf";
settings.format = "docx";
// define docx convert options
let convertOptions = new groupdocs_conversion_cloud.DocxConvertOptions();
convertOptions.fromPage = 1; // set start page number
convertOptions.pagesCount = 2; // total pages to convert
settings.convertOptions = convertOptions
settings.outputPath = "range_of_pages.docx";
// create convert document request
let request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings);
// convert document
let result = await convertApi.convertDocument(request);
console.log("Document converted successfully: " + result[0].url);
// initialize api
let convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
// define convert settings
let settings = new groupdocs_conversion_cloud.ConvertSettings();
settings.filePath = "sample.pdf";
settings.format = "docx";
// define docx convert options
let convertOptions = new groupdocs_conversion_cloud.DocxConvertOptions();
convertOptions.pages = [1,2]; // set page numbers to convert
settings.convertOptions = convertOptions
settings.outputPath = "specific_pages.docx";
// create convert document request
let request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings);
// convert document
let result = await convertApi.convertDocument(request);
console.log("Document converted successfully: " + result[0].url);
// construct FileApi
var fileApi = groupdocs_conversion_cloud.FileApi.fromConfig(config);
// create download file request
let request = new groupdocs_conversion_cloud.DownloadFileRequest("output/sample.docx", myStorage);
// download file
let response = await fileApi.downloadFile(request);
// save file in your working directory
fs.writeFile("C:\\Files\\sample.docx", response, "binary", function (err) { });
console.log(response);
var resourcesFolder = 'C:\\Files\\sample.pdf';
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.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