skip to Main Content

How to convert pdf file into DOCx in node js.
I want to convert pdf content into a doc so that we can edit the pdf on client end

2

Answers


  1. To convert a PDF file into a DOCX format in Node.js, you can use external libraries like pdf2docx. Here’s a basic example of how you can achieve this:

    First, make sure you have Node.js installed on your machine. Then, create a new Node.js project and initialize it with npm init.

    Next, install the pdf2docx package:

    npm install pdf2docx
    

    After installing the package, you can create a script to convert a PDF file to DOCX:

    javascript

    const fs = require('fs');
    const pdf2docx = require('pdf2docx').converter;
    
    // Define input and output file paths
    const inputPdfFilePath = 'input.pdf';
    const outputDocxFilePath = 'output.docx';
    
    // Convert PDF to DOCX
    pdf2docx(inputPdfFilePath, outputDocxFilePath, function(err, result) {
      if (err) {
        console.error('Error converting PDF to DOCX:', err);
      } else {
        console.log('PDF converted to DOCX successfully');
        console.log('Output file:', outputDocxFilePath);
      }
    });
    

    Replace ‘input.pdf’ with the path to your PDF file and ‘output.docx’ with the desired path for the generated DOCX file.

    This script will read the input PDF file, convert it to DOCX format using pdf2docx, and save the resulting DOCX file to the specified output path.

    Once you have the DOCX file generated, you can provide it to your client for editing. Keep in mind that converting PDF to DOCX may not always result in perfect formatting, especially for complex PDFs. You may need to further process the DOCX file or use additional tools to achieve the desired result.

    Login or Signup to reply.
  2. Have you tried the package:

    puppeteer
    

    install the package – npm install puppeteer

    here is a example you can go through –

    const puppeteer = require('puppeteer');
    
    (async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    
    await page.goto('file://path/to/your/input.pdf');
    await page.emulateMedia('screen'); // Simulate screen size for better rendering
    await page.waitForSelector('.page'); // Wait for the PDF to load
    
    const outputBuffer = await page.exportFile('output.docx', { type: 'docx' });
    
    await browser.close();
    
    // Write the generated DOCX buffer to a file (optional)
    // fs.writeFileSync('output.docx', outputBuffer);
    
    console.log('Conversion completed successfully!');
    })();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search