Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
kritzware committed Sep 2, 2021
2 parents 6882337 + 2a02f0c commit 5ee30a8
Show file tree
Hide file tree
Showing 14 changed files with 378 additions and 124 deletions.
12 changes: 4 additions & 8 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/bin/bash

# Guide:
#
# 1. Copy this file to .env
Expand All @@ -8,11 +6,9 @@
#
# 2. Fill the blanks

export NODE_ENV=development
export PORT=9000
export ALLOW_HTTP=true
NODE_ENV=development
PORT=9000
ALLOW_HTTP=true

# Warning: PDF rendering does not work in Chrome when it is in headed mode.
export DEBUG_MODE=false

echo "Environment variables set!"
DEBUG_MODE=false
19 changes: 19 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/src/index.js",
"env": {
"NODE_ENV": "development",
"PORT": "9000",
"ALLOW_HTTP": "true",
}
}
]
}
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# CHANGELOG

* change the `:html` output to return `document.documentElement.innerHTML` instead of previously used `document.body.innerHTML`

## 1.0.0

* initial version
38 changes: 22 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,17 @@ and requests are direct connections to it.

* 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
* 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.*
**⚠️ Restrictions ⚠️:**

* For security reasons the urls have been restricted and HTML rendering is disabled. For full demo, run this app locally or deploy to Heroku.
* 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**

Expand Down Expand Up @@ -100,20 +104,24 @@ https://url-to-pdf-api.herokuapp.com/api/render?url=http://google.com&waitFor=10

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

**Wait for an element macthing the selector `input` appears.**
**Wait for an element matching the selector `input` appears.**

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

**Render HTML sent in JSON body**

*NOTE: Demo app has disabled html rendering for security reasons.*

```bash
curl -o html.pdf -XPOST -d'{"html": "<body>test</body>"}' -H"content-type: application/json" https://url-to-pdf-api.herokuapp.com/api/render
curl -o html.pdf -XPOST -d'{"html": "<body>test</body>"}' -H"content-type: application/json" http://localhost:9000/api/render
```

**Render HTML sent as text body**

*NOTE: Demo app has disabled html rendering for security reasons.*

```bash
curl -o html.pdf -XPOST -d@page.html -H"content-type: text/html" https://url-to-pdf-api.herokuapp.com/api/render
curl -o html.pdf -XPOST -d@test/resources/large.html -H"content-type: text/html" http://localhost:9000/api/render
```

