Paged.js

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

README

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 pagedmedia.org.

NPM Module

  1. ```sh
  2. $ npm install pagedjs
  3. ```

  1. ``` js
  2. import { Previewer } from 'pagedjs';

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

Polyfill


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

  1. ``` html
  2. <script src="https://unpkg.com/pagedjs/dist/paged.polyfill.js"></script>
  3. ```


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.

  1. ``` html
  2. <script>
  3. window.PagedConfig = {
  4.   before: () => {
  5.    return new Promise(resolve, reject) {
  6.     setTimeout(() => { resolve() }, 1000);
  7.    }
  8.   },
  9.   after: (flow) => { console.log("after", flow) },
  10. };
  11. </script>
  12. ```

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

  1. ``` html
  2. <script>
  3. window.PagedConfig = {
  4.   auto: false
  5.   after: (flow) => { console.log("after", flow) },
  6. };
  7. setTimeout(() => {
  8.   window.PagedPolyfill.preview();
  9. }, 1000);
  10. </script>
  11. ```

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://gitlab.pagedmedia.org/tools/pagedjs-cli.

Modules


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.

  1. ``` html
  2. <script src="https://unpkg.com/pagedjs/dist/paged.polyfill.js"></script>
  3. <script>
  4. class MyHandler extends Paged.Handler {
  5.   constructor(chunker, polisher, caller) {
  6.    super(chunker, polisher, caller);
  7.   }
  8.   afterPageLayout(pageFragment, page) {
  9.    console.log(pageFragment);
  10.   }
  11. }
  12. Paged.registerHandlers(MyHandler);
  13. </script>
  14. ```

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.

  1. ``` js
  2. // Previewer
  3. beforePreview(content, renderTo)
  4. afterPreview(pages)

  5. // Chunker
  6. beforeParsed(content)
  7. filter(content)
  8. afterParsed(parsed)
  9. beforePageLayout(page)
  10. afterPageLayout(pageElement, page, breakToken)
  11. afterRendered(pages)

  12. // Polisher
  13. beforeTreeParse(text, sheet)
  14. beforeTreeWalk(ast)
  15. afterTreeWalk(ast, sheet)
  16. onUrl(urlNode)
  17. onAtPage(atPageNode)
  18. onRule(ruleNode)
  19. onDeclaration(declarationNode, ruleNode)
  20. onContent(contentNode, declarationNode, ruleNode)

  21. // Layout
  22. layoutNode(node)
  23. renderNode(node, sourceNode, layout)
  24. onOverflow(overflow, rendered, bounds)
  25. onBreakToken(breakToken, overflow, rendered)
  26. afterOverflowRemoved(removed, rendered)
  27. ```

Setup

Install dependencies
  1. ```sh
  2. $ npm install
  3. ```

Development

Run the local dev-server with livereload and autocompile on http://localhost:9090/
  1. ```sh
  2. $ npm start
  3. ```

Deployment

Build the dist output
  1. ```sh
  2. $ npm run build
  3. ```

Compile the lib output
  1. ```sh
  2. $ npm run compile
  3. ```

Generate legacy builds with polyfills included
  1. ```sh
  2. $ npm run legacy
  3. ```

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.

  1. ``` sh
  2. npm test
  3. ```

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.

To run them you'll need to install a local version of Ghostscript for you system according to https://www.npmjs.com/package/ghostscript4js#prerequisites

For Mac you can install it with

  1. ``` sh
  2. brew install ghostscript
  3. ```

For Debian you can install it with

  1. ``` sh
  2. sudo apt-get install ghostscript
  3. sudo apt-get install libgs-dev
  4. ```

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

  1. ``` sh
  2. npm run build
  3. ```

Then run the jest tests in puppeteer.

  1. ``` sh
  2. npm run specs
  3. ```

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

  1. ``` sh
  2. NODE_ENV=debug npm run specs
  3. ```

To update the stored pdf images you can run

  1. ``` sh
  2. npm run specs -- --updateSnapshot
  3. ```

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

  1. ``` sh
  2. docker build -t pagedmedia/pagedjs .
  3. ```

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

  1. ``` sh
  2. docker run -it -p 9090:9090 pagedmedia/pagedjs
  3. ```

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

  1. ``` sh
  2. docker run -it --security-opt 'seccomp=seccomp.json' pagedmedia/pagedjs npm test
  3. ```

Contributors


Core team

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

Licence


MIT License (MIT), which you can read here