<commit_before>![SPHERE.IO icon](https://admin.sphere.io/assets/images/sphere_logo_rgb_long.png)

# Express PDF

[![Build Status](https://travis-ci.org/sphereio/sphere-express-pdf.svg?branch=master)](https://travis-ci.org/sphereio/sphere-express-pdf) [![Coverage Status](https://coveralls.io/repos/sphereio/sphere-express-pdf/badge.png?branch=master)](https://coveralls.io/r/sphereio/sphere-express-pdf?branch=master) [![Dependency Status](https://david-dm.org/sphereio/sphere-express-pdf.svg?theme=shields.io)](https://david-dm.org/sphereio/sphere-express-pdf) [![devDependency Status](https://david-dm.org/sphereio/sphere-express-pdf/dev-status.svg?theme=shields.io)](https://david-dm.org/sphereio/sphere-express-pdf#info=devDependencies)

Run a Restlet webserver to generate PDFs from HTML, using phantom.js

> Inspired from [html2pdf.it](https://github.com/Muscula/html2pdf.it)

## Getting Started

> Make sure to have [phantom.js](http://phantomjs.org/) installed

```bash
$ brew install phantom
```

Start the webserver

```bash
$ npm start
# or
$ grunt run # will watch for changes and keep the server alive
```

To run it on **production** set the environment

```bash
$ NODE_ENV=production npm start
```

You can also pipe the logs (pretty printed) with `bunyan`

```bash
$ npm start | bunyan -o short

> sphere-express-pdf@0.1.0 start /Users/nicola/dev/src/sphere-express-pdf
> node ./lib/app.js

11:02:04.039Z  INFO sphere-express-pdf: Starting express application on port 3999 (development)
11:02:04.260Z  INFO sphere-express-pdf: Listening for HTTP on http://localhost:3999
...
```

### StrongOps support
If you would like to monitor your application there is a default support for [StrongOps](http://strongloop.com/node-js-performance/strongops/) monitoring.

Install the `slc` command-line tool (optional, but strongly recommended)

```bash
$ npm install -g strong-cli
```

You can enable profiling by either:
- use a `strongloop.json` configuration file
- set `ENV` variables before running the app

```bash
SL_KEY=<API_KEY> SL_APP_NAME=sphere-express-pdf node lib/app.js |bunyan -o short

strong-agent profiling app 'sphere-express-pdf' pid '38855'
strong-agent dashboard is at https://strongops.strongloop.com
12:07:01.297Z  INFO sphere-express-pdf: Starting express application on port 3999 (development)
12:07:01.428Z  INFO sphere-express-pdf: Listening for HTTP on http://localhost:3999
```

If you don't setup monitoring, the app still works, you will just see this output when starting the server

```bash
$ node lib/app.js |bunyan -o short

strong-agent not profiling, configuration not found.
Generate configuration with:
    npm install -g strong-cli
    slc strongops
See http://docs.strongloop.com/strong-agent for more information.
12:07:01.297Z  INFO sphere-express-pdf: Starting express application on port 3999 (development)
12:07:01.428Z  INFO sphere-express-pdf: Listening for HTTP on http://localhost:3999
```


## Documentation
The webserver started by express.js offers some JSON endpoints to work with PDFs.
There are 2 basic principles regarding the endpoints:

- `POST` some data which will be used to generate a PDF
- `GET` the generated PDF in different ways

### Request body
```javascript
// defaults
{
  "paperSize": {
    "format": "A4", // possible values ['A3', 'A4', 'A5', 'Legal', 'Letter', 'Tabloid']
    "orientation": "portrait", // possible values ['portrait', 'landscape']
    "border": "1cm", // possible units ['mm', 'cm', 'in', 'px']
  },
  "content": "", // an HTML string with Handlebars template syntax
  "context": {}, // a JSON object used to pass as context in the template
  "download": false // true if the url link should trigger a PDF download, otherwise it will be rendered in the browser
}
```

### API

##### POST `/api/pdf/url`
Will generate a PDF based on the given `payload` data and returns a JSON with a
link to the PDF.
> Note that the link will **expire** after some time<commit_msg>Adjust readme<commit_after>![SPHERE.IO icon](https://admin.sphere.io/assets/images/sphere_logo_rgb_long.png)

# Express PDF

[![Build Status](https://travis-ci.org/sphereio/sphere-express-pdf.svg?branch=master)](https://travis-ci.org/sphereio/sphere-express-pdf) [![Coverage Status](https://coveralls.io/repos/sphereio/sphere-express-pdf/badge.png?branch=master)](https://coveralls.io/r/sphereio/sphere-express-pdf?branch=master) [![Dependency Status](https://david-dm.org/sphereio/sphere-express-pdf.svg?theme=shields.io)](https://david-dm.org/sphereio/sphere-express-pdf) [![devDependency Status](https://david-dm.org/sphereio/sphere-express-pdf/dev-status.svg?theme=shields.io)](https://david-dm.org/sphereio/sphere-express-pdf#info=devDependencies)

Run a Restlet webserver to generate PDFs from HTML, using phantom.js

> Inspired from [html2pdf.it](https://github.com/Muscula/html2pdf.it)

## Getting Started

> Make sure to have [phantom.js](http://phantomjs.org/) installed

```bash
$ brew install phantom
```

Start the webserver

```bash
$ npm start
# or
$ grunt run # will watch for changes and keep the server alive
```

To run it on **production** set the environment

```bash
$ NODE_ENV=production npm start
```

You can also pipe the logs (pretty printed) with `bunyan`

```bash
$ npm start | bunyan -o short

> sphere-express-pdf@0.1.0 start /Users/nicola/dev/src/sphere-express-pdf
> node ./lib/app.js

11:02:04.039Z  INFO sphere-express-pdf: Starting express application on port 3999 (development)
11:02:04.260Z  INFO sphere-express-pdf: Listening for HTTP on http://localhost:3999
...
```

### StrongOps support
If you would like to monitor your application there is a default support for [StrongOps](http://strongloop.com/node-js-performance/strongops/) monitoring.

Install the `slc` command-line tool (optional, but strongly recommended)

```bash
$ npm install -g strong-cli
```

You can enable profiling by either:
- use a `strongloop.json` configuration file
- set `ENV` variables before running the app

```bash
SL_KEY=<API_KEY> SL_APP_NAME=sphere-express-pdf slc run |bunyan -o short

strong-agent profiling app 'sphere-express-pdf' pid '38855'
strong-agent dashboard is at https://strongops.strongloop.com
12:07:01.297Z  INFO sphere-express-pdf: Starting express application on port 3999 (development)
12:07:01.428Z  INFO sphere-express-pdf: Listening for HTTP on http://localhost:3999
```

> If you don't setup monitoring, the app still works


## Documentation
The webserver started by express.js offers some JSON endpoints to work with PDFs.
There are 2 basic principles regarding the endpoints:

- `POST` some data which will be used to generate a PDF
- `GET` the generated PDF in different ways

### Request body
```javascript
// defaults
{
  "paperSize": {
    "format": "A4", // possible values ['A3', 'A4', 'A5', 'Legal', 'Letter', 'Tabloid']
    "orientation": "portrait", // possible values ['portrait', 'landscape']
    "border": "1cm", // possible units ['mm', 'cm', 'in', 'px']
  },
  "content": "", // an HTML string with Handlebars template syntax
  "context": {}, // a JSON object used to pass as context in the template
  "download": false // true if the url link should trigger a PDF download, otherwise it will be rendered in the browser
}
```

### API

##### POST `/api/pdf/url`
Will generate a PDF based on the given `payload` data and returns a JSON with a
link to the PDF.
> Note that the link will **expire** after some time
{'fraction_non_alphanumeric': 0.13544048419636853, 'fraction_numerical': 0.021788836583725622, 'mean_word_length': 4.56170531039641, 'pattern_counts': {'":': 14, '<': 5, '<?xml version=': 0, '>': 16, 'https://': 22, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6732053', 'n_tokens_mistral': 2517, 'n_tokens_neox': 2277, 'n_words': 742}