RepositoryHTML
PA

pagedjs/pagedjs

Display paginated content in the browser and generate print books using web technology

1.6kstars
173forks
201issues
7views

Featured achievements

The project's latest awards

1
TINIX TRENDING·Topic: polyfill

#1 Repository of the week

1
TINIX TRENDING·Topic: paged-media

#1 Repository of the week

1
TINIX TRENDING·Topic: typesetting

#1 Repository of the week

Paged.js logo - pagination in the browser

Paged.js - Paged Media Tools

Paged.js is an open-source library to display paginated content in the browser and to generate print books using web technology.

It contains a set of handlers for CSS transformations and fragmented layout which polyfill the Paged Media and Generated Content CSS modules, along with hooks to create new handlers for custom properties.

The currently supported properties can be found on the pagedjs website.

A quick overview to getting started with Paged Media CSS and Paged.js is available on pagedjs.org/documentation.

NPM Module

$ npm install pagedjs
import { Previewer } from 'pagedjs';

let paged = new Previewer();
let flow = paged.preview(DOMContent, ["path/to/css/file.css"], document.body).then((flow) => {
	console.log("Rendered", flow.total, "pages.");
})

Polyfill

Add the the paged.polyfill.js script to replace all @page css and render the html page with the Paged Media styles applied:

Try the polyfill with Aurorae.

By default the polyfill will run automatically as soon as the DOM is ready. However, you can add an async before function or return a Promise to delay the polyfill starting.

Otherwise you can disable auto running the previewer and call window.PagedPolyfill.preview(); whenever you want to start.

Chunker

Chunks up a document into paged media flows and applies print classes.

Examples:

Polisher

Converts @page css to classes, and applies counters and content.

Examples:

CLI

Command line interface to render out PDFs of HTML files using Puppeteer: https://github.com/pagedjs/pagedjs-cli.

Module

Modules are groups of handlers for that apply the layout and styles of a CSS module, such as Generated Content.

New handlers can be registered from import { registerHandlers } from 'pagedjs' or by calling Paged.registerHandlers on an html page.



Handlers have methods that correspond to the hooks for the parsing, layout and rendering of the Chunker and Polisher. Returning a promise or async function from a method in a handler will complete that task before continuing with the other registered methods for that hook.

// Previewer
beforePreview(content, renderTo)
afterPreview(pages)

// Chunker
beforeParsed(content)
filter(content)
afterParsed(parsed)
beforePageLayout(page)
onPageLayout(pageWrapper, breakToken, layout);
afterPageLayout(pageElement, page, breakToken)
finalizePage(pageElement, page, breakToken)
afterRendered(pages)

// Polisher
beforeTreeParse(text, sheet)
beforeTreeWalk(ast)
afterTreeWalk(ast, sheet)
onUrl(urlNode)
onAtPage(atPageNode)
onRule(ruleNode)
onDeclaration(declarationNode, ruleNode)
onContent(contentNode, declarationNode, ruleNode)

// Layout
layoutNode(node)
renderNode(node, sourceNode, layout)
onOverflow(overflow, rendered, bounds)
onBreakToken(breakToken, overflow, rendered)
afterOverflowRemoved(removed, rendered)
beforeRenderResult(breakToken, pageWrapper)

How Pagedjs processes content

Chunker.flow()
└── Chunker.render() -> Looping through all pages
└──── Chunker.layout*() -> Handles overflowing pages, adding new ones
└────── Page.layout() -> Creates new Layout and waits for new Breaktoken
└──────── Layout.renderTo() -> Iterates through nodes
└────────── Layout.findBreakToken() -> Tries to find overflow/breaktoken

Setup

Install dependencies

$ npm install

Development

Run the local dev-server with livereload and autocompile on http://localhost:9090/

$ npm start

Deployment

Build the dist output

$ npm run build

Compile the lib output

$ npm run compile

Generate legacy builds with polyfills included

$ npm run legacy

Testing

Testing for Paged.js uses Jest but is split into Tests and Specs.

Tests

Unit tests for Chunker and Polisher methods are run in node using JSDOM.

npm test

Specs

Specs run a html file in Chrome (using puppeteer) to test against CSS specifications.

They can also output a pdf and compare pages (one at a time) in that PDF with samples PDFs (saved as images).

The PDF comparison tests depend on the ghostscript and the ghostscript4js package.

It is recomend to run these in the Docker container below via:

npm run docker-specs

However if you'd like to run the specs outside of Docker, you'll need to install a local version of Ghostscript for your system according to https://www.npmjs.com/package/ghostscript4js#prerequisites

For Mac you can install it with

brew install ghostscript

For Debian you can install it with

sudo apt-get install ghostscript
sudo apt-get install libgs-dev

Now you can install the ghostscript4js library. For Linux you can optionally pass the location ghostscript was installed to in GS4JS_HOME.

GS4JS_HOME="/usr/lib/$(gcc -dumpmachine)" npm install ghostscript4js

To test the pdf output of specs, you'll need to build the library locally.

npm run build

Then run the jest tests in puppeteer.

npm run specs

To debug the results of a test in a browser you can add NODE_ENV=debug

NODE_ENV=debug npm run specs

To update the stored pdf images you can run

npm run specs -- --updateSnapshot

Docker

A pagedmedia/pagedjs docker image contains all the dependencies needed to run the pagedjs development server, as well as the pdf comparison tests.

To build the image run

docker build -t pagedmedia/pagedjs .

By default the container will run the development server with npm start

docker run -it -p 9090:9090 pagedmedia/pagedjs

The tests and specs can be run within the container by passing a seccomp file for Chrome and running npm test

docker run -it --security-opt 'seccomp=seccomp.json' pagedmedia/pagedjs npm test && npm run specs

Licence

MIT License (MIT), which you can read here

Similar Projects

Discover other similar projects and AI models.

RepositoryPython
microsoft/markitdown

Python tool for converting files and office documents to Markdown.

199.5k
Dec 2, 2026
RepositoryHTML
tw93/Kami

👩‍🚒 Good content deserves good paper. Layout rules and templates that let AI agents export PDF, PNG and editable PowerPoint.

12.6k
Nov 25, 2026
RepositoryPython
docling-project/docling

Get your documents ready for gen AI

71.3k
Dec 2, 2026
RepositoryJava
opendataloader-project/opendataloader-pdf

PDF Parser for AI-ready data. Automate PDF accessibility. Open-source.

30.4k
Nov 26, 2026
RepositoryKotlin
iamgio/quarkdown

🪐 Markdown with superpowers: from ideas to papers, presentations, websites, books, and knowledge bases.

16.4k
Nov 26, 2026
RepositoryJavaScript
twbs/bootstrap

The most popular HTML, CSS, and JavaScript framework for developing responsive, mobile first projects on the web.

175.1k
Dec 1, 2026
RepositoryC++
electron/electron

:electron: Build cross-platform desktop apps with JavaScript, HTML, and CSS

124.3k
Dec 2, 2026
RepositoryTypeScript
heygen-com/hyperframes

Open-source AI video editor: edit your own footage, or create and remake any video with Claude Code, Codex or the free Studio app. Write HTML, render MP4. By HeyGen.

94.6k
Dec 2, 2026
Modelsentence-similarity
sentence-transformers/all-MiniLM-L6-v2

all-MiniLM-L6-v2 This is a sentence-transformers(https://www.SBERT.net) model: It maps sentences & paragraphs to a 384 dimensional dense vector space and can be used for tasks like clustering or semantic search. Usage (Sentence-Transformers) Using t...

♥0
157.6M
Jun 1, 2026