PDF generator for scripted documents.
A library that:
-
Converts a scripted document (HTML, CSS, and JavaScript) to a PDF (using wkhtmltopdf)
-
Is used as a command line interface:
$ npm install -g documark-cli $ cd /path/to/my-document/ $ npm install documark $ edit document.html $ documark compile -
Can watch for files changes to recompile the document (
documark compile --watch)
My personally hatret towards WYSIWYG word processors (Word, Pages, etc.) sparked me to write this tool. I have used LaTeX for a while, but it felt like a waste of time. So instead I figured: why not use web technologies? I like Documark because it:
- Separates content and styling
- Uses mature webtechnologies like HTML, JS, and CSS for writing and styling the document
- Enforces a consistent document style. No more dragging around of table columns and floating images
- Allows version control with Git or SVN
- Simplifies collaboration by version control and splitting up the document into separate files
- Allows you to use your favorite text editor - like Vim ❤
- Makes automating things (through plugins) real easy
- Enables you to use libraries like D3 and MathJax for generating graphs and math formulas!
- Run
npm install -g documark-clito make thedocumarkcommand available - Currently manually installing wkhtmltopdf v0.12.2.1+ is still required, but we're working on this!
Go to the Documark example repository for a generated PDF and its source code.
-
Install the Documark CLI and wkhtmltopdf
-
Navigate to (an empty) document directory:
$ mkdir ~/Documents/MyDocument && cd $_ -
Install Documark:
$ npm install documark -
Install some basic styling:
$ npm i dmp-style-basic -
Add a
document.htmlfile:<!-- title: My Document plugins: - dmp-style-basic --> <chapter> <h1>My Document</h1> <p>Hello world!</p> </chapter>
-
Run
$ documark compile -
And finally open
Document.pdf
Document configuration can be done in two ways:
- In the document's front matter.
- In a separate
config.jsonfile.
If there is front matter in the document, the configuration file will be ignored.
Add plugins via the plugins key in the document.html front matter:
<!--
{
"title": "Document",
"plugins": ["dmp-plugin-loader", "dmp-hr-to-page-break"]
}
-->Plugins all have the documark-plugin keyword. They are listed on the NPM website.
Tip: Use the documark plugin loader to load custom plugins!
Themes are loaded as plugins and should be prefixed with dmp-theme-. A theme generally consists of some styling and other useful plugins, like table of contents, page headers, footers, margins, or math equations.
The easiest way is to load a predefined document style as a plugin. These plugins are prefixed with dmp-style-.
Another way of styling your document is through CSS files, inline CSS, or the element's style attribute.
These are the steps for compiling the PDF document:
- Input files (HTML, configuration, and assets)
- Convert to DOM tree (with CheerioJS)
- Process plugins (which can alter the DOM and PDF configuration)
- Emit
pre-compileevent - Generate PDF
- Emit
post-compileevent
Configure wkhtmltopdf with the pdf object in the documents front matter. For example:
<!--
{
"title": "Document",
"pdf": {
"userStyleSheet": "path/to/main.css"
}
}
-->Note that node-wkhtmltopdf is used as an intermediate package, which uses camel cased (userStyleSheet) options instead of dashed ones (user-style-sheet, like in the command line tool). See this page for a full list of configuration options.
Writing your own plugins is easy! Here's a boilerplate for a plugin named dmp-my-custom-plugin (dmp- is short for Documark plugin):
// Require modules outside the plugin function
var path = require('path');
// Add camel cased plugin name to function (for debugging)
module.exports = function dmpMyCustomPlugin ($, document, done) {
// Manipulate the DOM tree
$('my-custom-element').replaceWith('<p>Hello world!</p>');
// Or alter the configuration
document.config().pdf.marginLeft = '5cm';
// Don't forget to let Documark know the plugin is done!
done();
};A plugin has the following parameters:
$: the CheerioJS DOM tree (works a lot like jQuery) of the entire document.document: the Document instance. Usedocument.config()to get/set configuration variables.done: the callback function. Don't forget to call this at the end!
Finally load your plugin in your document configuration:
<!--
{
"plugins": ["dmp-my-custom-plugin"]
}
-->
<chapter>
<my-custom-element />
</chapter>- Move CLI commands to
documark-cli - Refactor codebase
- Add unit tests (for Travis)
- Research alternatives to wkhtmltopdf (#12)
- Use wkhtmltopdf binary package to automatically download the required wkhtmltopdf tools
- Build tools for debugging (dmp-debug, logger etc)
- Improve support: set up website, write wiki pages, and set up IRC channel
- Create Yeoman generator for easy document/plugin setup:
yo documarkandyo documark-plugin - Including code files/snippets with highlighting
- Create scientific - LaTex like - theme
- Landscape pages (not possible yet ◔̯◔)