wicked_pdf: ActionView::MissingTemplate after upgrade to rails 7

Issue description

We’ve been having issues with the render since our upgrade to rails 7.0 reporting that the template is not found even though the render code inside the controller didn’t change. I can confirm that running the same version of wicked_pdf on rails 6.1 still works fine in production.

# ...
render pdf: @title, layout: 'pdf', orientation: 'Landscape', template: 'controller/action.html.erb',
           show_as_html: params.key?('debug'), margin: { top: 5, bottom: 5, left: 5, right: 5 },
           footer: { right: 'Page [page] / [topage]', font_size: 8 }
# ...

Expected or desired behavior

Renders the pdf like usual

System specifications

wicked_pdf gem version: master on github

wkhtmltopdf version: 0.12.6

platform/distribution and version: docker ruby 3.0.3 alpine image

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 5
  • Comments: 29 (6 by maintainers)

Most upvoted comments

Hello and thanks for this issue! I was having the same problem and was able to solve it using: template: 'controller/action', formats: [:html]

Note that is formats instead of format

This is what worked

respond_to do |format|
  format.html
  format.pdf do
    render pdf: "hello-filename", template: "hello/print_pdf", formats: [:html], disposition: :inline, layout: 'pdf'
  end
end

The filename
print_pdf.html.erb

Thanks to https://github.com/complygroup

@guemidiborhane I’m not sure why this is yet, and am looking into it, but I was able to get it to work by changing template: 'controller/action.html.erb', to template: 'controller/action'. This should be a valid work-around for now.

Please try this and let me know how it goes!

I found myself with a similar error after upgrading to Rails 7. This was my initial working code:

WickedPdf.new.pdf_from_string(
  ActionController::Base.new.render_to_string(
    template: "model/action",
    layout: "layouts/pdf.html.erb",
  )
)

Which returned this error after the upgrade: ActionView::MissingTemplate (Missing template layouts/pdf.html.erb with {:locale=>[:it], :formats=>[:pdf], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby]} And this is how I had to change:

WickedPdf.new.pdf_from_string(
  ActionController::Base.new.render_to_string(
    template: "model/action",
    layout: "pdf",
  )
)

Additionally I had to rename layouts/pdf.html.erb to layouts/pdf.pdf.erb, otherwise it was ignored. No formats required in my case, as per other users.

@hybridspyda Let’s schedule a quick screen-share for me to help you solve your issue. Shoot me an email and we’ll coordinate a time.

@unixmonkey

Thanks for the pointing me in the right direction. I was able to get it working by:

  • adding ‘formats: :html’
  • removing the ‘.html’ file extension from ‘layout’ and .template’.

For anyone else who is struggling with this:

WORKED IN RAILS 6.1 BUT NOT RAILS 7:

render pdf: filename,
               disposition: 'attachment',
               layout: 'application.html',
               template: 'examples/index.html',
               print_media_type: true

WORKS IN RAILS 7:

render pdf: filename,
               disposition: 'attachment',
               layout: 'application',
               template: 'examples/index',
               formats: :html,
               print_media_type: true

For us, the solution was to rename the view files. many were like action_pdf.haml we made them action.pdf.haml

@srbong Here’s what I’d do to troubleshoot your issue:

  1. Comment out the print_media_type line, and maybe the disposition one (if it makes it easier to refresh the page)
  2. Comment out the layout line, does it render (though it might be wrong without the layout)
  3. Take .html off the template string, does it render or give you a better error message
  4. Add formats: :html and try again
  5. Change to formats :pdf and try again
  6. Add back options one at a time
  7. Add a debugger above the render line and step step step to see what’s happening inside the gem (mismatch might be somewhere in make_and_send_pdf’s render(render_opts) (which is Rails’s render)

I hope this helps some. Let me know if you are still having trouble, maybe send me an email and we can screenshare pair on it.

@gabriel-andreoli Ok, so this error message tells me that your render call is causing Rails to look for: app/views/lotes/gerar_etiqueta.pdf.erb (the :formats=>[:pdf] part), which is overriding your request for format: :html.

So either you can either:

  • Rename your template to .pdf.erb instead of .html.erb, and remove the format: :html option (which is the incorrect option name anyway) or:
  • change format: :html to formats: [:html].

No guarantees, but I think this will fix things for you.

Had this same issue with Rails 7 in production. I ran bundle update wkhtmltopdf-binary so it’s now 0.12.6.6 (was 0.12.6.5) and it’s now working.

Another gotcha: We were using formats: "html", which did not work anymore with Rails 7.0. However, changing it to a symbol formats: :html fixed the issue 🙄

this does not work for footer and header under rails 7 unfortunately.

 footer: { template: 'invoices/footer', 
                formats: [:html], 
                layout: 'pdf' 
             },

Does not throw an error like before but just does not displays the footer. Does anyone have an idea why?

EDIT:

The trick was to change the setting call for the footer to

footer: {
    content: render_to_string(
        'invoices/footer',
        formats: [:html],
        layout: 'pdf'
   )
},

Hello!!!, I have this issue

image

How can I solve it?

Thanks @yshmarov we haven’t merged our rails7 branch yet to production, so we’ll keep an eye about this…

@spaci76 I recommend you get on the latest version of this gem.

I was facing the same error since I upgraded to Rails 7.

My view file was named export_pdf.pdf.haml and my controller code looked like template: 'proposals/export_pdf.pdf.haml'.

I had to rename my file to export_pdf.html.haml and changed controller code to template: 'proposals/export_pdf' No need to set formats option.

@hybridspyda Maybe it can help you 😃

I can confirm that using formats fixes this issue.

<%= render partial: "show", formats: :html %>

This issue can probably be closed since this is really caused by an underlying Rails change. My guess is that PDF generation is the only place I’ll see this in my app because it’s the only place where I want to render html when the request type is not “.html”.

@aliciapaz sorry for the typo, the issue is that it wasn’t required previously