Fileforge is shutting down on December, 25th, 2024. Get help migrating
DOCX to PDF Conversion with Fileforge

DOCX to PDF Conversion with Fileforge

Friday, June 7, 2024

Auguste Lefevre Grunewald

Studied Computer Science and Quantitative Finance. Co-founder @ Fileforge. I'm passionate about technology, politics, history and nature. I love to share my thoughts and learn from others.

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 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.

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:

1
import { FileforgeClient } from "@fileforge/client";
2
import * as fs from "fs";
3
4
const ff = new FileforgeClient({
5
apiKey: process.env.API_KEY_INTEGRATION,
6
});
7
8
(async () => {
9
try {
10
const docxFile = fs.createReadStream("file-sample.docx");
11
const pdfStream = await ff.pdf.fromDocx(
12
docxFile,
13
{},
14
{
15
timeoutInSeconds: 30,
16
}
17
);
18
19
pdfStream.pipe(fs.createWriteStream("./result_docx.pdf"));
20
console.log("PDF conversion successful. Stream ready.");
21
} catch (error) {
22
console.error("Error during PDF conversion:", error);
23
}
24
})();

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.

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. It’s free!
  • Join the Fileforge community on Discord and share your feedback with us.
  • Contribute to the open-source library

Related products

Also on our blog