pagedjs vs pdfmake
Document Generation Libraries
pagedjspdfmakeSimilar Packages:

Document Generation Libraries

Document generation libraries are tools that facilitate the creation of documents in various formats, such as PDF or HTML, directly from web applications. These libraries provide developers with the ability to programmatically generate documents based on dynamic data, ensuring that the content is both customizable and maintainable. They are particularly useful for applications that require the generation of reports, invoices, or any form of printable content, allowing for a seamless integration of data and design.

Npm Package Weekly Downloads Trend

3 Years

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
pagedjs0-5.92 MB-3 years agoMIT
pdfmake012,25515.3 MB23417 days agoMIT

Feature Comparison: pagedjs vs pdfmake

Output Format

  • pagedjs:

    Paged.js is designed specifically for generating print-ready documents, allowing you to create high-quality outputs that adhere to web standards. It transforms HTML and CSS into paginated documents, making it suitable for producing books, reports, and other multi-page layouts that require precise control over pagination and styling.

  • pdfmake:

    pdfmake generates PDF documents directly from JavaScript objects, providing a straightforward API for creating complex documents. It allows for the inclusion of various elements such as tables, images, and custom fonts, making it versatile for generating invoices, forms, and reports.

Customization and Styling

  • pagedjs:

    Paged.js leverages the full power of CSS for styling documents, allowing developers to use familiar web design techniques to create visually appealing layouts. This includes advanced features like media queries for print, enabling responsive designs that adapt to different page sizes and orientations.

  • pdfmake:

    pdfmake offers a JSON-based configuration for styling, which may be less intuitive for those accustomed to CSS. However, it provides a range of options for customizing document elements, including font sizes, colors, and alignment, allowing for a good degree of flexibility in document design.

Complex Layouts

  • pagedjs:

    Paged.js is particularly strong in handling complex layouts, such as multi-column designs, footnotes, and running headers/footers. Its ability to interpret CSS for print media makes it suitable for projects that require intricate document structures and precise layout control.

  • pdfmake:

    pdfmake can handle basic layouts effectively but may struggle with more complex designs compared to Paged.js. It is better suited for simpler documents where the layout requirements are not as demanding.

Ease of Use

  • pagedjs:

    Paged.js may have a steeper learning curve, especially for developers unfamiliar with CSS for print. However, once mastered, it offers powerful capabilities for creating professional-grade documents that closely resemble web pages.

  • pdfmake:

    pdfmake is generally easier to get started with, especially for developers familiar with JavaScript. Its JSON-based API allows for quick document creation without needing extensive knowledge of CSS or HTML.

Integration

  • pagedjs:

    Paged.js is designed to work seamlessly with existing web applications, allowing developers to generate documents directly from their HTML content. This makes it an excellent choice for projects that already utilize web technologies extensively.

  • pdfmake:

    pdfmake is also easy to integrate into web applications, providing a straightforward way to generate PDFs on the client-side or server-side. Its reliance on JavaScript makes it a good fit for applications built with frameworks like React, Angular, or Vue.

How to Choose: pagedjs vs pdfmake

  • pagedjs:

    Choose Paged.js if your primary focus is on generating print-ready documents from HTML and CSS, as it excels in rendering web content with a high degree of fidelity to web standards. It is particularly beneficial for projects that require complex layouts, pagination, and styling, leveraging the power of CSS for print media.

  • pdfmake:

    Choose pdfmake if you need a straightforward way to generate PDF documents programmatically with a JSON-based API. It is ideal for applications that require dynamic PDF generation with features like tables, images, and custom styles, while also offering a simpler learning curve for those familiar with JavaScript.

README for pagedjs

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:

<script src="https://unpkg.com/pagedjs/dist/paged.polyfill.js"></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.

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

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

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

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

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.

<script src="https://unpkg.com/pagedjs/dist/paged.polyfill.js"></script>
<script>
	class MyHandler extends Paged.Handler {
		constructor(chunker, polisher, caller) {
			super(chunker, polisher, caller);
		}

		afterPageLayout(pageFragment, page) {
			console.log(pageFragment);
		}
	}
	Paged.registerHandlers(MyHandler);
</script>

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)

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

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