groupdocs-free-consulting / projects

0 stars 0 forks source link

Converting PDF files to DOCX or RTF using Groupdocs Cloud API in Node.js #38

Open boasbakker opened 1 year ago

boasbakker commented 1 year ago

Hello, I am trying to convert large PDF files to DOCX or RTF using the GroupDocs Cloud API in Node.js. However, I keep running into issues. I want to upload a locally stored file, convert it to a docx or rtf file, and then download it to a local folder again. This is my current code:

import * as groupdocs_conversion_cloud from "groupdocs-conversion-cloud";
async function PDFtoDOCX3(){
  var clientId : string = "";
  var clientSecret : string = "";
  var name = "input.pdf";
  var path : string = "C:\\Users\\Boas\\Documents\\" + name;
  let file = fs.readFileSync(path);
  let request = new groupdocs_conversion_cloud.ConvertDocumentDirectRequest("docx", file);
  let convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
  try {
    let result = await convertApi.convertDocumentDirect(request);
    fs.writeFile("converted.docx", result, "binary", function (err) { console.log(err.message)});
    console.log("Document converted: " + result.length);
  }
  catch (error) {
    console.log(error);
  }
}

When running this code, the program gets stuck and nothing happens After a very long time I get this response: { message: 'Error while parse server error: SyntaxError: Unexpected token < in JSON at position 0' } How can I fix this?

tilalahmad commented 1 year ago

@boasbakker

Please check out the sample code to convert PDF to DOCX without cloud storage. Hopefully, it will help you accomplish the task. Otherwise, please feel free to post your questions or concerns in our free forum.

// load the module
var GroupDocs = require('groupdocs-conversion-cloud');
var fs = require('fs');

// get your appSid and appKey at https://dashboard.groupdocs.cloud (free registration is required).
var appSid = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx";
var appKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

const convertDocumentOnline = async () => {
// construct Api
const api = GroupDocs.ConvertApi.fromKeys(appSid, appKey);

//const convertOptions = new GroupDocs.DocxConvertOptions();

const file = fs.readFileSync('C:/Temp/tes.pdf');
const localPath = "C:/Temp/tes.docx";

let loadOptions = new GroupDocs.PdfLoadOptions();
loadOptions.format = 'pdf';
let convertOptions = new GroupDocs.ConvertOptions();
convertOptions.pageSize = GroupDocs.WordProcessingConvertOptions.PageSizeEnum.Custom;

try {
let request = new GroupDocs.ConvertDocumentDirectRequest("docx",file,undefined,undefined,loadOptions,convertOptions);
console.log(request); 
let result = await api.convertDocumentDirect(request);

//console.log(result);                  
fs.writeFileSync(localPath, result);
} catch (err) {
throw err;
}
}

convertDocumentOnline()
.then(() => {
console.log("Document converted successfully");
})
.catch((err) => {
console.log("Error occurred while converting the document:", err);
})
boasbakker commented 1 year ago

What is the appSid and appKey variale? I only have the Client Id and Client Secret variables in my dashboard

boasbakker commented 1 year ago

I also get this error: [{ "resource": "", "owner": "typescript", "code": "2339", "severity": 8, "message": "Property 'pageSize' does not exist on type 'ConvertOptions'.", "source": "ts", "startLineNumber": 212, "startColumn": 16, "endLineNumber": 212, "endColumn": 24 }]

boasbakker commented 1 year ago

When removing the line: convertOptions.pageSize = GroupDocs.WordProcessingConvertOptions.PageSizeEnum.Custom; And using the clientID as appSid and the clientSecret as appKey, I get this error: Error occurred while converting the document: { message: 'Error while parse server error: SyntaxError: Unexpected token < in JSON at position 0' } On the GroupDocs Dashboard, the conversion shows up as successful though

tilalahmad commented 1 year ago

@boasbakker Please share your credentials in a private message with us. We will investigate the issue and guide you.

boasbakker commented 1 year ago

@tilalahmad The link you sent for sending private messages is for the forums, not for GitHub