mileszs / wicked_pdf

PDF generator (from HTML) plugin for Ruby on Rails
http://www.mileszs.com/wicked-pdf-plugin
MIT License
3.53k stars 645 forks source link

turbo_frame_request_id: Could not render layout: undefined method `headers' for nil:NilClass #1084

Open miguelpeniche opened 10 months ago

miguelpeniche commented 10 months ago

Console error:

irb(main):002> enrollment.save_diploma
  User Load (3.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
/Users/miguel/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/turbo-rails-1.5.0/app/controllers/turbo/frames/frame_request.rb:36:in `turbo_frame_request_id': Could not render layout: undefined method `headers' for nil:NilClass (NoMethodError)

      request.headers["Turbo-Frame"]
             ^^^^^^^^
/Users/miguel/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/turbo-rails-1.5.0/app/controllers/turbo/frames/frame_request.rb:36:in `turbo_frame_request_id': undefined method `headers' for nil:NilClass (NoMethodError)

      request.headers["Turbo-Frame"]

Method:

def save_pdf template, locals, orientation: "portrait", page_size: "Letter"
    name = template.split("/").last
    pdf_file = WickedPdf.new.pdf_from_string(
      ActionController::Base.new.render_to_string(
        template: "pdfs/#{template}",
        locals: locals,
        encoding: "UTF-8"
      ),
      pdf: to_s,
      page_size: page_size,
      orientation: orientation,
      margin: {top: 0, bottom: 0, left: 0, right: 0}
    )
    tempfile = Tempfile.new([name, ".pdf"], Rails.root.join("tmp"))
    tempfile.binmode
    tempfile.write pdf_file
    tempfile.close
    if pdf_file.present?
      update "#{name}": tempfile
      tempfile.unlink
    end
  end

MacOS Sonoma 14.2 wicked_pdf (2.6.3) wkhtmltopdf 0.12.6 (with patched qt)

Showing the file is ok, but trying to save the file with Carrierwave return this error

patchfx commented 8 months ago

You maybe able to work around this by using the ApplicationController renderer. This worked in my example.

Change your render_to_string to this:

ApplicationController.renderer.render(
        template: "pdfs/#{template}",
        locals: locals,
        encoding: "UTF-8"
      )

Hope this helps!