Convert PDF to Word documents in Node.js

To edit the content of a PDF file in Microsoft Word or other word processors that support DOC and DOCX formats. PDF files are editable, but DOC and DOCX files are more flexible and customizable.

Convert PDF to DOC

In case you want to convert PDF document, you can use AsposePdfToDoc function. Please check the following code snippet in order to convert in Node.js environment.

CommonJS:

  1. Call require and import asposepdfnodejs module as AsposePdf variable.
  2. Specify the name of the PDF file that will be converted.
  3. Call AsposePdf as Promise and perform the operation for converting file. Receive the object if successful.
  4. Call the function AsposePdfToDoc.
  5. Convert PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPDFtoDoc.doc”. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.

  const AsposePdf = require('asposepdfnodejs');
  const pdf_file = 'Aspose.pdf';
  AsposePdf().then(AsposePdfModule => {
      /*Convert a PDF-file to Doc and save the "ResultPDFtoDoc.doc"*/
      const json = AsposePdfModule.AsposePdfToDoc(pdf_file, "ResultPDFtoDoc.doc");
      console.log("AsposePdfToDoc => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
  });

ECMAScript/ES6:

  1. Import the asposepdfnodejs module.
  2. Specify the name of the PDF file that will be converted
  3. Initialize the AsposePdf module. Receive the object if successful.
  4. Call the function AsposePdfToDoc.
  5. Convert PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPDFtoDoc.doc”. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.

  import AsposePdf from 'asposepdfnodejs';
  const AsposePdfModule = await AsposePdf();
  const pdf_file = 'Aspose.pdf';
  /*Convert a PDF-file to Doc and save the "ResultPDFtoDoc.doc"*/
  const json = AsposePdfModule.AsposePdfToDoc(pdf_file, "ResultPDFtoDoc.doc");
  console.log("AsposePdfToDoc => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);

Convert PDF to DOCX

Aspose.PDF for Node.js via C++ toolkit lets you read and convert PDF documents to DOCX. DOCX is a well-known format for Microsoft Word documents whose structure was changed from plain binary to a combination of XML and binary files. Docx files can be opened with Word 2007 and lateral versions but not with the earlier versions of MS Word which support DOC file extensions.

In case you want to convert PDF document, you can use AsposePdfToDocX function. Please check the following code snippet in order to convert in Node.js environment.

CommonJS:

  1. Call require and import asposepdfnodejs module as AsposePdf variable.
  2. Specify the name of the PDF file that will be converted.
  3. Call AsposePdf as Promise and perform the operation for converting file. Receive the object if successful.
  4. Call the function AsposePdfToDocX.
  5. Convert PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPDFtoDocX.docx”. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.

  const AsposePdf = require('asposepdfnodejs');
  const pdf_file = 'Aspose.pdf';
  AsposePdf().then(AsposePdfModule => {
      /*Convert a PDF-file to DocX and save the "ResultPDFtoDocX.docx"*/
      const json = AsposePdfModule.AsposePdfToDocX(pdf_file, "ResultPDFtoDocX.docx");
      console.log("AsposePdfToDocX => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
  });

ECMAScript/ES6:

  1. Import the asposepdfnodejs module.
  2. Specify the name of the PDF file that will be converted
  3. Initialize the AsposePdf module. Receive the object if successful.
  4. Call the function AsposePdfToDocX.
  5. Convert PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPDFtoDocX.docx”. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.

  import AsposePdf from 'asposepdfnodejs';
  const AsposePdfModule = await AsposePdf();
  const pdf_file = 'Aspose.pdf';
  /*Convert a PDF-file to DocX and save the "ResultPDFtoDocX.docx"*/
  const json = AsposePdfModule.AsposePdfToDocX(pdf_file, "ResultPDFtoDocX.docx");
  console.log("AsposePdfToDocX => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);