Convert Doc to Pdf using NodeJS

To convert a DOC file to PDF using Node.js and PowerShell, you can use the following steps:

Install the ‘child_process’ package in your Node.js project by running the following command in your project’s terminal:

npm i child_process

Create a PowerShell script that converts the DOC file to PDF using Microsoft Word. You can use the following script:

$documents_path = 'C:\Users\SURAJ\Downloads\DoctoPdf\doc\Web_Service_Uing_Ajax_in_net.docx'
$output_path = 'C:\Users\SURAJ\Downloads\DoctoPdf\doc\Web_Service_Uing_Ajax_in_net12.pdf'
$word_app = New-Object -ComObject Word.Application
# This filter will find .doc as well as .docx documents
Get-ChildItem -Path $documents_path -Filter *.doc? | ForEach-Object {
    $document = $word_app.Documents.Open($_.FullName)
    $pdf_filename =  "$output_path"
    $document.SaveAs($pdf_filename)
    $document.Close()
}
$word_app.Quit()
return $output_path

GitHub Repo: https://github.com/surajvsk/doc-to-pdf-in-nodejs.git

  1. Make sure to replace the input and output file paths with the actual paths to your files.
  2. Use the ‘child_process’ package in your Node.js code to execute the PowerShell script. You can use the following code:
    const express =  require('express');
    const app =  express();
    const port = 5000;
    const { exec } = require('child_process');
    const { v4: uuidv4 } = require('uuid');
    const fileUpload = require('express-fileupload');
    const fs = require('fs');
    app.use(express.json({
        limit: "200mb"
      }));
      app.use(
        express.urlencoded({
          extended: true,
          limit: "200mb",
        })
      );
    
    
    app.use(fileUpload());
    
    app.post('/', function(req, res,next){
        console.log('req', req.files.inputfile)
    
        let uploaded_path = __dirname+'/doc/' + req.files.inputfile.name;
        fs.writeFile(uploaded_path, req.files.inputfile.data, function(err) {
            if(err) {
                return console.log(err);
            }
            ///console.log("The file was saved!");
            const cmd = `$documents_path = "${uploaded_path}"
            $output_path = '${__dirname}/doc/${uuidv4()}.pdf'
            $word_app = New-Object -ComObject Word.Application
            # This filter will find .doc as well as .docx documents
            Get-ChildItem -Path $documents_path -Filter *.doc? | ForEach-Object {
                $document = $word_app.Documents.Open($_.FullName)
                $pdf_filename = "$output_path"
                $document.SaveAs([ref] $pdf_filename, [ref] 17)
                $document.Close()
            }
            $word_app.Quit()
            return $output_path`; 
            exec(cmd, {'shell':'powershell.exe'}, (error, stdout, stderr)=> {
                console.log('Error', stderr)
                   console.log('OUTPUT', stdout);
                   res.status(200).json({status:200, msg:"success", stdout: stdout})
            })
        }); 
    
    })
    
    
    app.listen(port, function(){
        console.log('Server is running on port', port);
    })
  3. Make sure to replace the script path and input file path with the actual paths to your files.
  4. Run your Node.js code and the DOC file will be converted to PDF using PowerShell. The output PDF file will be saved to the path specified in the PowerShell script.


Similar Articles