Studiosity / grover

A Ruby gem to transform HTML into PDFs, PNGs or JPEGs using Google Puppeteer/Chromium
MIT License
932 stars 107 forks source link

Layout not used while using render_to_string #249

Open atif opened 3 months ago

atif commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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