Skip to content

Commit 6a69b6e

Browse files
committed
Replace wicked_pdf with ferrum_pdf for PDF generation
1 parent a7218ff commit 6a69b6e

15 files changed

Lines changed: 125 additions & 71 deletions

File tree

Gemfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ gem "turbo_power"
111111
gem "turbo-rails"
112112

113113
gem 'combine_pdf'
114-
gem 'wicked_pdf', github: "openfoodfoundation/wicked_pdf", branch: "master"
115-
gem 'wkhtmltopdf-binary', source: 'https://rubygems.org' # due to https://github.com/gem-coop/gem.coop/issues/36
114+
gem 'ferrum_pdf'
116115

117116
gem 'immigrant'
118117
gem 'roo' # read spreadsheets

Gemfile.lock

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@ GIT
1616
select2-rails (3.4.9)
1717
thor (>= 0.14)
1818

19-
GIT
20-
remote: https://github.com/openfoodfoundation/wicked_pdf.git
21-
revision: bce498de547cdf00d037fdbec29fae844d69ee8e
22-
branch: master
23-
specs:
24-
wicked_pdf (2.8.1)
25-
activesupport
26-
ostruct
27-
2819
GIT
2920
remote: https://github.com/podia/stimulus_reflex_testing.git
3021
revision: abac2ee34de347c589795b4d1a8e83e0baafb201
@@ -337,6 +328,9 @@ GEM
337328
concurrent-ruby (~> 1.1)
338329
webrick (~> 1.7)
339330
websocket-driver (~> 0.7)
331+
ferrum_pdf (3.1.0)
332+
actionpack (>= 6.0.0)
333+
ferrum (~> 0.15)
340334
ffaker (2.25.0)
341335
ffi (1.17.3)
342336
flipper (1.4.1)
@@ -972,11 +966,6 @@ GEM
972966
nokogiri (~> 1.8)
973967
zeitwerk (2.7.5)
974968

975-
GEM
976-
remote: https://rubygems.org/
977-
specs:
978-
wkhtmltopdf-binary (0.12.6.10)
979-
980969
PLATFORMS
981970
ruby
982971

@@ -1020,6 +1009,7 @@ DEPENDENCIES
10201009
dotenv
10211010
factory_bot_rails (= 6.2.0)
10221011
faraday
1012+
ferrum_pdf
10231013
ffaker
10241014
flipper
10251015
flipper-active_record
@@ -1124,8 +1114,6 @@ DEPENDENCIES
11241114
web!
11251115
web-console
11261116
webmock
1127-
wicked_pdf!
1128-
wkhtmltopdf-binary!
11291117

11301118
CHECKSUMS
11311119
Ascii85 (2.0.1) sha256=15cb5d941808543cbb9e7e6aea3c8ec3877f154c3461e8b3673e97f7ecedbe5a
@@ -1231,6 +1219,7 @@ CHECKSUMS
12311219
faraday-follow_redirects (0.4.0) sha256=d3fa1118ab1350e24035a272b4cff64948643bb7182846db89acaf87abadd5d9
12321220
faraday-net_http (3.4.2) sha256=f147758260d3526939bf57ecf911682f94926a3666502e24c69992765875906c
12331221
ferrum (0.17.1) sha256=51d591120fc593e5a13b5d9d6474389f5145bb92a91e36eab147b5d096c8cbe7
1222+
ferrum_pdf (3.1.0) sha256=5e2e7adf70ffef0c78dd5f9431cc38061984926968f012725b885f453f634945
12341223
ffaker (2.25.0) sha256=e485c5adf8195aac55662875b7f515469bca46d77b60d0e7d08db6861bcbec40
12351224
ffi (1.17.3) sha256=0e9f39f7bb3934f77ad6feab49662be77e87eedcdeb2a3f5c0234c2938563d4c
12361225
flipper (1.4.1) sha256=05843240fb3093c7037549e34b046b636c765120f22e0a6268212daf44166e4d
@@ -1467,8 +1456,6 @@ CHECKSUMS
14671456
webrick (1.9.2) sha256=beb4a15fc474defed24a3bda4ffd88a490d517c9e4e6118c3edce59e45864131
14681457
websocket-driver (0.8.0) sha256=ed0dba4b943c22f17f9a734817e808bc84cdce6a7e22045f5315aa57676d4962
14691458
websocket-extensions (0.1.5) sha256=1c6ba63092cda343eb53fc657110c71c754c56484aad42578495227d717a8241
1470-
wicked_pdf (2.8.1)
1471-
wkhtmltopdf-binary (0.12.6.10) sha256=863060a4559253d20f9a73c76106bd972a443aec9fdd5013b45e1d1a9a44f352
14721459
xml-simple (1.1.8) sha256=3b53afc963c0b952892dcfc0d30de24bda2cef3c861b37e8c286c6c5e0ab97ba
14731460
xpath (3.2.0) sha256=6dfda79d91bb3b949b947ecc5919f042ef2f399b904013eb3ef6d20dd3a4082e
14741461
zeitwerk (2.7.5) sha256=d8da92128c09ea6ec62c949011b00ed4a20242b255293dd66bf41545398f73dd

app/controllers/spree/admin/orders_controller.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,13 @@ def print
112112
end
113113
end
114114

115-
render_with_wicked_pdf InvoiceRenderer.new.args(@order, spree_current_user)
115+
renderer = InvoiceRenderer.new
116+
send_data(
117+
renderer.render_to_string(@order, spree_current_user),
118+
filename: renderer.filename(@order),
119+
type: "application/pdf",
120+
disposition: "inline"
121+
)
116122
end
117123

118124
private

