0

I'm trying to edit a word document using a REST API in Node.js .

Here's the code :

// api initialization
let editApi = groupdocs_editor_cloud.EditApi.fromKeys(clientId, clientSecret);
let fileApi = groupdocs_editor_cloud.FileApi.fromKeys(clientId, clientSecret);

// input file      
let fileInfo = new groupdocs_editor_cloud.FileInfo();
fileInfo.filePath = "Sample.docx";

// define load options
let loadOptions = new groupdocs_editor_cloud.WordProcessingLoadOptions();
loadOptions.fileInfo = fileInfo;
loadOptions.outputPath = "output";

// create load request
let loadRequest = new groupdocs_editor_cloud.LoadRequest(loadOptions);
let loadResult = await editApi.load(loadRequest);

// download html document
let downloadRequest = new groupdocs_editor_cloud.DownloadFileRequest(loadResult.htmlPath);
let buf = await fileApi.downloadFile(downloadRequest);
let htmlString = buf.toString("utf-8");

// edit something...
htmlString = htmlString.replace("Title of the document", "Welcome");
htmlString = htmlString.replace("Subtitle #1", "Hello world");

// upload html back to storage
let uploadRequest = new groupdocs_editor_cloud.UploadFileRequest(loadResult.htmlPath, new Buffer.from(htmlString, "utf-8"));
await fileApi.uploadFile(uploadRequest);

// save html back to docx
let saveOptions = new groupdocs_editor_cloud.WordProcessingSaveOptions();
saveOptions.fileInfo = fileInfo;
saveOptions.outputPath = "output/edited.docx";
saveOptions.htmlPath = loadResult.htmlPath;
saveOptions.resourcesPath = loadResult.resourcesPath;

// create save request
let saveRequest = new groupdocs_editor_cloud.SaveRequest(saveOptions);
let saveResult = await editApi.save(saveRequest);
console.log("Document edited: " + saveResult.path);

I am getting the below error when trying to execute this code :

Error: Required parameter "requestObj.path" was null or undefined when calling downloadFile.
       at FileApi.<anonymous> (C:\Users\souha.hayouni\Documents\Test-auto-IMPRESS\impress_front_automation\node_modules\groupdocs-editor-cloud\lib\editor_api.js:226:23)
       at Generator.next (<anonymous>)
       at C:\Users\souha.hayouni\Documents\Test-auto-IMPRESS\impress_front_automation\node_modules\groupdocs-editor-cloud\lib\editor_api.js:30:71
       at new Promise (<anonymous>)
       at __awaiter (C:\Users\souha.hayouni\Documents\Test-auto-IMPRESS\impress_front_automation\node_modules\groupdocs-editor-cloud\lib\editor_api.js:26:12)
       at FileApi.downloadFile (C:\Users\souha.hayouni\Documents\Test-auto-IMPRESS\impress_front_automation\node_modules\groupdocs-editor-cloud\lib\editor_api.js:217:16)
       at modifyFile (C:\Users\souha.hayouni\Documents\Test-auto-IMPRESS\impress_front_automation\features\Impress_stories\Download_generated_reports\Download_generated_reports.js:738:20)
       at World.<anonymous> (C:\Users\souha.hayouni\Documents\Test-auto-IMPRESS\impress_front_automation\features\Impress_stories\Download_generated_reports\Download_generated_reports.js:525:9)
       at World.<anonymous> (C:\Users\souha.hayouni\Documents\Test-auto-IMPRESS\impress_front_automation\features\support\_index.js:20:29)
       at World.arity0 (eval at module.exports (C:\Users\souha.hayouni\Documents\Test-auto-IMPRESS\impress_front_automation\node_modules\util-arity\arity.js:22:24), <anonymous>:3:39)

Source Code : https://blog.groupdocs.cloud/2021/07/28/edit-word-documents-using-rest-api-in-node.js/#:~:text=Document%20Editor%20REST%20API%20and%20Node.,-js%20SDK&text=js%20SDK%20of%20GroupDocs.,%2C%20TXT%2C%20HTML%2C%20XML.

macropod
  • 12,757
  • 2
  • 9
  • 21

1 Answers1

0

I tested the sample code using GroupDocs.Editor Cloud SDK for Node.js 22.5 and unable to notice any issue. Please try the latest release of the SDK, it will help you resolve the issue. Here is my complete script file code.

P.S: I am a developer evangelist at groupdocs.cloud.

// load the module

// For complete examples and data files, please go to https://github.com/groupdocs-editor-cloud/groupdocs-editor-cloud-node-samples
editor_cloud = require("groupdocs-editor-cloud");
 
const appSid = "xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx"; // Get AppKey and AppSID from https://dashboard.groupdocs.cloud
const appKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // Get AppKey and AppSID from https://dashboard.groupdocs.cloud
  
const editDocument = async () => {

// api initialization
editApi = editor_cloud.EditApi.fromKeys(appSid, appKey);
fileApi = editor_cloud.FileApi.fromKeys(appSid, appKey);
 
// The document already uploaded into the storage.
// Load it into editable state      
let fileInfo = new editor_cloud.FileInfo();
fileInfo.filePath = "02_pages.docx";
let loadOptions = new editor_cloud.WordProcessingLoadOptions();
loadOptions.fileInfo = fileInfo;
loadOptions.outputPath = "output";
let loadResult = await editApi.load(new editor_cloud.LoadRequest(loadOptions));
 
// Download html document
let downloadRequest = new editor_cloud.DownloadFileRequest(loadResult.htmlPath);
//let buf = await fileApi.downloadFile(new editor_cloud.DownloadFileRequest(loadResult.htmlPath));
let buf = await fileApi.downloadFile(downloadRequest);
let htmlString = buf.toString("utf-8");
 
// Edit something...
htmlString = htmlString.replace("Sample test text", "Hello world");
 
// Upload html back to storage
await fileApi.uploadFile(new editor_cloud.UploadFileRequest(loadResult.htmlPath, new Buffer(htmlString, "utf-8")));
 
// Save html back to docx
let saveOptions = new editor_cloud.WordProcessingSaveOptions();
saveOptions.fileInfo = fileInfo;
saveOptions.outputPath = "output/edited.docx";
saveOptions.htmlPath = loadResult.htmlPath;
saveOptions.resourcesPath = loadResult.resourcesPath;
let saveResult = await editApi.save(new editor_cloud.SaveRequest(saveOptions));
 
// Done.
console.log("Document edited: " + saveResult.path);

}

editDocument()
.then(() => {
console.log("Document edited successfully");
})
.catch((err) => {
console.log("Error occurred while editing the document:", err);
})
Tilal Ahmad
  • 940
  • 5
  • 9