This repository wiil be deprecated.
Please use hc-pdf-server instead.
Since I hadn't been able to maintain it for a while, I completely rewrote it in TypeScript and changed the library and other things accordingly.
Simple and fast PDF rendering server. Using Headless Chrome & Express & Puppeteer.
GET URL or POST HTML returns PDF binary.
You can run this on Docker container, Kubernetes(GKE), or Google App Engine(beta).
https://developers.google.com/web/updates/2017/04/headless-chrome
https://github.com/GoogleChrome/puppeteer
Instead of Docker container, You can also run this on Google App Engine. Please use app.yaml and edit it for your purpose.
gcloud app deploy --project your-project
More detail: https://cloud.google.com/appengine/docs/standard/nodejs/using-headless-chrome-with-puppeteer
Since this product is supposed to be used within local network (like Kubernetes, GKE), error control and security measures are minimum, please accept only reliable requests. It does not assume direct disclosure to the outside.
git clone this repository.
Please specify whether to use Chromium bundled with Puppeteer (recommend) or Chrome, and change locale setting etc. as necessary.
If you convert pages in Japanese, Chinese or languages other than English, you will need to install each font files. Also, you can use WEB fonts, but since it takes a long time for requesting and downloading them, we recommend that install the font files in the server.
cp AnyFonts.ttf ./fonts/
sudo docker build -t hcep-pdf-server:latest .
Below example, run with 8000 port.
sudo docker run -it --rm \
-p 8000:8000 \
--name hcep-pdf-server \
hcep-pdf-server:latest
curl "http://localhost:8000?url=http://example.com" -o hcep-pdf-get.pdf
curl -sS http://localhost:8000 -v -d html=hcep-pdf-ok -o hcep-pdf-post.pdf
Please note that because the page does not have a URL, when sending html in POST method, you can not use a relative path.
So, you need to include external files with domain.
Bad, not working
<link href="/static/style.css" rel="stylesheet">
OK
<link href="http://example.com/static/style.css" rel="stylesheet">
Execute mocha in the container run with the below command.
% sudo docker exec -e DEBUG="" varuna-hcep-pdf-server mocha
SERVER_URL: http://localhost:8000
TAREGT_URL: https://www.google.com
HTML_TEST_STRINGS: <html>ok</html>
requests routes
env: development
Listening on: 8000
GET /hc 200 0.987 ms - -
✓ Health Check GET /hc
GET / 400 0.188 ms - -
✓ GET / with no url
GET /?url=https://www.google.com 200 798.680 ms - 76794
✓ GET / with url https://www.google.com (801ms)
POST / 200 104.470 ms - 8374
✓ POST / html=<html>ok</html> (106ms)
GET /screenshot?url=https://www.google.com 200 354.910 ms - 27974
✓ GET /screenshot with url https://www.google.com (356ms)
POST /screenshot 200 1911.266 ms - 3732
✓ POST /screenshot html=<html>ok</html> (1912ms)
default pdf options
✓ empty return default
✓ not exists return default
✓ A4 in default presets
✓ A3 in default presets
myPdfOptionPresets set
✓ format in A4ShowPageNumberFooter is matched
✓ displayHeaderFooter in A4ShowPageNumberFooter is matched
✓ headerTemplate in A4ShowPageNumberFooter is matched
✓ footerTemplate in A4ShowPageNumberFooter is matched
14 passing (3s)
testing express-app complete! process.exit()
Whether to use chromium attached to puppeteer. If you want to run this on Google App Engine, you must set it to "true".
default: false (use installed Chrome by Dockerfile)
The path of installed google-chrome binary. If HCEP_USE_CHROMIUM is true, this value is ignored
default: /usr/bin/google-chrome
Timeout milliseconds of the browser's Page default: 10000
Listen Port by the express app
default: 8000
Timeout milliseconds of the express app
default: 30000
default: 10MB
If you want to extend the PDF option presets yourself, create a file with reference to "app/my-pdf-option-presets.js.sample" and specify the file path in this variable.
default: none
example: "./my-pdf-options"
app/my-pdf-options.js
module.exports.myPdfOptionPresets = {
'A4ShowPageNumberFooter': {
format: 'A4',
displayHeaderFooter: true,
headerTemplate: '<span></span>',
footerTemplate: `<div style="font-size:7pt;text-align:center;padding-bottom:5mm;width:100%;">
<span class="pageNumber"></span> / <span class="totalPages"></span>
</div>`
}
}
You can make your PDF options. Read the puppeteer API's docs. https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions
default: 18mm
default: A4
default: 'http://localhost:8000'
default: 'https://www.google.com'
uyamazak:blog
This project has been maintained under the support of yagish履歴書 and is actually used for PDF generation.