Skip to content

Commit

Permalink
s/electron-pdf/grover/
Browse files Browse the repository at this point in the history
  • Loading branch information
TomNaessens committed Mar 18, 2024
1 parent a33410f commit 12681b1
Show file tree
Hide file tree
Showing 11 changed files with 600 additions and 1,298 deletions.
6 changes: 0 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ FROM ruby:3.3.0

ENV RAILS_ENV=production

RUN adduser --system --group factuur

WORKDIR /app

# Install Yarn
Expand All @@ -24,8 +22,4 @@ RUN yarn install

RUN bundle exec rails assets:precompile

RUN chown -R factuur:factuur /app

USER factuur

CMD bundle exec rails s -b 0.0.0.0
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ gem 'font-awesome-rails'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false

# PDF goodies
gem 'grover'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.12.2)
combine_pdf (1.0.26)
matrix
ruby-rc4 (>= 0.1.5)
concurrent-ruby (1.2.3)
crass (1.0.6)
cucumber (8.0.0)
Expand Down Expand Up @@ -166,6 +169,9 @@ GEM
formatador (1.1.0)
globalid (1.2.1)
activesupport (>= 6.1)
grover (1.1.6)
combine_pdf (~> 1.0)
nokogiri (~> 1.0)
guard (2.18.1)
formatador (>= 0.2.4)
listen (>= 2.7, < 4.0)
Expand Down Expand Up @@ -204,6 +210,7 @@ GEM
net-pop
net-smtp
marcel (1.0.4)
matrix (0.4.2)
meta_request (0.7.4)
rack-contrib (>= 1.1, < 3)
railties (>= 3.0.0, < 7.1)
Expand Down Expand Up @@ -308,6 +315,7 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
ruby-rc4 (0.1.5)
rubyzip (2.3.2)
sassc (2.4.0)
ffi (~> 1.9)
Expand Down Expand Up @@ -397,6 +405,7 @@ DEPENDENCIES
ed25519
faker
font-awesome-rails
grover
guard
guard-livereload (~> 2.5)
jbuilder (~> 2.5)
Expand Down
49 changes: 9 additions & 40 deletions app/models/note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
# index_notes_on_deleted_at (deleted_at)
#

require 'open3'
require 'tmpdir'

class Note < ActiveRecord::Base
acts_as_paranoid

Expand All @@ -42,43 +39,15 @@ class Note < ActiveRecord::Base
belongs_to :contact

def generate_pdf
res = nil
Dir.mktmpdir do |dir|
# TODO: Put this in a config
# command = 'electron-pdf'
browser_config = '{ "webPreferences": { "sandbox" : false } }'
exe = "node_modules/.bin/electron-pdf --browserConfig '#{browser_config}'"

exe.prepend('xvfb-run -n 9 ') if Rails.env.production?
input_file = "#{dir}/input.html"
output_file = "#{dir}/output.pdf"

s = ApplicationController.render(
'notes/note_pdf',
layout: 'paper',
locals: {
note: self
}
)
File.write(input_file, s)

logger.info '======'
command = "#{exe} #{input_file} #{output_file}"
logger.info "Running '#{command}'"
Open3.popen3(command) do |_, out, err, wait_thr|
logger.info "STDOUT: #{out.read}"
logger.info "STDERR: #{err.read}"

if wait_thr.value == 0
res = File.binread(output_file)
else
errors.add :generated_pdf, 'Error creating PDF. Check logs.'
end
end
logger.info '======'
end

res
rendered_view = ApplicationController.render(
'notes/note_pdf',
layout: 'paper',
locals: {
note: self
}
)

Grover.new(rendered_view, format: 'A4').to_pdf
end

def self.next_note_number
Expand Down
2 changes: 1 addition & 1 deletion config/deploy/production.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
server '12urenloop.be', user: 'root', roles: %w{web app db}, ssh_options: {
server 'saruman', user: 'root', roles: %w{web app db}, ssh_options: {
forward_agent: true,
auth_methods: ['publickey'],
port: 2222
Expand Down
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,6 @@

# Uncomment if you wish to allow Action Cable access from any origin.
# config.action_cable.disable_request_forgery_protection = true

config.action_mailer.default_url_options = {host: 'localhost:3000'}
end
2 changes: 2 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,6 @@
# config.active_record.database_selector = { delay: 2.seconds }
# config.active_record.database_resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver
# config.active_record.database_resolver_context = ActiveRecord::Middleware::DatabaseSelector::Resolver::Session

config.action_mailer.default_url_options = { host: 'factuur.12urenloop.be' }
end
2 changes: 2 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@

# Annotate rendered view with file names.
# config.action_view.annotate_rendered_view_with_filenames = true

config.action_mailer.default_url_options = {host: 'localhost:3000'}
end
7 changes: 7 additions & 0 deletions config/initializers/grover.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Grover.configure do |config|
config.options = {
# Groves runs puppeteer under the hood which runs in a separate headless Chrome
# It thus needs an absolute URL as it won't be able to resolve anything relatively defined
display_url: "#{Rails.env.development? ? "http://" : "https://"}#{Rails.configuration.action_mailer.default_url_options[:host]}",
}
end
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"dependencies": {
"electron-pdf": "^15.0.0"
"puppeteer": "^20.2.0"
}
}
Loading

0 comments on commit 12681b1

Please sign in to comment.