Skip to content

Commit 46bfc49

Browse files
authored
Make PDF generation Url based instead of htmlbased (#1212)
* intial commit * simplyfy calls * make pdf not have a max length * hide payment status from client * fix lint * coderabbit suggestion
1 parent 2109237 commit 46bfc49

5 files changed

Lines changed: 34 additions & 78 deletions

File tree

app/controllers/invoices_controller.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ def show
2020
# Authorize for authenticated access (integer ID), skip for token-based access
2121
authorize @invoice, :show? unless token_based_access
2222

23+
# Hide navbar when generating PDF
24+
@show_navigationbar = params[:pdf].blank?
25+
@show_extras = params[:pdf].blank?
26+
2327
respond_to do |format|
2428
format.html
2529
format.pdf { render_invoice_pdf }
@@ -93,16 +97,14 @@ def permitted_attributes
9397
params.require(:invoice).permit(%i[user_id activity_id name_override email_override rows], rows_attributes: %i[name amount price])
9498
end
9599

96-
def render_invoice_pdf # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
100+
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
101+
def render_invoice_pdf
97102
token_based_access = !integer_id?(params[:id])
98103
authorize @invoice, :download? unless token_based_access
99104

100-
html = render_to_string(
101-
template: 'invoices/show',
102-
formats: [:html],
103-
layout: 'pdf'
104-
)
105-
pdf = Grover.new(html).to_pdf
105+
# Use token-based URL for unauthenticated Grover access
106+
url = invoice_url(@invoice.token, pdf: true, only_path: false)
107+
pdf = Grover.new(url).to_pdf
106108
send_data pdf, filename: "Factuur-#{@invoice.human_id}.pdf", type: 'application/pdf', disposition: 'attachment'
107109
rescue StandardError => e
108110
Rails.logger.error "Failed to generate PDF for invoice #{@invoice.id}: #{e.message}"
@@ -113,4 +115,5 @@ def render_invoice_pdf # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
113115
redirect_to invoice_path(@invoice)
114116
end
115117
end
118+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
116119
end

app/mailers/invoice_mailer.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@ def invoice_mail(invoice) # rubocop:disable Metrics/AbcSize, Metrics/MethodLengt
1010
end
1111

1212
begin
13-
html = render_to_string(
14-
template: 'invoices/show',
15-
formats: [:html],
16-
layout: 'pdf'
17-
)
18-
pdf = Grover.new(html).to_pdf
13+
# Use token-based URL for unauthenticated Grover access
14+
url = url_for(controller: 'invoices', action: 'show', id: invoice.token, pdf: true, only_path: false)
15+
pdf = Grover.new(url).to_pdf
1916
attachments["#{invoice.human_id}.pdf"] = pdf
2017
rescue StandardError => e
2118
Rails.logger.error "Failed to generate PDF attachment for invoice #{invoice.id}: #{e.message}"

app/views/invoices/show.html.erb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<%= Rails.application.config.x.company_kvk %>
4949
</td>
5050
</tr>
51+
<% if current_user&.treasurer? %>
5152
<tr>
5253
<th scope="row">
5354
Status
@@ -60,10 +61,11 @@
6061
<% end %>
6162
</td>
6263
</tr>
64+
<% end %>
6365
</tbody>
6466
</table>
6567

66-
<% unless @invoice.paid? %>
68+
<% if !@invoice.paid? && Rails.application.config.x.mollie_api_key.present? %>
6769
<%= link_to pay_invoice_url @invoice.token do %>
6870
<button class="col-sm-3 offset-sm-9 btn btn-primary">Betalen</button>
6971
<% end %>

app/views/layouts/pdf.html.erb

Lines changed: 0 additions & 37 deletions
This file was deleted.

config/initializers/grover.rb

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
1-
# Grover Global Configuration
2-
#
3-
# Use this to set up shared configuration options for your entire application.
4-
# Any of the configuration options shown here can also be applied to single
5-
# models by passing arguments to the Grover.new call.
6-
#
7-
# To learn more, check out the README:
8-
# https://github.com/Studiosity/grover
9-
101
Grover.configure do |config|
11-
options = {
12-
format: 'A4',
2+
config.options = {
3+
viewport: {
4+
width: 794, # A4 width in pixels at 96 DPI (210mm)
5+
height: 1123 # Starting height, will expand as needed
6+
},
7+
emulate_media: 'screen',
138
print_background: true,
14-
prefer_css_page_size: false,
15-
display_url: "https://#{Rails.application.config.x.sofia_host}"
9+
executable_path: Rails.env.development? ? nil : '/usr/bin/chromium',
10+
launch_args: if Rails.env.development?
11+
[]
12+
else
13+
[
14+
'--no-sandbox',
15+
'--disable-setuid-sandbox',
16+
'--disable-dev-shm-usage',
17+
'--disable-gpu',
18+
'--disable-software-rasterizer'
19+
]
20+
end
1621
}
17-
18-
unless Rails.env.development?
19-
options[:executable_path] = '/usr/bin/chromium'
20-
options[:launch_args] = [
21-
'--no-sandbox',
22-
'--disable-setuid-sandbox',
23-
'--disable-dev-shm-usage',
24-
'--disable-gpu',
25-
'--disable-software-rasterizer',
26-
'--hide-scrollbars'
27-
]
28-
end
29-
30-
config.options = options
3122
end

0 commit comments

Comments
 (0)