Easily Convert Any Document to PDF – A Complete Guide
Tutorials

Easily Convert Any Document to PDF – A Complete Guide

Follow this step-by-step guide to easily generate professional PDFs from Word docs using a document conversion API. Fast, reliable, and workflow-friendly.
Easily Convert Any Document to PDF – A Complete Guide
SO
Sohail Pathan
Last updated on January 08, 2024

Introduction

Converting documents from one format to another is a common task for developers, and one of the most frequent conversions is turning Word documents into PDFs.
To help developers pick the right tools, we explored the benefits of PDF conversion, compared top tools, and looked at options like SmallPDF and CloudConvert. Cloud-based APIs are another convenient solution, and providers like ApyHub or ConvertAPI can be especially useful.
In this tutorial, we will walk you through how to use the ApyHub Convert Doc to PDF API with NodeJS in 7 simple steps.

1. Install the required libraries:

First, install the necessary dependencies for your NodeJS project. Open your terminal in the project folder and run:
1
npm install axios
What this does:
  • axios is a popular HTTP client for NodeJS.
  • It simplifies sending HTTP requests and handling responses.
In this tutorial, Axios is used to send a POST request to the ApyHub Document Conversion API.

2. Create a new file:

Create a new file called convertDocToPDF.js and import Axios:
1
const axios = require('axios');

3. Define a function to convert the Docx. file to PDF:

Now, define a function that handles the conversion from Word to PDF. This function will:
  • Send a request to the API
  • Pass the document URL
  • Handle errors if the conversion fails
1
import axios from "axios";
2
3
async function convertDocToPDF(docURL, outputFileName) {
4
const apiURL = 'https://api.apyhub.com/convert/word-url/pdf-url';
5
const requestData = {
6
url: docURL // URL of the Word document
7
};
8
const options = {
9
method: 'POST',
10
url: apiURL,
11
params: {output: `${outputFileName}.pdf`, landscape: 'false'},
12
headers: {
13
'apy-token': 'YOUR_APY_TOKEN', // Replace with your Unique apy-token
14
'Content-Type': 'application/json'
15
},
16
data: requestData
17
};
18
19
try {
20
const response = await axios.request(options);
21
console.log("PDF conversion successful! Here's the PDF URL:", response.data);
22
return response.data; // Or handle it as you need
23
} catch (error) {
24
console.error("An error occurred during the conversion:", error.message);
25
throw error; // Or handle the error as needed
26
}
27
}

4. Invoke the convertDocToPDF function:

After invoking the convertDocToPDF function, pass the Word document URL and the desired output filename as arguments. This function sends a request to the Document Conversion API, which converts the Word document to a PDF and returns the URL of the converted PDF. The successful conversion URL is logged to the console. If there is an error during the conversion process, it is caught and the error message is logged to the console.
1
const docURL = '***** PROVIDE THE URL OF DOC *****'; // Replace with your document URL
2
const outputFilename = '***** PROVIDE THE OUTPUT FILE NAME *****'; // Replace with your desired output PDF file name
3
4
convertDocToPDF(docURL, outputFilename)
5
.then((pdfUrl) => {
6
console.log('PDF conversion successful! PDF URL:', pdfUrl);
7
})
8
.catch((error) => {
9
console.error('Error:', error.message);
10
});

5. Save the file and execute the script

Save the file open your terminal, navigate to the project directory, and run the following command to execute the script:
1
node convertDocToPDF.js

6. Send a request to the API

Your script will:
  • Send a request to ApyHub
  • Convert the Word document to PDF
  • Save it as ${outputFilename}.pdf in your project directory
And that’s it! You now have a working document-to-PDF conversion setup using NodeJS and the ApyHub API.
Step 7: Explore More Conversion APIs
ApyHub also offers other useful document conversion APIs, such as:
  • Spreadsheets to PDF
  • Presentations to PDF
  • Adding watermarks to PDFs
Depending on your use case, these can be very helpful.
✅ Conclusion
Converting documents to PDF with NodeJS is straightforward using the ApyHub API. With just a few lines of code, you can automate conversions and integrate this functionality directly into your applications.
And that's it! Was easy right?
Keep in mind that ApyHub also has similar conversion APIs. For example, some APIs help you convert spreadsheets to PDFs, presentations to PDFs, and apply watermarks to PDFs, which can help depending on your use case.