@@ -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
116119end
0 commit comments