Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Support pdf generation from persistent chrome process
  • Loading branch information
wioux committed Apr 20, 2018
1 parent b1bb4d3 commit 775d5fb
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .env.example
Expand Up @@ -21,6 +21,10 @@ POSTGRES_PASSWORD=mysecretpassword

REDIS_URI=redis://redis/0/cache

# Set if you're using the chrome container
# CHROME_HOST=chrome
# CHROME_PORT=9222

SENTRY_DSN=
SENTRY_PUBLIC_DSN=

Expand Down
5 changes: 0 additions & 5 deletions Dockerfile
Expand Up @@ -24,11 +24,6 @@ RUN echo "@edge http://nl.alpinelinux.org/alpine/edge/main" >>/etc/apk/repositor
nodejs \
yarn \

chromium@edgecommunity \

# Needed by html-pdf-chrome dependency
grep@edge \

# Set up crontab.
&& echo "*/15 * * * * su -s/bin/sh www-data -c \
'cd /opt/trainers-hub && bundle exec rake blog:update' >>/proc/1/fd/1 2>&1" >>/etc/crontabs/root \
Expand Down
32 changes: 28 additions & 4 deletions bin/html-pdf-chrome
Expand Up @@ -2,9 +2,10 @@

var process = require('process');
var fs = require('fs');
var dns = require('dns');

var argv = require('minimist')(process.argv.slice(2), {
string: ['html', 'pdf']
string: ['html', 'pdf', 'chrome-host', 'chrome-port']
});

var htmlPdf = require('../node_modules/html-pdf-chrome');
Expand All @@ -26,6 +27,29 @@ var options = {
]
};

htmlPdf.create(html, options).then(function(pdf) {
pdf.toFile(argv['pdf']);
});
if (argv['chrome-port']) {
options.port = argv['chrome-port'];
}

if (argv['chrome-host']) {
dns.lookup(argv['chrome-host'], function(err, ip) {
if (err) {
console.error(err);
process.exit(1);
}

options.host = ip;
run();
});
} else {
run();
}

function run() {
htmlPdf.create(html, options).then(function(pdf) {
pdf.toFile(argv['pdf']);
}).catch(function(e) {
console.error(e);
process.exit(1);
});
}
7 changes: 7 additions & 0 deletions docker-compose.yml.example
Expand Up @@ -76,3 +76,10 @@ services:
# env_file: .env
#
# command: bundle exec rake jobs:work

# Chrome browser for production
# chrome:
# image: justinribeiro/chrome-headless:latest
# command: --headless --disable-gpu --no-sandbox \
# --remote-debugging-address=0.0.0.0 \
# --remote-debugging-port=9222
12 changes: 12 additions & 0 deletions lib/pdf_template.rb
@@ -1,6 +1,8 @@
require "shellwords"

class PdfTemplate
Error = Class.new(Exception)

attr_reader :controller_options

def initialize(options)
Expand Down Expand Up @@ -37,6 +39,14 @@ def print_pdf(input)
"--html=#{input}",
"--pdf=#{output.path}"]

if ENV["CHROME_HOST"]
command << "--chrome-host=#{ENV["CHROME_HOST"]}"
end

if ENV["CHROME_PORT"]
command << "--chrome-port=#{ENV["CHROME_PORT"]}"
end

Rails.logger.info(Shellwords.shelljoin(command))

begin
Expand All @@ -47,6 +57,8 @@ def print_pdf(input)
raise e
end

raise Error, "html-pdf-chrome command failed" unless $?.success?

output
end

Expand Down

0 comments on commit 775d5fb

Please sign in to comment.