简体   繁体   中英

how to convert office to pdf using libreoffice-converter in node.js?

after i downloaded LibreOffice software and started running the node.js code to change the format of one file to another(ie, office to pdf). it is showing errors.please help me with solution.here i used libreoffice-converted npm package.

//Here is the code

async function main(a,b,c,d) {

const ext = '.pdf'
const inputPath='./upload/'+d+"/"+a+b;
const outputPath= './upload/'+d+"/"+a+c;
// Read file
const docxBuf =  f.readFile(inputPath);

// Convert it to pdf format with undefined filter (see Libreoffice docs about filter)
let pdfBuf = await libre.convertAsync(docxBuf, ext, undefined,function(err,data){
  if(err){
    console.log("dada");
  }
  console.log(data);
  // Here in done you have pdf file which you can save or transfer in another stream
  f.writeFile(outputPath, data);
});
}
app.get("/upload/:foldername/:filename/:orgformat/:toformat",async function(req,res){
  const naoffo=req.params.foldername;
  const naoffi=req.params.filename;
  const orfo=req.params.orgformat;
  const tofo=req.params.toformat;
  const finawiex=return_filename(naoffi);
 if((orfo===".pptx"||orfo===".docx"||orfo===".xlsx")&& (tofo===".pdf")){  
     main(finawiex,orfo,tofo,naoffo);
  }
 else{
   console.log("errros");
 }
  });

sorry if i am wrong with my description and code.please and here are the errors.

node:internal/errors:465 ErrorCaptureStackTrace(err); ^

TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of Promise at Object.writeFile (node:fs:2123:5) at saveSource (C:\Users\User\Desktop\Webdevelopment\shareanywhere\node_modules\libreoffice-convert\index.js:46:36) at runTask (C:\Users\User\Desktop\Webdevelopment\shareanywhere\node_modules\async\dist\async.js:1155:17) at C:\Users\User\Desktop\Webdevelopment\shareanywhere\node_modules\async\dist\async.js:1091:35 at processQueue (C:\Users\User\Desktop\Webdevelopment\shareanywhere\node_modules\async\dist\async.js:1101:17) at Object.auto (C:\Users\User\Desktop\Webdevelopment\shareanywhere\node_modules\async\dist\async.js:1088:9) at convertWithOptions (C:\Users\User\Desktop\Webdevelopment\shareanywhere\node_modules\libreoffice-convert\index.js:15:18) at Object.convert (C:\Users\User\Desktop\Webdevelopment\shareanywhere\node_modules\libreoffice-convert\index.js:77:12) at node:internal/util:360:7 at new Promise () { code: 'ERR_INVALID_ARG_TYPE' }

You are missing the code that creates the promised function you are using:

libre.convertAsync = require('util').promisify(libre.convert)

There is the example code: https://github.com/elwerene/libreoffice-convert

You are missing the code that creates the promised function you are using:

libre.convertAsync = require('util').promisify(libre.convert)

Remember add the fs import for use the fs.readFile function

There is the example code: https://github.com/elwerene/libreoffice-convert

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM