Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate PDFs with chromium, remove wicked_pdf #453

Merged
merged 19 commits into from
Apr 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ RUN if [ "$BUILD_ENV" = "development" ]; then \
fi

RUN echo "@edge http://nl.alpinelinux.org/alpine/edge/main" >>/etc/apk/repositories \
&& echo "@edgetesting http://nl.alpinelinux.org/alpine/edge/testing" >>/etc/apk/repositories \
&& echo "@edgecommunity http://nl.alpinelinux.org/alpine/edge/community" >>/etc/apk/repositories \
&& apk upgrade --update-cache \
&& apk add \
build-base \
Expand All @@ -21,13 +21,13 @@ RUN echo "@edge http://nl.alpinelinux.org/alpine/edge/main" >>/etc/apk/repositor
postgresql-client \
imagemagick \
ghostscript \
xvfb \
wkhtmltopdf@edgetesting \
nodejs \
yarn \

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove xvfb and wkhtmltopdf@edgetesting from the Dockerfile?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep! Good idea.

# Needed for wkhtmltopdf
dbus \
chromium@edgecommunity \

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

# Set up crontab.
&& echo "*/15 * * * * su -s/bin/sh www-data -c \
Expand All @@ -36,7 +36,9 @@ RUN echo "@edge http://nl.alpinelinux.org/alpine/edge/main" >>/etc/apk/repositor
&& echo "*/15 * * * * root su -s/bin/sh www-data -c \
'cd /opt/trainers-hub && bundle exec rake glossary:update' >>/proc/1/fd/1 2>&1" >>/etc/crontab

ENV DISPLAY=:99
ENV CHROME_BIN=/usr/bin/chromium-browser
ENV CHROME_PATH=/usr/lib/chromium/

COPY Gemfile* ./
RUN bundle install

Expand Down
3 changes: 0 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ gem "httparty"
# Slugging/permalinks
gem "friendly_id"

# HTML to PDF
gem "wicked_pdf"

# Feature flipping
gem "flipper", github: "jnunemaker/flipper"

Expand Down
2 changes: 0 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,6 @@ GEM
websocket-driver (0.6.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.2)
wicked_pdf (1.1.0)
xpath (2.1.0)
nokogiri (~> 1.3)

Expand Down Expand Up @@ -424,7 +423,6 @@ DEPENDENCIES
tzinfo-data
uglifier (>= 1.3.0)
web-console (>= 3.3.0)
wicked_pdf

BUNDLED WITH
1.16.1
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ table {
width: 100%;
}


ul, ol {
margin-left: 1.6rem;
}
Expand All @@ -196,6 +195,25 @@ h1 {
page-break-before: always;
}

@page {
size: auto;
margin: 0.6in 1in 1.5in 1in;
}

#header {
position: absolute;
top: 0;
left: 0;
right: 0;
}

#header img {
width: 100%;
}

#main-content {
margin-top: 65px;
}
Copy link
Collaborator

@vbrown608 vbrown608 Apr 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some styles aren't getting picked up when I test this locally and the image doesn't seem to be coming through. The font size (or maybe weight?) also seems a bit heavy. I'm not sure if this is unique to my setup, maybe we can check in about it on Monday.

pdf

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed a change to inline the stylesheet and use a data URI for the header image, which should be more reliable.


