forked from alvarcarto/url-to-pdf-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
14 changed files
with
378 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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** | ||
|
||
|
@@ -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 | ||
|
@@ -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. | ||
|
@@ -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. | ||
|
@@ -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. | ||
|
@@ -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:** | ||
|
@@ -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) | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
Oops, something went wrong.