Empower your business software with a cloud-based file format conversion library. This Aspose.PDF tutorial is about how to convert PDF to DOC in Node.js app.

pdf to doc converter

PDF To DOC Node.js

Overview

Welcome to another exciting blog post in the series of Aspose.PDF Cloud Product Family. In the recent past, we have published many articles such as Download Attachment from PDF using Python, Add Text and Image Footer to PDF using Python, and more. However, this is an Aspose.PDF tutorial in which we will learn about a REST API that acts as a PDF to DOC converter for your business application. Therefore, we will cover this guide by covering the following points.

What is Aspose.PDF & file format conversion?

When it comes to scaling, maintaining, and streamlining an online business application, it is always vital to opt for cloud infrastructure. Aspose.cloud offers cloud-based cross-platform REST APIs for many file format conversions in multiple languages such as JavaScript, Python, and many more. Aspose.PDF is one of the cloud-based product families that offers file format conversion of the most popular and widely used file format Portable Document Format(PDF). Since file format conversion is the process of converting data files from one format to another. However, this online file convertor(Aspose.PDF) enables you to convert your business/personal PDF files to any other popular file format such as PDF to MS Doc and DocX, PDF to HTML, PDF to SVG, and many more. Above all, you can try the cloud APIs using a live editor.

In addition, PDF file format is highly used in the corporate sector due to its efficiency and capability. Moreover, it has the capability to carry media elements, hyperlinks, visual representation, digital signatures, and many other types of information. Further, there are compatibility issues attached with various file formats and it is required to convert data files from one format to other. Therefore, Aspose.PDF comes up with all capabilities to convert one file format to another, and the PDF to DOC converter is one of them that we are going to explore in this PDF To DOC Node.js library tutorial.

How to setup Aspose.PDF Cloud?

In this section, we will see how to get started with Aspose.PDF.

Assumption: Before moving ahead, make sure you have installed the following simple pre-requisites on your system.

Once you have set up the above requirements, navigate to this dashboard of Aspose.cloud and sign in. This dashboard is a cloud space where you can get to know about subscription plans of cloud APIs and opt for any one that suits you. Moreover, it also provides storage, files, and application management. Now, open the application tab from the menu placed on the left-hand side and create your new application as shown in the image below.

PDF To DOC Node.js

While creating a new application, you can create your storage by choosing any option. Above all, you can opt for any third-party storage for your data files such as Google Drive, Dropbox, Amazon s3, and more. Once you are done, you will get your Client Id and Client Secret from your application UI as shown in the image below. This set of credentials will be used to make calls to the Aspose.PDF cloud APIs to make the PDF to DOC converter functional. Further, the secure way to store your API credentials is to use the environment variables. In the next section, we will see how to convert a PDF to DOC by using PDF To DOC Node.js library.

pdf to doc converter

How to convert a PDF to DOC using Aspose.PDF Cloud SDKs?

Till now, our local environment is set up, storage is generated, and have availed our credentials for cloud APIs.

The next step of this Aspose.PDF tutorial is to install write some code to practically practice the PDF to DOC converter using the SDKs. As we are going to implement in Node.js, therefore, we have chosen Nestjs. Although you can test the SDKs in other frameworks of Nodejs with a few code modifications.

Note: Please visit this link in case you want to set up Nestjs on your machine.

Once your server is up & running, go into the root directory and place the source PDF file that is to be converted to DOC as we have placed a PDF file ’m.pdf’ in this example. Further, we also have generated a folder “myfolder” in the cloud storage, which you can create as per your choice. However, our resultant file(i.e. DOC file) will be saved into its folder. After that, open the app.controller.ts file placed inside the ‘src’ folder, your app.controller.ts file should look like it as mentioned below.

import { Controller, Post } from '@nestjs/common';
import { AppService } from './app.service';
import { PdfApi } from 'asposepdfcloud';
import \* as fs from 'fs';
@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Post('/pdf-to-doc')
  async pdfToDoc() {
    const name = 'm.pdf';
    const SrcFile = '/' + name;
    const resultName = 'result.doc';
    const resultPath = 'myfolder/' + resultName;
    const storageName = 'testing';
    const fileToWrite = process.cwd() + '/' + resultName;
    const pdfApi = new PdfApi(
      'YOUR-APPSID',
      'YOUR-APPKEY',
    );
    try {
      const data = fs.readFileSync(name);
      await pdfApi.uploadFile(SrcFile, new Buffer(data), storageName);
      await pdfApi.putPdfInStorageToDoc(
        name,
        resultPath,
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        '',
        '',
        '',
        '',
        '',
        '',
        '',
        '',
        '',
        storageName,
      );

      // Download pdf from cloud storage

      const fileData = await pdfApi.downloadFile(resultPath, storageName, '');
      const writeStream = fs.createWriteStream(fileToWrite);
      writeStream.write(fileData.body);
      return 'PDF converted to DOC successfully';
    } catch (e) {
      throw e;
    }
  }
}

Now, restart the server, you can make a post request at http://localhost:3000/pdf-to-doc. Finally, you should see a file named “result.doc” in your project’s root folder.

Final Thoughts

This brings us to the end of this Aspose.PDF tutorial. We have gone through some fundamentals of file format conversion and how to convert a PDF to DOC using PDF To DOC Node.js. Similarly, Aspose.PDF offers cloud SDKs in many other languages that you may check out. This blog post will surely help you if you are looking for a PDF to DOC converter for your business software. In addition, there is comprehensive documentation for every feature of every online file convertor.

Last but not least, blog.aspose.cloud is continuously writing new blog posts. Therefore, please stay in touch for regular updates. Moreover, you can follow us on our social media accounts Facebook, LinkedIn, and Twitter.

Ask a question

Feel free to visit our forum which is very active to respond to questions and queries/discussions.

Explore

You may find the following links relevant: