Skip to content

Commit

Permalink
Migrate from html-pdf to puppeteer
Browse files Browse the repository at this point in the history
Sadly with the migration to Puppeteer we're going to make headers and
footers significantly worse due to a number of bugs and incomplete
features in Puppeteers headers and footers. In an effort to move mdpdf
forward we'll make a release with these changes and see what people
say. After all, the best way to get feedback on the internet is to be wrong.
  • Loading branch information
BlueHatbRit committed Jan 19, 2019
1 parent 02b7d40 commit b816c4b
Show file tree
Hide file tree
Showing 106 changed files with 775 additions and 4,659 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
language: node_js
node_js:
- "4"
- "5"
- "6"
- "7"
- "8"
- "9"
- "10"
- "11"

branches:
only:
Expand Down
15 changes: 15 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Contributing to MDPDF

Thanks for contributing to MDPDF, we try to make it as simple as possible to contribute and we accept contributions in a variety of different ways.

Before starting, please familiarise yourself with the [Code of Conduct](./CODE_OF_CONDUCT.md). This code of conduct is enforced across all mediums used to maintain mdpdf including github, mailing lists, forums, irc channels, etc.

## Bug reports

When submitting bug reports, please provide as much detail as possible and ensure you follow any issue templates. Bug reports are best submitted via GitHub but can also be emailed to project maintainers.

## Fixes, features, and code

Before getting started with your feature, fix, or code it's a good idea to ensure there is an issue open for what you're about to do. This ensures we can track discussion

If you'd like to contibute code we'd be delighted to review and consider it either over GitHub pull requests or by emailing a patch it to one of the project maintainers listed in the [package.json](./package.json). We will try to review and discuss your changes as soon as possible but mdpdf is a side project with few maintainers so please be paitent.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# MDPDF - Markdown to PDF converter
[![Build Status](https://travis-ci.org/BlueHatbRit/mdpdf.svg?branch=master)](https://travis-ci.org/BlueHatbRit/mdpdf) [![Build status](https://ci.appveyor.com/api/projects/status/x0ng3luokjb9eosm/branch/master?svg=true)](https://ci.appveyor.com/project/BlueHatbRit/mdpdf/branch/master) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo) [![NPM version](https://img.shields.io/npm/v/mdpdf.svg)](https://www.npmjs.com/package/mdpdf)
[![Build Status](https://travis-ci.org/BlueHatbRit/mdpdf.svg?branch=master)](https://travis-ci.org/BlueHatbRit/mdpdf) [![Build status](https://ci.appveyor.com/api/projects/status/x0ng3luokjb9eosm/branch/master?svg=true)](https://ci.appveyor.com/project/BlueHatbRit/mdpdf/branch/master) [![NPM version](https://img.shields.io/npm/v/mdpdf.svg)](https://www.npmjs.com/package/mdpdf) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)

A command line markdown to pdf converter with support for page headers, footers, and custom stylesheets. Mdpdf is incredibly configurable and has a JavaScript API for more extravogant usage.

Expand All @@ -9,10 +9,12 @@ For examples of how to use headers and footers, see the [examples directory](./e

## Donate

Like this project and want to donate towards it's continued development?
This project is actively maintained during evenings and weekends, please consider either donating or contributing in other ways.

<a href="https://www.buymeacoffee.com/elliot" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Donate" style="height: auto !important;width: auto !important;" ></a>

If you wish to report bugs or contribute fixes and features, please see the [contributors guide](./CONTRIBUTING.md)

## Installation

Install globally to use from the command line.
Expand Down Expand Up @@ -49,19 +51,25 @@ Install locally to access the API.

Length parameters (`<height>` and `<size>`) require a unit. Valid units are `mm`, `cm`, `in` and `px`.

## Headers and footers

Mdpdf is powered by Puppeteer, the headless Chrome browser project by Google. [Puppeteer provides a number of header and footer html classes which can be used to insert things such as page numbers](https://github.com/GoogleChrome/puppeteer/blob/v1.11.0/docs/api.md#pagepdfoptions).

**Note:** mdpdf pre-2.x made use of Phantom.js which had considerably better support for headers and footers, and including better styling support for them. Sadly Phantom.js is no longer supported and had a number of other rendering bugs meaning it was no longer possible to support it as a core component of mdpdf. If you previously relied on good header and footer support you may wish to stick with 1.x releases until Puppeteer prioritises better header and footer support. Currently headers and footers do work in 2.x+ releases, but their styles must be handled independently of the main markdown file via `--styles` options and a few css tags do not work correctly. Past this there shouldn't be any issues with 2.x+ headers and footers.

## Environment variables

* `MDPDF_STYLES` - The full path to a stylesheet you wish to use if `--style` is not specified when calling `mdpdf` from the command line.

## Emoji Support
## Emoji support

In text emoji's are also supported, but there are a few instances of shorthand which do not work and require the longhand version, i.e. `:+1:` doesn't work but `:thumbsup:` will.

## Programatic API

The API is very straight forward with a single function `convert()` which takes some options. The convert method uses a promise generated by the [Bluebird.js](bluebirdjs.com) library. For a full example see the [bin/index.js](./bin/index.js)!

### Example API Usage
### Example API usage

```JavaScript
const mdpdf = require('mdpdf');
Expand Down Expand Up @@ -92,7 +100,7 @@ mdpdf.convert(options).then((pdfPath) => {
* `ghStyle` - Boolean value of whether or not to use the GitHub Markdown CSS, *defaults to false*.
* `defaultStyle` - Boolean value of whether or not to use the additional default styles. These styles provide some things like a basic border and font size. *Defaults to false*.
* `header` - Full path to a the Handlebars (`.hbs`) file which will be your header. If you set this, you must set the header height (see below).
* `debug` - When this is set the intermediate HTML will be saved into a file, the value of this field should be the full path to the destination HTML.
* `debug` - When this is set the intermediate HTML will be saved into a file.
* `pdf` (**required**) - An object which contains some sub parameters to control the final PDF document
* `format` (**required**) - Final document format, allowed values are "A3, A4, A5, Legal, Letter, Tabloid"
* `orientation` - Final document size orientation, allowed values are "potrait, orientation"
Expand Down
6 changes: 4 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Test against the latest version of this Node.js version
environment:
matrix:
- nodejs_version: "11"
- nodejs_version: "10"
- nodejs_version: "9"
- nodejs_version: "8"
- nodejs_version: "7"
- nodejs_version: "6"
- nodejs_version: "5"
- nodejs_version: "4"

branches:
only:
Expand Down
139 changes: 77 additions & 62 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const fs = require('fs');
const meow = require('meow');
const mdpdf = require('../');

const cli = meow(`
const cli = meow(
`
Usage:
$ mdpdf <source> [<destination>] [options]
Expand Down Expand Up @@ -41,37 +42,40 @@ const cli = meow(`
Global Settings:
You can also set a global default stylesheet by setting the MDPDF_STYLES environment
variable as the path to your single css stylesheet. The --style flag will override this.
`, {
alias: {
s: 'style',
h: 'header',
f: 'footer',
d: 'debug',
v: 'version',
r: 'format',
o: 'orientation'
}
});
`,
{
alias: {
s: 'style',
h: 'header',
f: 'footer',
d: 'debug',
v: 'version',
r: 'format',
o: 'orientation',
},
}
);

function isMd(path) {
if (!path) {
return true;
}
const accepted = ['md'];
const current = path.split('.').pop();
if (accepted.indexOf(current) !== -1) {
return true;
}
return false;
if (!path) {
return true;
}
const accepted = ['md'];
const current = path.split('.').pop();
if (accepted.indexOf(current) !== -1) {
return true;
}
return false;
}

const source = cli.input[0];
if (!source || !isMd(source)) {
// Invalid source, show help and exit
cli.showHelp();
// Invalid source, show help and exit
cli.showHelp();
}

const destination = cli.input[1] || source.slice(0, source.indexOf('.md')) + '.pdf';
const destination =
cli.input[1] || source.slice(0, source.indexOf('.md')) + '.pdf';
const debug = cli.flags.debug || false;
let style = cli.flags.style;
const header = cli.flags.header;
Expand All @@ -91,46 +95,57 @@ const envStyleName = 'MDPDF_STYLES';

// If styles have not been provided through the CLI flag, but the environment variable exists
if (!style && process.env[envStyleName]) {
// Ensure the css file exists
const envCssPath = path.resolve(process.env[envStyleName]);
if (fs.existsSync(envCssPath)) {
style = envCssPath;
}
// Ensure the css file exists
const envCssPath = path.resolve(process.env[envStyleName]);
if (fs.existsSync(envCssPath)) {
style = envCssPath;
}
}

const options = {
ghStyle: !style,
defaultStyle: true,
source: path.resolve(source),
destination: path.resolve(destination),
styles: style ? path.resolve(style) : null,
header: header ? path.resolve(header) : null,
footer: footer ? path.resolve(footer) : null,
noEmoji: cli.flags.noEmoji || false,
debug: debug ? source.slice(0, source.indexOf('.md')) + '.html' : null,
pdf: {
format: pdfFormat,
orientation: pdfOrientation,
quality: '100',
base: path.join('file://', __dirname, '/assets/'),
header: {
height: headerHeight || null
},
footer: {
height: footerHeight || null
},
border: {
top: borderTop,
left: borderLeft,
bottom: borderBottom,
right: borderRight
}
}
ghStyle: !style,
defaultStyle: true,
source: path.resolve(source),
destination: path.resolve(destination),
styles: style ? path.resolve(style) : null,
header: header ? path.resolve(header) : null,
footer: footer ? path.resolve(footer) : null,
noEmoji: cli.flags.noEmoji || false,
debug: debug
? path.resolve(source.slice(0, source.indexOf('.md')) + '.html')
: null,
pdf: {
format: pdfFormat,
orientation: pdfOrientation,
quality: '100',
base: path.join('file://', __dirname, '/assets/'),
header: {
height: headerHeight || null,
},
footer: {
height: footerHeight || null,
},
border: {
top: borderTop,
left: borderLeft,
bottom: borderBottom,
right: borderRight,
},
},
};

mdpdf.convert(options).then(pdfPath => {
console.log('✨ PDF created successfully at:', pdfPath);
}).catch(err => {
console.error(err);
process.exitCode = 1;
});
return mdpdf
.convert(options)
.then(pdfPath => {
// Pretty print for terminals, or just return the output
// path for scripts and pipes.
if (process.stdout.isTTY) {
console.log('✨ PDF created successfully at:', pdfPath);
} else {
console.log(pdfPath);
}
})
.catch(err => {
console.error(err);
process.exitCode = 1;
});
2 changes: 1 addition & 1 deletion examples/footers/footer.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<center>
<center style="font-size: 10px">
<p><b>MDPDF - An elegant markdown to pdf converter</b></p>
</center>
2 changes: 1 addition & 1 deletion examples/headers/header.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<center>
<center style="font-size: 10px">
<h3>MDPDF - An elegant markdown to pdf converter</h3>
</center>
Loading

0 comments on commit b816c4b

Please sign in to comment.