msokk / electron-render-service

Microservice for rendering PDF/PNG/JPEG from HTML with Electron
MIT License
102 stars 31 forks source link
docker electron html2image html2pdf microservice pdf

:no_entry: [DEPRECATED]

This project is not actively developed anymore as there are better solutions out there that are headless. I would suggest using https://github.com/alvarcarto/url-to-pdf-api

electron-render-service

Greenkeeper badge Build Status Docker Hub npm

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

Alternatively an HTML payload can be POST-ed.

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?accessKey=secret&url=https%3A%2F%2Fgithub.com%2Fmsokk%2Felectron-render-service'

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

Docker Swarm needs extra configuration to work - --shm-size is not implemented use --mount type=tmpfs,dst=/dev/shm,tmpfs-size=134217728 instead and blank hostname -e HOSTNAME=.

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 libxss1 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?accessKey=secret&url=https%3A%2F%2Fgithub.com%2Fmsokk%2Felectron-render-service'

Endpoints

GET /pdf - Render PDF

Query params (About PDF params):

POST /pdf

Identical as above, omit url and provide HTML in request body.

GET /png|jpeg - Render PNG/JPEG

Query params:

POST /png|jpeg

Identical as above, omit url and provide HTML in request body.

GET /stats - Display render pool stats

Query params:

Environment variables

Required
Optional

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.