.toc ol {
margin-left: 0;
Expand Down
12 changes: 12 additions & 0 deletions app/helpers/pdf_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,16 @@ module PdfHelper
def page_break
content_tag(:div, "", class: "page-break")
end

def asset_data_uri(path)
asset = Rails.application.assets.find_asset(path)
data = Base64.encode64(asset.source)
"data:#{asset.content_type};base64,#{Rack::Utils.escape(data)}"
end

def pdf_style_sheet
content_tag(:style, type: "text/css") do
Rails.application.assets.find_asset("pdf.scss").to_s.html_safe # rubocop:disable Rails/OutputSafety
end
end
end
10 changes: 5 additions & 5 deletions app/views/layouts/pdf.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<title><%= page_title %></title>

<style type="text/css">
<%= File.read(Rails.root.join("app/assets/stylesheets/pdf.css")).html_safe %>
</style>

<%= pdf_style_sheet %>
</head>

<body class="section-<%= controller_name %> page-<%= action_name %>">
<div id="header">
<img src="<%= asset_data_uri("print-header.png") %>" />
</div>

<div id="main-content">
<%= yield %>
</div>
Expand Down
31 changes: 31 additions & 0 deletions bin/html-pdf-chrome
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env node

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

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

var htmlPdf = require('../node_modules/html-pdf-chrome');

if (!argv['html'] || !argv['pdf']) {
console.log('Usage: html-pdf-chrome --html input.html --pdf output.pdf');
process.exit(1);
}

var html = fs.readFileSync(argv['html']).toString();

var options = {
chromePath: 'chromium-browser',
chromeFlags: [
'--disable-gpu',
'--no-sandbox',
'--headless',
'--hide-scrollbars',
]
};

htmlPdf.create(html, options).then(function(pdf) {
pdf.toFile(argv['pdf']);
});
61 changes: 0 additions & 61 deletions config/initializers/wicked_pdf.rb

This file was deleted.

3 changes: 0 additions & 3 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,4 @@ if [ "$ROLE" = "web" ]; then
cp -ru public/* nginx_static
fi

rm -rf "/tmp/.X$DISPLAY-lock"
/usr/bin/Xvfb "$DISPLAY" -ac &

exec "$@"
57 changes: 34 additions & 23 deletions lib/pdf_template.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
require "shellwords"

class PdfTemplate
attr_reader :controller_options, :wicked_options
attr_reader :controller_options

def initialize(options)
@controller_options = {
template: options.fetch(:template),
layout: "layouts/pdf.html.erb"
}

@wicked_options = options.slice!(:template)
wicked_options[:pdf] = wicked_options.delete(:name)
wicked_options.reverse_merge!(
margin: {
top: "0.6in",
bottom: "1in",
left: "1in",
right: "0.6in"
}
)
end

def render(locals)
def render(locals = {})
controller = ApplicationController.new

locals.each do |name, value|
Expand All @@ -28,15 +19,35 @@ def render(locals)

doc = rebase_urls(controller.render_to_string(controller_options))

pdf = WickedPdf.new.pdf_from_string(doc, wicked_options)
input = Tempfile.new(["pdf", ".html"])
input.binmode
input.write(doc)
input.flush
input.rewind

print_pdf(input.path)
end

private

tmp = Tempfile.new(["pdf", ".pdf"])
tmp.binmode
tmp.write(pdf)
tmp.flush
tmp.rewind
def print_pdf(input)
output = Tempfile.new(["pdf", ".pdf"])

command = ["bin/html-pdf-chrome",
"--html=#{input}",
"--pdf=#{output.path}"]

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

begin
pid = Process.spawn(*command)
Timeout.timeout(3){ Process.waitpid(pid) }
rescue Timeout::Error => e
Process.kill("TERM", pid)
raise e
end

tmp
output
end

def rebase_urls(html)
Expand All @@ -46,13 +57,13 @@ def rebase_urls(html)
url_base = [ENV["SERVER_PROTOCOL"], ENV["SERVER_HOST"]].join("://")
url_base = [url_base, ENV["SERVER_PORT"]].join(":") if ENV["SERVER_PORT"]

doc.css("a, img").each do |a|
if a["href"] && a["href"] !~ /^https?:\/\//
doc.css("a, img, link, script").each do |a|
if a["href"] && a["href"] !~ /^(https?:\/\/|data:)/
path = a["href"].sub(/^\//, "")
a["href"] = "#{url_base}/#{path}"
end

if a["src"] && a["src"] !~ /^https?:\/\//
if a["src"] && a["src"] !~ /^(https?:\/\/|data:)/
path = a["src"].sub(/^\//, "")
a["src"] = "#{url_base}/#{path}"
end
Expand Down
1 change: 0 additions & 1 deletion lib/tasks/pdf.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace :pdfs do
task recreate: :environment do
Lesson.published.find_each do |lesson|
UpdateLessonPdf.perform_now(lesson.id)
sleep 0.5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

end
end
end
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"name": "trainers-hub",
"private": true,
"dependencies": {
"html-pdf-chrome": "^0.5.0",
"jquery": "^3.2.1",
"minimist": "^1.2.0",
"react-sortable-hoc": "^0.6.8"
},
"devDependencies": {
Expand Down
7 changes: 4 additions & 3 deletions spec/jobs/update_lesson_pdf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

describe "#perform" do
it "should create a PDF" do
expect(lesson.reload.pdf).not_to be_present
pdf = double()
expect_any_instance_of(PdfTemplate).to receive(:render){ pdf }

expect(Lesson).to receive(:find){ lesson }
expect(lesson).to receive(:update!).with(pdf: pdf)
UpdateLessonPdf.new.perform(lesson.id)

expect(lesson.reload.pdf).to be_present
end
end
end
Loading