Public | Automated Build

Last pushed: 17 days ago
Short Description
Microservice for rendering PDFs from HTML with Electron
Full Description

electron-render-service




Simple PDF/PNG/JPEG render service, accepts webpage URL and returns the resource.

Docker usage

Based on official Debian Jessie image, uses latest electron.

  1. docker run -t -e RENDERER_ACCESS_KEY=secret -p 3000:3000 msokk/electron-render-service
  2. wget -o out.pdf http://<node_address>:3000/pdf?url=https://github.com/msokk/electron-render-service&accessKey=secret

NB: Set bigger shared memory size --shm-size=Xm (default: 64m) if dealing with very heavy pages.

Note: Add --security-opt seccomp:unconfined to supress libudev: udev_has_devtmpfs: name_to_handle_at on /dev: Operation not permitted warning.

Installation on Debian with Node.js

# Enable contrib packages
sed -i 's/main/main contrib/g' /etc/apt/sources.list

# Install packages needed for runtime
apt-get update && apt-get install -y xvfb libgtk2.0-0 ttf-mscorefonts-installer libnotify4 libgconf2-4 libnss3 dbus-x11

# Install from NPM
npm install -g electron-render-service

# Run in virtual framebuffer
RENDERER_ACCESS_KEY=secret xvfb-run --server-args="-screen 0 1024x768x24" electron-render-service

wget -o out.pdf http://localhost:3000/pdf?url=https://github.com/msokk/electron-render-service&accessKey=secret

Endpoints

GET /pdf - Render PDF

Query params (About PDF params):

  • accessKey - Authentication key.
  • url - Full URL to fetch.
  • pageSize - Specify page size of the generated PDF. (default: A4)
  • marginsType - Specify the type of margins to use (default: 0)
  • printBackground - Whether to print CSS backgrounds. (default: true)
  • landscape - true for landscape, false for portrait. (default: false)
  • removePrintMedia - Removes any <link media="print"> stylesheets on page before render. (default: false)
  • delay - Specify how long to wait before generating the PDF (default: 0)
  • waitForText - Specify a specific string of text to find before generating the PDF (default: false)

GET /png|jpeg - Render PNG/JPEG

Query params:

  • accessKey - Authentication key.
  • url - Full URL to fetch.
  • quality - JPEG quality. (default: 80)
  • delay - Specify how long to wait before generating the image (default: 0)
  • waitForText - Specify a specific string of text to find before generating the image (default: false)
  • browserWidth - Browser window width (default: rect.width || env.WINDOW_WIDTH, max: 3000)
  • browserHeight - Browser window height (default: rect.height || env.WINDOW_HEIGHT, max: 3000)
  • Clipping rectangle (optional, but all 4 integers need to be set)
    • clippingRect[x]
    • clippingRect[y]
    • clippingRect[width]
    • clippingRect[height]

GET /stats - Display render pool stats

Query params:

  • accessKey - Generic authentication key is required.

Environment variables

Required
  • RENDERER_ACCESS_KEY or RENDERER_ACCESS_KEY_<suffix> - Secret key for limiting access. Suffixed keys are used as labels in access log for debugging usage.
Optional
  • CONCURRENCY - Number of browser windows to run in parallel (default: 1)
  • TIMEOUT - Number of seconds before request timeouts (default: 30)
  • WINDOW_WIDTH - Default window width (default: 1024)
  • WINDOW_HEIGHT - Default window height (default: 768)
  • HOSTNAME - Hostname to accept Express connections on (default: 0.0.0.0)
  • PORT - (default: 3000)

Rendering issues in latest Electron (Chrome 49)

  • box-shadow is black in PDF - To keep the box shadow, add -webkit-filter: blur(0); rule next to it. Note that this rasterizes the whole layer, making large areas noticeably blurry. Or just hide the box shadow.
  • border-radius creates double width borders without rounded corners

Delayed Rendering

Not all content is loaded once the DOM is loaded, some data can take time because calls are being made via websockets and other methods. You can delay the rendering by either providing a delay value in the query string or you can provide waitForText in the query string.

If you specify waitForText the service will continually scan the loaded URL until the overall timeout is reached. If the text passed to the variable waitForText is found before the timeout, the PDF/image will generate and return.

Docker Pull Command
Owner
msokk
Source Repository

Comments (0)