4

I want to use GROVER to export same ERB/HTML pages from my application as PDF. It works, but the generated PDF seems to be missing the styles and formatting, no CSS seems to be processed.

Here my code within the controller:

 html_string = render_to_string(
  {
     template: 'users/show.html.erb',
     locals: { id: params[:id] }
  })

pdf = Grover.new(html_string, format: 'A4').to_pdf

respond_to do |format|
  format.html
  format.pdf do
    send_data(pdf, disposition: 'inline', filename: "Show_ID_#{params[:id]}", type: 'application/pdf')
  end
end

My question is, how I can persuade GROVER also to process the CSS files ?

1 Answer 1

6

I had the same problem where my layout was rendered without the CSS. I was able to resolve the issue by adding the display_url option in Grover.

In local development this would be for example:

pdf = Grover.new(html_string, format: 'A4', display_url: "http://localhost:3000").to_pdf

You need to change your display_url depending on your environment.

If this does not work, make sure the html_string that you are rendering actually includes the correct markup. You can render out the html_string as HTML to verify all markup is included.

2
  • Jerry, does this work in your development environment? Since I'm generating a PDF in response to a request, this triggers requests to my application during the first request (it's a request triggering a request), and it simply hangs in my development env, even if I start puma with 2 workers.
    – sandre89
    Commented Sep 17, 2021 at 2:43
  • 1
    It works in my dev environment with display_url: "http://localhost:3000". It's hard to comment on your specific setup. I had to do a few tweaks to get pupeteer to run correctly, do you get any errors in your logs? In my situation, I created a background job which is responsible for the PDF creation. I pass a resource to the job, generate the html view (with render_to_string) and Grover generates the PDF all in a background job. Would that be a solution to avoid any race conditions perhaps in your requests? Commented Sep 20, 2021 at 9:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.