Getting started with server-side JavaScript and MuPDF
Jamie Lemon·August 21, 2024

Introduction
MuPDF is a highly capable, open-source library for PDF document management. It is a tried and tested solution which is available for many different programming languages including JavaScript.
This article aims to be a guide on how to integrate MuPDF.js with server-side JavaScript - in this case we are going with Node.js for the implementation.
Node.js allows developers to use JavaScript on the server-side, enabling them to create fast, scalable, and efficient applications. Its non-blocking, event-driven architecture, combined with the vast npm ecosystem, makes it a powerful tool for building modern web applications, APIs, and real-time services.
Let’s get started with trying it out!
Setting up Node.js
Let’s develop a local solution for our host computer and imagine we are starting from scratch.
Firstly, we should install Node.js. Go to https://nodejs.org/en/download/package-manager and follow the recommended instructions there to do that.
Once installed, you should have a Node available on your command line, to validate this run:
node -v
This should verify that the Node.js version is available in the environment.
Setting up MuPDF.js
MuPDF.js was developed as a NPM package to solve for server-side MuPDF within Node.js environments. It is available from npm and can be easily installed for your project.
For example, create a folder where you would like to start your project and simply run:
npm install mupdf
If you check the contents of this folder you will notice it has created a folder called “node_modules” and within that you will see a sub-folder “mupdf”. There is no need to worry about this for now - essentially this means that the mupdf.js library is now available for your project via node!
To test this, you can start node by typing:
node
This will start the Node.js runtime. To get MuPDF up and running in the following prompt import it with:
const mupdf = await import("mupdf")
(this tells us to import everything from the “mupdf” library and return a JavaScript object called “mupdf”)
We should now have the MuPDF library available as the JavaScript object mupdf
. We can run a quick test with:
console.log(mupdf)
You should then see this MuPDF object’s details - this is a JavaScript object available from the MuPDF.js library available for our use!
What Next?
Assuming we have Node.js with the MuPDF.js library nicely set up, and the system is available, then what kind of application do we want to make?
Let’s imagine a few options…
1. A Document Library
We could have a library of PDF documents made available to search and extract information. In this way the server could interrogate the documents, extract information and make it accessible to another application. PDF documents could be served from an offline resource meaning they are protected, and the actual source files are never publicly available. This is especially relevant if you are dealing with confidential files such as bank statements or invoices.
The setup for our solution may then be as follows:

2. A Document Processor
A user could upload documents to node.js on an on-demand basis - data is transitory, temporarily stored for the user session, and then deleted. During this time the user can access the MuPDF.js API to edit, convert or extract information from their document as they need.
The application setup might then be something like this:

3. Document Convertor
A very simple one-process service to convert PDFs to image files. Create an application which receives a PDF and returns an array of image files - one for each page of the document. Let the API allow for setting the DPI (Dots-Per-Inch) or whether to render annotations or not.
Choosing a Hosting Environment
Several hosting options are available for deploying Node.js applications. Here are a few popular choices:
- Platform-as-a-Service (PaaS): Services like Heroku, Vercel, and Google App Engine provide easy deployment with minimal server management. They often include features like automated scaling, environment configuration, and logging.
- Infrastructure-as-a-Service (IaaS): Providers like AWS (Amazon Web Services EC2), Microsoft Azure, and DigitalOcean offer virtual machines (VMs) where you can set up and configure your environment from scratch.
- Containerization: Using Docker, you can package your application and its dependencies into a container, which can then be deployed to services like AWS ECS, Kubernetes, or Docker Hub.
- Serverless Deployment: Services like AWS Lambda or Netlify allow you to deploy functions or small applications without managing servers. This is ideal for smaller applications or microservices.
We won’t go into the details of the setup for each of these, but suffice to say as long as the server can run JavaScript then you’ll be okay!
Best practices
- Remember to communicate back to the user with both success and error results.
- Large PDF files can be typical - how can you inform the user that a costly process is underway? For example, if we are converting all the pages of a 100-page PDF document then we should ensure that the user is aware of how long this may take and provide feedback as much as possible.
- Stay current with the MuPDF.js library. Keep looking out for new versions for improvements which could assist your application.
Resources
Finally, please check MuPDF.js on Github - it includes many examples and is the place to add issues, share ideas, or ask questions directly on our Discord.