## API
Expand Down Expand Up @@ -153,8 +161,9 @@ The only required parameter is `url`.
Parameter | Type | Default | Description
----------|------|---------|------------
url | string | - | URL to render as PDF. (required)
output | string | pdf | Specify the output format. Possible values: `pdf` or `screenshot`.
output | string | pdf | Specify the output format. Possible values: `pdf` , `screenshot` or `html`.
emulateScreenMedia | boolean | `true` | Emulates `@media screen` when rendering the PDF.
enableGPU | boolean | `false` | When set, enables chrome GPU. For windows user, this will always return false. See https://developers.google.com/web/updates/2017/04/headless-chrome
ignoreHttpsErrors | boolean | `false` | Ignores possible HTTPS errors when navigating to a page.
scrollPage | boolean | `false` | Scroll page down before rendering to trigger lazy loading elements.
waitFor | number or string | - | Number in ms to wait before render or selector element to wait before render.
Expand All @@ -175,9 +184,7 @@ cookies[0][httpOnly] | boolean | - | Cookie httpOnly
cookies[0][secure] | boolean | - | Cookie secure
cookies[0][sameSite] | string | - | `Strict` or `Lax`
goto.timeout | number | `30000` | Maximum navigation time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout.
goto.waitUntil | string | `networkidle` | When to consider navigation succeeded. Options: `load`, `networkidle`. `load` = consider navigation to be finished when the load event is fired. `networkidle` = consider navigation to be finished when the network activity stays "idle" for at least `goto.networkIdleTimeout` ms.
goto.networkIdleInflight | number | `2` | Maximum amount of inflight requests which are considered "idle". Takes effect only with `goto.waitUntil`: 'networkidle' parameter.
goto.networkIdleTimeout | number | `2000` | A timeout to wait before completing navigation. Takes effect only with waitUntil: 'networkidle' parameter.
goto.waitUntil | string | `networkidle0` | When to consider navigation succeeded. Options: `load`, `domcontentloaded`, `networkidle0`, `networkidle2`. `load` - consider navigation to be finished when the load event is fired. `domcontentloaded` - consider navigation to be finished when the `DOMContentLoaded` event is fired. `networkidle0` - consider navigation to be finished when there are no more than 0 network connections for at least `500` ms. `networkidle2` - consider navigation to be finished when there are no more than 2 network connections for at least `500` ms.
pdf.scale | number | `1` | Scale of the webpage rendering.
pdf.printBackground | boolean | `false`| Print background graphics.
pdf.displayHeaderFooter | boolean | `false` | Display header and footer.
Expand All @@ -188,6 +195,7 @@ pdf.pageRanges | string | - | Paper ranges to print, e.g., '1-5, 8, 11-13'. Defa
pdf.format | string | `A4` | Paper format. If set, takes priority over width or height options.
pdf.width | string | - | Paper width, accepts values labeled with units.
pdf.height | string | - | Paper height, accepts values labeled with units.
pdf.fullPage | boolean | - | Create PDF in a single page
pdf.margin.top | string | - | Top margin, accepts values labeled with units.
pdf.margin.right | string | - | Right margin, accepts values labeled with units.
pdf.margin.bottom | string | - | Bottom margin, accepts values labeled with units.
Expand All @@ -200,6 +208,7 @@ screenshot.clip.x | number | - | Specifies x-coordinate of top-left corner of cl
screenshot.clip.y | number | - | Specifies y-coordinate of top-left corner of clipping region of the page.
screenshot.clip.width | number | - | Specifies width of clipping region of the page.
screenshot.clip.height | number | - | Specifies height of clipping region of the page.
screenshot.selector | string | - | Specifies css selector to clip the screenshot to.


**Example:**
Expand Down Expand Up @@ -264,11 +273,11 @@ The only required parameter is `url`.
**Example:**

```bash
curl -o google.pdf -XPOST -d'{"url": "http://google.com"}' -H"content-type: application/json" https://url-to-pdf-api.herokuapp.com/api/render
curl -o google.pdf -XPOST -d'{"url": "http://google.com"}' -H"content-type: application/json" http://localhost:9000/api/render
```

```bash
curl -o html.pdf -XPOST -d'{"html": "<body>test</body>"}' -H"content-type: application/json" https://url-to-pdf-api.herokuapp.com/api/render
curl -o html.pdf -XPOST -d'{"html": "<body>test</body>"}' -H"content-type: application/json" http://localhost:9000/api/render
```

### POST /api/render - (HTML)
Expand All @@ -283,7 +292,7 @@ paremeter.

```bash
curl -o receipt.html https://rawgit.com/wildbit/postmark-templates/master/templates_inlined/receipt.html
curl -o html.pdf -XPOST [email protected] -H"content-type: text/html" https://url-to-pdf-api.herokuapp.com/api/render?pdf.scale=1
curl -o html.pdf -XPOST [email protected] -H"content-type: text/html" http://localhost:9000/api/render?pdf.scale=1
```

## Development
Expand All @@ -309,9 +318,6 @@ First, clone the repository and cd into it.

* `cp .env.sample .env`
* Fill in the blanks in `.env`
* `source .env` or `bash .env`

Or use [autoenv](https://github.com/kennethreitz/autoenv).

* `npm install`
* `npm start` Start express server locally
Expand Down
Loading

0 comments on commit 5ee30a8

Please sign in to comment.