README.md 6.65 KB
Newer Older
julien's avatar
julien committed
1
<img style="display: block; margin: 5em 0 auto;" src="https://www.pagedmedia.org/wp-content/uploads/2018/11/pagedjs.png" alt="Paged.js logo - pagination in the browser"/>
2

Fred Chasen's avatar
Fred Chasen committed
3
Paged.js - Paged Media Tools
Fred Chasen's avatar
Fred Chasen committed
4 5
===========

Fred Chasen's avatar
Fred Chasen committed
6 7 8 9 10 11 12
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](https://www.w3.org/TR/css-page-3/) and [Generated Content](https://www.w3.org/TR/css-gcpm-3/) CSS modules, along with hooks to create new handlers for custom properties.

The currently supported properties can be found on [the wiki](https://gitlab.pagedmedia.org/tools/pagedjs/wikis/Support-of-specifications).

A quick overview to getting started with Paged Media CSS and Paged.js is available on [pagedmedia.org](https://www.pagedmedia.org/paged-js/).
13

Fred Chasen's avatar
Fred Chasen committed
14 15 16 17 18 19
## NPM Module
```sh
$ npm install pagedjs
```

```js
20
import { Previewer } from 'pagedjs';
Fred Chasen's avatar
Fred Chasen committed
21

22 23
let paged = new Previewer();
let flow = paged.preview(DOMContent, ["path/to/css/file.css"], document.body).then((flow) => {
Fred Chasen's avatar
Fred Chasen committed
24 25 26 27
	console.log("Rendered", flow.total, "pages.");
})
```

Fred Chasen's avatar
Fred Chasen committed
28
## Polyfill
Fred Chasen's avatar
Fred Chasen committed
29

Fred Chasen's avatar
Fred Chasen committed
30
Add the the `paged.polyfill.js` script to replace all `@page` css and render the html page with the Paged Media styles applied:
Fred Chasen's avatar
Fred Chasen committed
31 32

```html
Fred Chasen's avatar
Fred Chasen committed
33
<script src="https://unpkg.com/pagedjs/dist/paged.polyfill.js"></script>
Fred Chasen's avatar
Fred Chasen committed
34 35
```

Fred Chasen's avatar
Fred Chasen committed
36
Try the [polyfill with Aurorae](https://s3.amazonaws.com/pagedmedia/pagedjs/examples/polyfill.html).
Fred Chasen's avatar
Fred Chasen committed
37

Fred Chasen's avatar
Fred Chasen committed
38 39 40 41 42 43 44 45 46 47 48 49
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.

```html
<script>
	window.PagedConfig = {
		before: () => {
			return new Promise(resolve, reject) {
				setTimeout(() => { resolve() }, 1000);
			}
		},
		after: (flow) => { console.log("after", flow) },
50 51 52 53 54 55 56
		handlers: [
			{
				afterPageLayout(pageFragment, page) {
					console.log(pageFragment);
				}
			}
		]
Fred Chasen's avatar
Fred Chasen committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	};
</script>
```

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

```html
<script>
	window.PagedConfig = {
		auto: false
		after: (flow) => { console.log("after", flow) },
	};

	setTimeout(() => {
		window.PagedPolyfill.preview();
	}, 1000);
</script>
```

Fred Chasen's avatar
Fred Chasen committed
77 78 79
## Chunker
Chunks up a document into paged media flows and applies print classes.

Fred Chasen's avatar
Fred Chasen committed
80
Examples:
Fred Chasen's avatar
Fred Chasen committed
81

Fred Chasen's avatar
Fred Chasen committed
82 83
* Process the [first 50 pages of Moby Dick](https://s3.amazonaws.com/pagedmedia/pagedjs/examples/index.html).
* Upload and [chunk an Epub using Epub.js](https://s3.amazonaws.com/pagedmedia/pagedjs/examples/epub.html).
Fred Chasen's avatar
Fred Chasen committed
84

Fred Chasen's avatar
Fred Chasen committed
85
## Polisher
Fred Chasen's avatar
Fred Chasen committed
86 87
Converts `@page` css to classes, and applies counters and content.

Fred Chasen's avatar
Fred Chasen committed
88 89 90
Examples:

* Test [styles for print](https://s3.amazonaws.com/pagedmedia/pagedjs/examples/polisher.html).
Fred Chasen's avatar
Fred Chasen committed
91 92 93 94

### CLI

Command line interface to render out PDFs of HTML files using Puppeteer: [https://gitlab.pagedmedia.org/polyfills/pagedjs-cli](https://gitlab.pagedmedia.org/polyfills/pagedjs-cli).
Fred Chasen's avatar
Fred Chasen committed
95

96 97 98 99 100 101 102
## 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.

```html
Fred Chasen's avatar
Fred Chasen committed
103
<script src="https://unpkg.com/pagedjs/dist/paged.polyfill.js"></script>
104
<script>
105 106 107 108 109 110 111 112
	window.PagedConfig = {
		handlers: [
			{
				afterPageLayout(pageFragment, page) {
					console.log(pageFragment);
				}
			}
		]
113 114 115 116 117 118 119
	}
</script>
```

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.

```js
120 121 122 123
// Previewer
beforePreview(content, renderTo)
afterPreview(pages)

124
// Chunker
Fred Chasen's avatar
Fred Chasen committed
125
beforeParsed(content)
126 127 128 129 130 131
afterParsed(parsed)
beforePageLayout(page)
afterPageLayout(pageElement, page, breakToken)
afterRendered(pages)

// Polisher
Fred Chasen's avatar
Fred Chasen committed
132
beforeTreeParse(text, sheet)
133 134
beforeTreeWalk(ast)
afterTreeWalk(ast, sheet)
135 136 137 138 139
onUrl(urlNode)
onAtPage(atPageNode)
onRule(ruleNode)
onDeclaration(declarationNode, ruleNode)
onContent(contentNode, declarationNode, ruleNode)
140 141 142 143 144 145

// Layout
layoutNode(node)
renderNode(node, sourceNode)
onOverflow(overflow, rendered, bounds)
onBreakToken(breakToken, overflow, rendered)
146 147
```

Fred Chasen's avatar
Fred Chasen committed
148 149 150 151 152 153 154
## Setup
Install dependencies
```sh
$ npm install
```

## Development
155
Run the local dev-server with livereload and autocompile on [http://localhost:9090/](http://localhost:9090/)
Fred Chasen's avatar
Fred Chasen committed
156 157 158
```sh
$ npm start
```
Fred Chasen's avatar
Fred Chasen committed
159

Fred Chasen's avatar
Fred Chasen committed
160
## Deployment
161

Fred Chasen's avatar
Fred Chasen committed
162
Build the `dist` output
163

Fred Chasen's avatar
Fred Chasen committed
164
```sh
165
$ npm run build
Fred Chasen's avatar
Fred Chasen committed
166
```
Fred Chasen's avatar
Fred Chasen committed
167

168 169
## Testing

Fred Chasen's avatar
Fred Chasen committed
170 171 172 173 174 175 176
Testing for Paged.js uses [Jest](https://facebook.github.io/jest/en/) but is split into Tests and Specs.

### Tests

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

```bash
177
npm test
Fred Chasen's avatar
Fred Chasen committed
178 179 180 181 182 183 184 185
```

### 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).

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
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

```bash
brew install ghostscript
```

For Debian you can install it with

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

Fred Chasen's avatar
Fred Chasen committed
203
To test the pdf output of specs, you'll need to build the library locally.
204 205

```bash
Fred Chasen's avatar
Fred Chasen committed
206
npm run build
207 208 209 210 211
```

Then run the jest tests in puppeteer.

```bash
Fred Chasen's avatar
Fred Chasen committed
212
npm run specs
213 214 215 216 217
```

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

```bash
Fred Chasen's avatar
Fred Chasen committed
218
NODE_ENV=debug npm run specs
219 220 221 222 223
```

To update the stored pdf images you can run

```bash
Fred Chasen's avatar
Fred Chasen committed
224
npm run specs -- --updateSnapshot
225
```
Fred Chasen's avatar
Fred Chasen committed
226 227 228 229 230 231 232 233

### 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

```bash
234
docker build -t pagedmedia/pagedjs .
Fred Chasen's avatar
Fred Chasen committed
235 236 237 238 239 240 241 242 243
```

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

```bash
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`
Fred Chasen's avatar
Fred Chasen committed
244

Fred Chasen's avatar
Fred Chasen committed
245 246 247
```bash
docker run -it --security-opt 'seccomp=seccomp.json' pagedmedia/pagedjs npm test
```
248

249
## Contributors
250 251 252 253 254 255

### Core team
The core team behind paged.js includes [Adam Hyde](https://adamhyde.net), [Julie Blanc](http://julie-blanc.fr/), [Fred Chasen](http://fchasen.com/) & Julien Taquet.

## Licence

256
MIT License (MIT), which you can read [here](https://gitlab.pagedmedia.org/tools/pagedjs/blob/master/LICENSE.md)