<commit_before>[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/alvarcarto/url-to-pdf-api)

[![Build Status](https://travis-ci.org/alvarcarto/url-to-pdf-api.svg?branch=master)](https://travis-ci.org/alvarcarto/url-to-pdf-api)

# URL to PDF Microservice

> Web page PDF rendering done right. Microservice for rendering receipts, invoices, or any content. Packaged to an easy API.

![Logo](docs/logo.png)

**⚠️ WARNING ⚠️** *Don't serve this API publicly to the internet unless you are aware of the
risks. It allows API users to run any JavaScript code inside a Chrome session on the server.
It's fairly easy to expose the contents of files on the server. You have been warned!. See https://github.com/alvarcarto/url-to-pdf-api/issues/12 for background.*

**⭐️ Features:**

* Converts any URL or HTML content to a PDF file or an image (PNG/JPEG)
* Rendered with Headless Chrome, using [Puppeteer](https://github.com/GoogleChrome/puppeteer). The PDFs should match to the ones generated with a desktop Chrome.
* Sensible defaults but everything is configurable.
* Single-page app (SPA) support. Waits until all network requests are finished before rendering.
* Easy deployment to Heroku. We love Lambda but...Deploy to Heroku button.
* Renders lazy loaded elements. *(scrollPage option)*
* Supports optional `x-api-key` authentication. *(`API_TOKENS` env var)*

Usage is as simple as https://url-to-pdf-api.herokuapp.com/api/render?url=http://google.com. There's also a `POST /api/render` if you prefer to send options in the body.

**🔍 Why?**

This microservice is useful when you need to automatically produce PDF files
for whatever reason. The files could be receipts, weekly reports, invoices,
or any content.

PDFs can be generated in many ways, but one of them is to convert HTML+CSS
content to a PDF. This API does just that.

**🚀 Shortcuts:**

* [Examples](#examples)
* [API](#api)
* [I want to run this myself](#development)

## How it works

![](docs/heroku.png)

Local setup is identical except Express API is running on your machine
and requests are direct connections to it.

### Good to know

* **By default, page's `@media print` CSS rules are ignored**. We set Chrome to emulate `@media screen` to make the default PDFs look more like actual sites. To get results closer to desktop Chrome, add `&emulateScreenMedia=false` query parameter. See more at [Puppeteer API docs](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions).

* Chrome is launched with `--no-sandbox --disable-setuid-sandbox` flags to fix usage in Heroku. See [this issue](https://github.com/GoogleChrome/puppeteer/issues/290).

* Heavy pages may cause Chrome to crash if the server doesn't have enough RAM.

* Docker wrapper for this can be found here: https://github.com/microbox/node-url-to-pdf-api


## Examples

*Note: the demo Heroku app runs on a free dyno which sleep after idle.
A request to sleeping dyno may take even 30 seconds.*

**The most minimal example, render google.com**

https://url-to-pdf-api.herokuapp.com/api/render?url=http://google.com

**The most minimal example, render google.com as PNG image**
<commit_msg>Point to different docker Image that is actually maintained<commit_after>[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/alvarcarto/url-to-pdf-api)

[![Build Status](https://travis-ci.org/alvarcarto/url-to-pdf-api.svg?branch=master)](https://travis-ci.org/alvarcarto/url-to-pdf-api)

# URL to PDF Microservice

> Web page PDF rendering done right. Microservice for rendering receipts, invoices, or any content. Packaged to an easy API.

![Logo](docs/logo.png)

**⚠️ WARNING ⚠️** *Don't serve this API publicly to the internet unless you are aware of the
risks. It allows API users to run any JavaScript code inside a Chrome session on the server.
It's fairly easy to expose the contents of files on the server. You have been warned!. See https://github.com/alvarcarto/url-to-pdf-api/issues/12 for background.*

**⭐️ Features:**

* Converts any URL or HTML content to a PDF file or an image (PNG/JPEG)
* Rendered with Headless Chrome, using [Puppeteer](https://github.com/GoogleChrome/puppeteer). The PDFs should match to the ones generated with a desktop Chrome.
* Sensible defaults but everything is configurable.
* Single-page app (SPA) support. Waits until all network requests are finished before rendering.
* Easy deployment to Heroku. We love Lambda but...Deploy to Heroku button.
* Renders lazy loaded elements. *(scrollPage option)*
* Supports optional `x-api-key` authentication. *(`API_TOKENS` env var)*

Usage is as simple as https://url-to-pdf-api.herokuapp.com/api/render?url=http://google.com. There's also a `POST /api/render` if you prefer to send options in the body.

**🔍 Why?**

This microservice is useful when you need to automatically produce PDF files
for whatever reason. The files could be receipts, weekly reports, invoices,
or any content.

PDFs can be generated in many ways, but one of them is to convert HTML+CSS
content to a PDF. This API does just that.

**🚀 Shortcuts:**

* [Examples](#examples)
* [API](#api)
* [I want to run this myself](#development)

## How it works

![](docs/heroku.png)

Local setup is identical except Express API is running on your machine
and requests are direct connections to it.

### Good to know

* **By default, page's `@media print` CSS rules are ignored**. We set Chrome to emulate `@media screen` to make the default PDFs look more like actual sites. To get results closer to desktop Chrome, add `&emulateScreenMedia=false` query parameter. See more at [Puppeteer API docs](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions).

* Chrome is launched with `--no-sandbox --disable-setuid-sandbox` flags to fix usage in Heroku. See [this issue](https://github.com/GoogleChrome/puppeteer/issues/290).

* Heavy pages may cause Chrome to crash if the server doesn't have enough RAM.

* Docker image for this can be found here: https://github.com/restorecommerce/pdf-rendering-srv


## Examples

*Note: the demo Heroku app runs on a free dyno which sleep after idle.
A request to sleeping dyno may take even 30 seconds.*

**The most minimal example, render google.com**

https://url-to-pdf-api.herokuapp.com/api/render?url=http://google.com

**The most minimal example, render google.com as PNG image**

{'fraction_non_alphanumeric': 0.10958056028478563, 'fraction_numerical': 0.0021668472372697724, 'mean_word_length': 4.425692695214106, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 24, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '993815', 'n_tokens_mistral': 1981, 'n_tokens_neox': 1926, 'n_words': 784}