DOCX to PDF Conversion via API
Tags: pdf, api, node, docx
Published: 2024-06-10 08:13:25

**tl;dr**: In this article you will learn how to convert .docx files to PDF programmatically using the Fileforge API.

## Introduction
With the latest update of the Fileforge API, you can now convert .docx files to PDF. This feature is particularly useful for generating invoices, reports, and other documents that need to be shared or printed. In this article, I will show you how to convert .docx files to PDF using the Fileforge API. Especially, we will use the Node.js SDK to demonstrate the process.

## Prerequisites

Before we get started, you will need to sign up for a free Fileforge account and create an API key. You can sign up for an account [here](https://app.fileforge.com) and get your API key from the dashboard.

I also encourage you to take a look at the documentation for the Fileforge API, which you can find [here](https://docs.fileforge.com).

## Converting .docx to PDF

Let's dive into the code. Here is a simple example of how you can convert a .docx file to PDF using the Fileforge API in Node.js:

```javascript
import { FileforgeClient } from "@fileforge/client";
import * as fs from "fs";

const ff = new FileforgeClient({
  apiKey: process.env.API_KEY_INTEGRATION,
});

(async () => {
  try {
    const docxFile = fs.createReadStream("file-sample.docx");
    const pdfStream = await ff.pdf.fromDocx(
      docxFile,
      {},
      {
        timeoutInSeconds: 30,
      },
    );

    pdfStream.pipe(fs.createWriteStream("./result_docx.pdf"));
    console.log("PDF conversion successful. Stream ready.");
  } catch (error) {
    console.error("Error during PDF conversion:", error);
  }
})();
```

In this code snippet, we:

- First import the `FileforgeClient` class from the `@fileforge/client` package. We then create a new instance of the `FileforgeClient` class with our API key.
- Next, we read the .docx file from the local file system using `fs.createReadStream`. 
- Finally, we call the `ff.pdf.fromDocx` method with the .docx file stream as the first argument. This method returns a PDF stream that we can pipe to a file stream using `fs.createWriteStream`.

You can try this code snippet with your own .docx file. Make sure to replace `"file-sample.docx"` with the path to your .docx file.

## How it works behind the scenes

This service operates on a LibreOffice headless server. When you send a .docx file to the Fileforge API, we use this headless server to convert your file to a PDF file. As of now, this solution may not fully support all .docx features, but we are continuously improving it. If you encounter any issues or have any specific request, please [contact us](mailto:contact@fileforge.com).

## Conclusion

In this article we have demonstrated how to convert .docx files to PDF using the Fileforge API. We also discussed how this service works behind the scenes. We are working to improve the quality of the conversion service and add more features in the future. If you have any feedback or suggestions, please feel free to reach out to us. We would love to hear from you!

- Try the new Fileforge API endpoints for .docx to pdf conversion [here](https://app.fileforge.com). It's free!
- Join the Fileforge community on [Discord](https://discord.com/invite/uRJE6e2rgr) and share your feedback with us.
- Contribute to the [open-source library](https://github.com/OnedocLabs/react-print-pdf)

This article is taken from [Fileforge's blog](https://www.fileforge.com/blog/docx-to-pdf)