Available Now: Explore our latest release with enhanced accessibility and powerful IDP features

Getting started with PDF Generation using Node.js and the Apryse SDK

By Roger Dunham | 2024 Dec 13

Sanity Image
Read time

4 min

Introduction

Copied to clipboard

Node.js is great at managing lots of people using a website at the same time, making things fast and smooth. That’s one of the reasons why it is used for many hundreds of thousands of websites.

Plus, since it uses the same language for both the website’s inside and outside, it helps developers work together more easily, reducing the number of programming languages that they need to know.

Node.js supports a wide range of libraries that let you leverage other functionality to get you to market faster, and in this article, we will see how you can create PDFs in a Node.js project using the Apryse SDK and Node.js.

Creating a simple, empty PDF.

Copied to clipboard

Prerequisites

You will need to have Node.js and npm installed.

Apryse SDK works with many versions of these tools, but for this article I was using Node 18.19.0 and npm 10.8.3

Step 1 – Initialize the project

Create a new folder and navigate into it. Then use npm to scaffold the new project.

npm init 

This will lead you through a series of questions about how you want to set up your project. For now, you can probably just accept the default values.

Step 2 – Import the PDFNet-node module

Within a terminal import the PDFNet-node module.

npm -I @pdftron/pdfnet-node  

This will download the module and place it into the node_modules folder for the project.

Blog image

Figure 1 - After importing the pdf-node module it will be available in the node_modules folder for the project.

Step 3 – Create a simple index.js file

The Apryse SDK requires a license key, but it is free to get a trial license key.

Next, create a file called index.js (or whatever other name you specified as the entry point for your app when initializing the project), and paste the following code into it (along with your license key).

const {PDFNet} = require('@pdftron/pdfnet-node') 
 
const main = async () =>{ 
  const doc = await PDFNet.PDFDoc.create(); 
  const page = await doc.pageCreate(); 
  doc.pagePushBack(page); 
  doc.save('blank.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized); 
} 
 
PDFNet.runWithCleanup(main, "[Your license key]").catch((err)=>{ 
  console.log(err); 
}).then(()=>{ 
  PDFNet.shutdown(); 
}) 

Step 4 – Run the project - and create a PDF

You can now run the project using Node.

Node index.js 

After a few moments a new PDF will be created called “blank.pdf”, which you can open in a viewer (in this case Xodo PDF Studio).

Blog image

Figure 2 - The newly created file is a valid PDF - even though it is empty.

Twelve lines of code to create an empty PDF is awesome, even though an empty PDF is not very useful. The Apryse SDK, however, offers much more with a wide range of functionality for working with PDFs.

Step 5 – Try out the rest of the Node.js samples

There’s a huge range of samples available for Node. You can either find the code from the Apryse Samples page or, alternatively, download and install the samples as a Node module using npm.

npm i @pdftron/pdfnet-node-samples 
Blog image

Figure 3 - One way to get the samples is to download the entire set as a Node module.

Having downloaded the samples as a Node module, the easiest way to use them is to navigate to the folder where they are located.

cd node_modules/@pdftron/pdfnet-node-samples 

Next paste your license key into the file LicenseKey.js.

Blog image

Figure 4 - An example of pasting the license key so that it can be used from the samples.

You are now ready to run the samples. You can either run all the samples by using Runall.bat or Runall.sh (depending on your platform), or else you can run a specific sample (for example Document Creation) by using Node and the name of the file.

Node ./DocumentCreationTest/DocumentCreationTest.js        

In either case, the processing will start and in a few moments some output will be generated.

Blog image

Figure 5 - A PDF created using the sample code for document creation.

Some of the samples require extra add-on modules to be downloaded. If this happens then this will be reported in the console output.

Conclusion

Copied to clipboard

Node.js is a great framework for developing server-side apps. With the addition of the PDF-node module from Apryse, you can quickly leverage our class-leading tools to implement a wide range of functionality – saving you time and getting you to market faster.

Grab yourself a trial license key and try things out for yourself. There’s a wealth of documentation available, and if you run into any problems then reach out to us on Discord and our Solution Engineers will be happy to help.

Sanity Image

Roger Dunham

Share this post

email
linkedIn
twitter