Skip to content

Commit

Permalink
Use onLoadFinished event to determine when to render the page. Add lo…
Browse files Browse the repository at this point in the history
…adTimeout option to limit time page can take to load.
  • Loading branch information
alanshaw committed Jan 25, 2015
1 parent 0871371 commit 3fe390d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
44 changes: 26 additions & 18 deletions README.md
Expand Up @@ -33,61 +33,67 @@ markdownpdf().from("/path/to/document.md").to("/path/to/document.pdf", function
Pass an options object (`markdownpdf({/* options */})`) to configure the output.

#### options.phantomPath
Type: `String`
Default value: `Path provided by phantomjs module`
Type: `String`
Default value: Path provided by phantomjs module

Path to phantom binary

#### options.cssPath
Type: `String`
Default value: `[module path]/markdown-pdf/pdf.css`
Type: `String`
Default value: `[module path]/markdown-pdf/css/pdf.css`

Path to custom CSS file, relative to the current directory

#### options.highlightCssPath
Type: `String`
Default value: `[module path]/markdown-pdf/highlight.css`
Type: `String`
Default value: `[module path]/markdown-pdf/css/highlight.css`

Path to custom highlight CSS file (for code highlighting), relative to the current directory
Path to custom highlight CSS file (for code highlighting with [highlight.js](https://highlightjs.org)), relative to the current directory.

#### options.paperFormat
Type: `String`
Type: `String`
Default value: `A4`

'A3', 'A4', 'A5', 'Legal', 'Letter' or 'Tabloid'

#### options.paperOrientation
Type: `String`
Type: `String`
Default value: `portrait`

'portrait' or 'landscape'

#### options.paperBorder
Type: `String`
Type: `String`
Default value: `1cm`

Supported dimension units are: 'mm', 'cm', 'in', 'px'

#### options.runningsPath
Type: `String`
Type: `String`
Default value: `runnings.js`

Path to CommonJS module which sets the page header and footer (see [runnings.js](lib/runnings.js))

#### options.renderDelay
Type: `Number`
Default value: `1000`
Type: `Number`
Default value: Time until [`page.onLoadFinished`](http://phantomjs.org/api/webpage/handler/on-load-finished.html) event fired

Delay in millis before rendering the PDF (give HTML and CSS a chance to load)
Delay in millis before rendering the PDF

#### options.loadTimeout
Type: `Number`
Default value: `10000`

If `renderDelay` option isn't set, this is the timeout (in ms) before the page is rendered in case the `page.onLoadFinished` event doesn't fire.

#### options.preProcessMd
Type: `Function`
Type: `Function`
Default value: `function () { return through() }`

A function that returns a [through2 stream](https://npmjs.org/package/through2) that transforms the markdown before it is converted to HTML.

#### options.preProcessHtml
Type: `Function`
Type: `Function`
Default value: `function () { return through() }`

A function that returns a [through2 stream](https://npmjs.org/package/through2) that transforms the HTML before it is converted to PDF.
Expand Down Expand Up @@ -204,21 +210,23 @@ npm install -g markdown-pdf

### Usage

```sh
```
Usage: markdown-pdf [options] <markdown-file-path>
Options:
-h, --help output usage information
-V, --version output the version number
<markdown-file-path> Path of the markdown file to convert
-c, --cwd [path] Current working directory
-p, --phantom-path [path] Path to phantom binary
-h, --runnings-path [path] Path to runnings (header, footer)
-s, --css-path [path] Path to custom CSS file
-z, --highlight-css-path [path] Path to custom highlight-CSS file
-f, --paper-format [format] 'A3', 'A4', 'A5', 'Legal', 'Letter' or 'Tabloid'
-r, --paper-orientation [orientation] 'portrait' or 'landscape'
-b, --paper-border [measurement] Supported dimension units are: 'mm', 'cm', 'in', 'px'
-d, --render-delay [millis] Delay before rendering the PDF (give HTML and CSS a chance to load)
-d, --render-delay [millis] Delay before rendering the PDF
-t, --load-timeout [millis] Timeout before the page is rendered in case `page.onLoadFinished` isn't fired
-o, --out [path] Path of where to save the PDF
```
9 changes: 4 additions & 5 deletions bin/markdown-pdf
Expand Up @@ -14,7 +14,8 @@ program.version(require('../package.json').version)
.option('-f, --paper-format [format]', "'A3', 'A4', 'A5', 'Legal', 'Letter' or 'Tabloid'")
.option('-r, --paper-orientation [orientation]', "'portrait' or 'landscape'")
.option('-b, --paper-border [measurement]', "Supported dimension units are: 'mm', 'cm', 'in', 'px'")
.option('-d, --render-delay [millis]', "Delay before rendering the PDF (give HTML and CSS a chance to load)")
.option('-d, --render-delay [millis]', "Delay before rendering the PDF")
.option('-t, --load-timeout [millis]', "Timeout before the page is rendered in case `page.onLoadFinished` isn't fired")
.option('-o, --out [path]', "Path of where to save the PDF")
.parse(process.argv)

Expand All @@ -32,11 +33,9 @@ var opts = {
, paperOrientation: program.paperOrientation
, paperBorder: program.paperBorder
, renderDelay: program.renderDelay
, loadTimeout: program.loadTimeout
}

markdownpdf(opts).from(program.args[0]).to(program.out, function (er) {
if (er) {
console.error(er)
return process.exit(-1)
}
if (er) throw er
})
4 changes: 3 additions & 1 deletion index.js
Expand Up @@ -21,7 +21,8 @@ function markdownpdf (opts) {
opts.paperFormat = opts.paperFormat || "A4"
opts.paperOrientation = opts.paperOrientation || "portrait"
opts.paperBorder = opts.paperBorder || "1cm"
opts.renderDelay = opts.renderDelay || 500
opts.renderDelay = opts.renderDelay == null ? 0 : opts.renderDelay
opts.loadTimeout = opts.loadTimeout == null ? 10000 : opts.loadTimeout
opts.preProcessMd = opts.preProcessMd || function () { return through() }
opts.preProcessHtml = opts.preProcessHtml || function () { return through() }

Expand Down Expand Up @@ -88,6 +89,7 @@ function markdownpdf (opts) {
, opts.paperOrientation
, opts.paperBorder
, opts.renderDelay
, opts.loadTimeout
]

childProcess.execFile(opts.phantomPath, childArgs, function (er, stdout, stderr) {
Expand Down
19 changes: 15 additions & 4 deletions phantom/render.js
Expand Up @@ -4,7 +4,7 @@ var system = require("system")
, os = require("system").os

// Read in arguments
var args = ["in", "out", "cwd", "runningsPath", "cssPath", "highlightCssPath", "paperFormat", "paperOrientation", "paperBorder", "renderDelay", "jsonPath"].reduce(function (args, name, i) {
var args = ["in", "out", "cwd", "runningsPath", "cssPath", "highlightCssPath", "paperFormat", "paperOrientation", "paperBorder", "renderDelay", "loadTimeout"].reduce(function (args, name, i) {
args[name] = system.args[i + 1]
return args
}, {})
Expand Down Expand Up @@ -36,12 +36,23 @@ page.evaluate(function (cssPaths) {
// Set the PDF paper size
page.paperSize = paperSize(args.runningsPath, {format: args.paperFormat, orientation: args.paperOrientation, border: args.paperBorder})

// Render the page
setTimeout(function () {
args.renderDelay = parseInt(args.renderDelay, 10)

if (args.renderDelay) {
setTimeout(render, args.renderDelay)
} else {
var loadTimeout = setTimeout(render, parseInt(args.loadTimeout, 10))
page.onLoadFinished = function () {
clearTimeout(loadTimeout)
render()
}
}

function render () {
page.render(args.out)
page.close()
phantom.exit(0)
}, parseInt(args.renderDelay, 10))
}

function paperSize (runningsPath, obj) {
var runnings = require(runningsPath)
Expand Down

0 comments on commit 3fe390d

Please sign in to comment.