app/helpers/application_helper.rb

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,7 @@ def cache_key_with_locale(key, locale)
7878
end
7979

8080
def pdf_stylesheet_pack_tag(source)
81-
# With shakapacker dev server running, the wicked_pdf_stylesheet_pack_tag will produce a
82-
# relative path, because we don't have `config.action_controller.asset_host`. Relative path
83-
# can't be resolved by `wkhtmltopdf`. So we pass the wepacker dev server host and port to
84-
# the shakapacker helper, so it generates the correct url.
85-
# For more info: https://stackoverflow.com/questions/58490299/how-to-include-css-stylesheet-into-wicked-pdf/60541688#60541688
86-
if running_in_development?
87-
options = { media: "all",
88-
host: "#{Shakapacker.dev_server.host}:#{Shakapacker.dev_server.port}" }
89-
stylesheet_pack_tag(source, **options)
90-
else
91-
wicked_pdf_stylesheet_pack_tag(source)
92-
end
81+
# FerrumPdf uses the current page URL to resolve relative asset paths.
82+
stylesheet_pack_tag(source, media: "all")
9383
end
9484
end

app/services/invoice_renderer.rb

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,45 @@
11
# frozen_string_literal: true
22

33
class InvoiceRenderer
4-
def initialize(renderer = ApplicationController.new, user = nil)
4+
def initialize(renderer = ApplicationController.new, user = nil, pdf_renderer = PdfRenderer.new)
55
@renderer = renderer
66
@user = user
7+
@pdf_renderer = pdf_renderer
78
end
89

910
def render_to_string(order, user = @user)
1011
renderer.instance_variable_set(:@order, order)
11-
renderer.render_to_string_with_wicked_pdf(args(order, user))
12+
html = renderer.render_to_string(args(user))
13+
14+
pdf_renderer.render(html, display_url:)
1215
end
1316

14-
def args(order, user = @user)
17+
def args(user = @user)
1518
@user = user
1619
{
17-
pdf: "invoice-#{order.number}.pdf",
1820
template: invoice_template,
1921
formats: [:html],
20-
encoding: "UTF-8"
22+
encoding: "UTF-8",
23+
layout: false
2124
}
2225
end
2326

27+
def filename(order)
28+
"invoice-#{order.number}.pdf"
29+
end
30+
2431
private
2532

26-
attr_reader :renderer
33+
attr_reader :renderer, :pdf_renderer
34+
35+
def display_url
36+
return unless renderer.respond_to?(:request)
37+
38+
request = renderer.request
39+
request.original_url if request.respond_to?(:original_url)
40+
rescue StandardError
41+
nil
42+
end
2743

2844
def invoice_template
2945
if OpenFoodNetwork::FeatureToggle.enabled?(:invoices, @user)

app/services/pdf_renderer.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
class PdfRenderer
4+
def render(html, display_url: default_display_url)
5+
display_url ||= default_display_url
6+
7+
FerrumPdf.render_pdf(html: html_document(html), display_url:)
8+
end
9+
10+
private
11+
12+
def html_document(html)
13+
# If the string already starts like a full HTML document just return it.
14+
return html if html.match?(/\A\s*(<!doctype|<html)/i)
15+
16+
<<~HTML
17+
<!DOCTYPE html>
18+
<html>
19+
<head>
20+
<meta charset="utf-8">
21+
</head>
22+
<body>
23+
#{html}
24+
</body>
25+
</html>
26+
HTML
27+
end
28+
29+
def default_display_url
30+
options = Rails.application.routes.default_url_options.symbolize_keys
31+
host = options.fetch(:host)
32+
protocol = options[:protocol].presence || "http"
33+
port = options[:port]
34+
35+
"#{protocol}://#{host}#{":#{port}" if port}/"
36+
end
37+
end

app/views/layouts/pdf.html.haml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
%html
33
%head
44
%meta{charset: 'utf-8'}
5-
-# Using wicked_pdf_stylesheet_pack_tag with a new pdf pack was not working when using
6-
-# WickedPdf.new.pdf_from_string cause the css file reference was not absolute
7-
-# So I ended up putting inline css here, so it's included for sure in the PDF
85
:css
96
body {
107
width: 100%;

app/views/spree/admin/orders/invoice2.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
= t :tax_invoice
99
- if @order.distributor.display_invoice_logo? && @order.distributor.logo.variable?
1010
%td{ :align => "right", rowspan: 2 }
11-
= wicked_pdf_image_tag @order.distributor.logo_url(:small)
11+
= image_tag @order.distributor.logo_url(:small)
1212
%tr{ valign: "top" }
1313
%td{ :align => "left" }
1414
- if @order.distributor.business_address.blank?

app/views/spree/admin/orders/invoice4.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
= t :tax_invoice
99
- if @order.distributor.display_invoice_logo? && @order.distributor.logo_url
1010
%td{ :align => "right", rowspan: 2 }
11-
= wicked_pdf_image_tag @order.distributor.logo_url
11+
= image_tag @order.distributor.logo_url
1212
%tr{ valign: "top" }
1313
%td{ :align => "left" }
1414
- if @order.distributor.business_address.blank?

app/webpacker/css/mail/email.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,6 @@ ul {
413413
/*
414414
* Fix overlapping table header on second page of long invoices.
415415
* Problem description: https://github.com/openfoodfoundation/openfoodnetwork/issues/1738
416-
* Solution: https://github.com/wkhtmltopdf/wkhtmltopdf/issues/1770#issuecomment-73530576
417416
*/
418417
thead { display: table-header-group }
419418
tfoot { display: table-row-group }

0 commit comments

Comments
 (0)