How to Build a Browser-Based PDF Color Inverter Tool Using JavaScript


Reading PDF documents for long periods can become tiring, especially when the document contains bright backgrounds or when you’re working in a low-light environment.

In other situations, designers, developers, and print professionals may want to inspect how a document looks with inverted colors or prepare alternative versions for accessibility and review.

A PDF Color Inverter Tool makes this possible by transforming the colors of PDF pages while keeping the document structure intact. Instead of editing every image or graphic manually, users can upload a PDF, invert its colors, preview the results, and download a newly generated document in just a few clicks.

In this tutorial, you’ll build a browser-based PDF Color Inverter Tool using JavaScript. Users will be able to upload a PDF, preview its pages, choose an inversion mode, specify the page range to process, adjust the output quality, enable live preview, generate the inverted PDF, review the result, rename the output file, and download it, all without uploading the document to a server.

We’ll use PDF.js to render PDF pages inside the browser, the HTML Canvas API to manipulate pixel colors, and PDF-lib to generate the final PDF.

By the end of this tutorial, you’ll have a fully functional client-side PDF color inversion tool similar to the one available on my site All In One Tools.

Table of Contents

A PDF Color Inverter Tool changes the appearance of a PDF by reversing the colors of its pages. Light colors become dark, dark colors become light, and every pixel is recalculated to create an inverted version of the original document. This can improve readability in certain environments, help preview designs in dark mode, or simply provide an alternative way to view a document.

In this project, users can upload a PDF, browse through every page, choose how the colors should be inverted, define the page range to process, select the output quality, enable a live preview, generate the inverted document, rename the output file, and download the finished PDF. Since all processing happens locally inside the browser, the original document never leaves the user’s device.

Behind the scenes, PDF.js renders each PDF page onto an HTML canvas. Once a page is rendered, JavaScript accesses the pixel data using the Canvas API. Every pixel’s red, green, and blue values are recalculated to create the inverted version of the page.

After processing all selected pages, PDF-lib assembles the modified pages into a new PDF that users can preview and download.

A single pixel is represented by four values:

const pixel = {

    red: 120,

    green: 85,

    blue: 200,

    alpha: 255

};

During color inversion, each color channel is transformed by subtracting its value from 255.

red = 255 - red;

green = 255 - green;

blue = 255 - blue;

Repeating this calculation for every pixel on every selected page produces the final inverted PDF while preserving the document’s layout, page order, and dimensions.

Project Setup

Before writing any image-processing code, let’s create a simple project structure for our PDF Color Inverter Tool.

We’ll build the application using plain HTML, CSS, and JavaScript together with two libraries:

Our project structure looks like this:

pdf-color-inverter/

│── index.html
│── style.css
│── script.js
│── pdf.worker.min.js
│── assets/

Keeping everything separated makes the application easier to understand and maintain.

Include the required libraries before loading your own JavaScript.

Scroll to Top