Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including pagedjs with all npm packages installed. Try it out:

var pagedjs = require("pagedjs")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

pagedjs v0.1.32

Chunks up a document into paged media flows and applies print styles

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

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

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",, "pages.");


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

<script src=""></script>

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.

    window.PagedConfig = {
        before: () => {
            return new Promise(resolve, reject) {
                setTimeout(() => { resolve() }, 1000);
        after: (flow) => { console.log("after", flow) },

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

    window.PagedConfig = {
        auto: false
        after: (flow) => { console.log("after", flow) },

    setTimeout(() => {
    }, 1000);


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



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



Command line interface to render out PDFs of HTML files using Puppeteer:


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.

<script src=""></script>
    class MyHandler extends Paged.Handler {
        constructor(chunker, polisher, caller) {
            super(chunker, polisher, caller);

        afterPageLayout(pageFragment, page) {

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

// Chunker
afterPageLayout(pageElement, page, breakToken)

// Polisher
beforeTreeParse(text, sheet)
afterTreeWalk(ast, sheet)
onDeclaration(declarationNode, ruleNode)
onContent(contentNode, declarationNode, ruleNode)

// Layout
renderNode(node, sourceNode)
onOverflow(overflow, rendered, bounds)
onBreakToken(breakToken, overflow, rendered)


Install dependencies

$ npm install


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

$ npm start


Build the dist output

$ npm run prepare


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


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

npm run tests


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.

To run them you'll need to install a local version of Ghostscript for you system according to

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

Then you can install the node library

npm install ghostscript4js --no-save

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


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


Core team

The core team behind paged.js includes Adam Hyde, Julie Blanc, Fred Chasen & Julien Taquet.


MIT License (MIT), which you can read here


RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free