PDF is a preferred format to share important documents, and it is a common practice to share Word documents in a PDF format. Although Microsoft Office provides a built-in functionality to convert Word to PDF, you may need to convert your Word documents (DOC or DOCX) into PDF programmatically. As a Node.js developer, you can easily convert Word documents to PDF files in your Node.js applications on the cloud. In this article, you will learn how to convert Word documents to PDF using a REST API in Node.js.
The following topics shall be covered in this article:
- DOCX to PDF Conversion REST API and Node.js SDK
- Convert Word Documents to PDF using a REST API in Node.js
- Word to PDF Conversion with Advanced Options
- Convert Word to PDF with Watermark using Node.js
- DOCX to PDF Conversion without using Cloud Storage
- Convert Range of Pages from DOCX to PDF in Node.js
- Convert Specific Pages of DOCX to PDF using Node.js
DOCX to PDF Conversion REST API and Node.js SDK
For converting DOCX to PDF, I will be using the Node.js SDK of GroupDocs.Conversion Cloud API. It is a platform-independent document and image conversion solution without depending on any tool or software. It enables you to quickly and reliably convert images and documents of any supported file format to any format you need. You can easily convert between over 50 types of documents and images such as Word, PowerPoint, Excel, PDF, HTML, CAD, raster images, etc.
You can install GroupDocs.Conversion Cloud to your Node.js project using the following command in the console:
npm install groupdocs-conversion-cloud
Please get your Client ID and Client Secret from the dashboard before you start following the steps and available code examples. Once you have your ID and secret, please add in the code as shown below:
Convert Word Documents to PDF using a REST API in Node.js
You can convert your Word documents to PDF programmatically on the cloud by following the simple steps given below:
- Upload the DOCX file to the cloud
- Convert DOCX to PDF using Node.js
- Download the converted PDF file
Upload the DOCX File
Firstly, upload the DOCX file to the cloud using the following code sample:
As a result, the uploaded DOCX file will be available in the files section of your dashboard on the cloud.
Convert DOCX to PDF using Node.js
You can easily convert DOCX to PDF document programmatically by following the steps mentioned below:
- Create an instance of the ConvertApi
- Create an instance of the ConvertSettings
- Provide the input DOCX file path
- Assign “pdf” to the format
- Provide the output file path
- Create ConvertDocumentRequest with ConvertSettings
- Convert by calling the convertDocument() method with ConvertDocumentRequest
The following code example shows how to convert DOCX to PDF using a REST API in Node.js.
Download the Converted File
The above code sample will save the converted PDF file on the cloud. You can download it using the following code sample:
Word to PDF Conversion with Advanced Options
You can convert Word documents to PDF files with some advanced settings by following the steps given below:
- Create an instance of the ConvertApi
- Create an instance of the ConvertSettings
- Provide the DOCX file path
- Assign “pdf” to format
- Provide the output file path
- Create an instance of the DocxLoadOptions
- Optionally set various load options such as hideComments, hideWordTrackedChanges, etc.
- Create an instance of the PdfConvertOptions
- Optionally set various convert options such as displayDocTitle, margins (top, left, right, bottom), etc.
- Create ConvertDocumentRequest with ConvertSettings
- Convert by calling the convertDocument() method with ConvertDocumentRequest
The following code example shows how to convert a Word document to a PDF document with advanced convert options.
Convert Word to PDF with Watermark using Node.js
You can convert Word documents to PDF documents and add watermarks to converted documents programmatically by following the steps given below:
- Create an instance of the ConvertApi
- Create an instance of the ConvertSettings
- Provide the input DOCX file path
- Assign “pdf” to the format
- Provide the output file path
- Create an instance of the WatermarkOptions
- Set Watermark Text, Color, Width, Height, etc.
- Define the PdfConvertOptions and assign WatermarkOptions
- Create ConvertDocumentRequest with ConvertSettings
- Convert by calling the convertDocument() method with ConvertDocumentRequest
The following code example shows how to convert a Word document to a PDF document and add a watermark to the converted PDF document using a REST API in Node.js. Please follow the steps mentioned earlier to upload and download files.
DOCX to PDF Conversion without using Cloud Storage
You can convert a Word document to PDF without using the cloud storage by passing it in the request body and receiving the output file in the API response. Please follow the steps mentioned below to convert a DOCX to a PDF without using cloud storage.
- Create an instance of the ConvertApi
- Read input DOCX file from local path
- Create ConvertDocumentDirectRequest
- Provide the target format as “pdf” and the input file path as input parameters
- Get results by calling the convertDocument_Direct()_ method with ConvertDocumentDirectRequest
- Save the output file to the local path using FileStream.writeFile() method
The following code example shows how to convert a Word document to a PDF without using cloud storage.
Convert Range of Pages from DOCX to PDF in Node.js
You can easily convert a range of pages from a Word document to a PDF file programmatically by following the steps mentioned below:
- Create an instance of the ConvertApi
- Create an instance of the ConvertSettings
- Provide the input DOCX file path
- Assign “pdf” to the format
- Provide the output file path
- Create an instance of the PdfConvertOptions
- Provide a page range to convert from start page number and total pages to convert
- Assign PdfConvertOptions to ConvertSettings
- Create ConvertDocumentRequest with ConvertSettings
- Convert by calling the convertDocument() method with ConvertDocumentRequest
The following code sample shows how to convert a range of pages from a DOCX to PDF using a REST API in Node.js. Please follow the steps mentioned earlier to upload and download a file.
Convert Specific Pages of DOCX to PDF in Node.js
You can convert specific pages of a Word document to a PDF file programmatically by following the steps mentioned below:
- Create an instance of the ConvertApi
- Create an instance of the ConvertSettings
- Provide the input DOCX file path
- Assign “pdf” to the format
- Provide the output file path
- Create an instance of the PdfConvertOptions
- Provide specific page numbers to convert
- Assign _PdfConvertOptions_ to ConvertSettings
- Create ConvertDocumentRequest with ConvertSettings
- Convert by calling the convertDocument() method with ConvertDocumentRequest
The following code example shows how to convert specific pages of a Word document to PDF using a REST API in Node.js. Please follow the steps mentioned earlier to upload and download a file.
Try Online
Please try the following free online DOCX to PDF conversion tool, which is developed using the above API. https://products.groupdocs.app/conversion/docx-to-pdf
Conclusion
In this article, you have learned how to convert Word documents to PDF files on the cloud. You have also learned how to convert specific pages of a Word document to a PDF using Node.js. Moreover, you have learned how to convert a range of pages from a DOCX to a PDF programmatically. Furthermore, you have learned how to add a watermark to the converted PDF document. This article also explained how to programmatically upload the DOCX file on the cloud and then download the converted PDF file from the cloud. You can learn more about GroupDocs.Conversion Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. In case of any ambiguity, please feel free to contact us on the forum.