Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layout not used while using render_to_string #249

Open
atif opened this issue Aug 3, 2024 · 3 comments
Open

Layout not used while using render_to_string #249

atif opened this issue Aug 3, 2024 · 3 comments

Comments

@atif
Copy link

atif commented Aug 3, 2024

I'm using grover to generate a pdf file (using tailwindcss). Here's the code snippet

html = render_to_string(template: 'controller_name/method_name', layout: 'pdf')
pdf = Grover.new(html).to_pdf
send_data pdf, filename: 'report.pdf', type: 'application/pdf', disposition: 'inline'

I've the application/tailwindcss in pdf layout. But rendering above only renders the contents of method template without using layout. Can you please guide how to use layout

@abrom
Copy link
Contributor

abrom commented Aug 3, 2024

There is a section in the readme describing this. See https://github.com/Studiosity/grover?tab=readme-ov-file#relative-paths

Your HTML is very likely going to be using relative paths. Consider how the browser driven by grover is going to know how to resolve those relative paths 😉

Sure fire way to test this.. dump the rendered HTML to a file.. open the file in a browser. Do you get the same result? (hint.. yep!)

@atif
Copy link
Author

atif commented Aug 3, 2024

Thanks for your reply, I did output the rendered html to file and it just contains the contents of the method.html.erb and doesn't include layout contents. I'm a noob and didn't understand how to fix it.

Also, one more thing to ask here, I setup grover by creating an initializer file with some options. I wasn't able to make it run using Rails 7 by adding the middleware part in application.rb (I get UnknownFormat exception) when I add the following to application.rb

# in application.rb
require 'grover'
config.middleware.use Grover::Middleware

Exception:
ActionController::UnknownFormat in XYZController#show

My Request:
http://localhost:3000/xyz/1.pdf

It works without the middleware part though but doesn't render layout while generating pdf. Can you please guide if I am missing something on my side?

@atif
Copy link
Author

atif commented Aug 3, 2024

Using html = Controller.render(template: 'xyz/show', layout: 'pdf') fixed my problem and allowed me to include layout in pdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants