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

CSS is not taken in account when generating the PDF #199

Closed
jetienne opened this issue Aug 14, 2023 · 1 comment
Closed

CSS is not taken in account when generating the PDF #199

jetienne opened this issue Aug 14, 2023 · 1 comment

Comments

@jetienne
Copy link

jetienne commented Aug 14, 2023

I am using Rails 7.0.7, grover (1.1.5).

My controller code is :

 def show
    @contract = Contract.find(params[:id])
    html = render_to_string layout: 'application'

    respond_to do |format|
      format.pdf do
        pdf = Grover.new(html, format: 'A4').to_pdf
        send_data pdf, filename: @contract.id, type: 'application/pdf', disposition: 'inline'
      end
    end
  end

The layout is the following:

!!!
%html
  %head
    %title= page_title app: 'My App'
    = stylesheet_link_tag "pdf"
  %body
    = yield

and finally, I have a file pdf.css file in app/assets/stylesheets.

By setting raise_on_request_failure to true, I got the following error:
"#<Grover::JavaScript::RequestFailedError: net::ERR_ABORTED at http://example.com/assets/pdf-b4394a610268cf28d98e5ef256be9e3db28862f5993e7458584da1b33530afa0.css>"

How would you manage your development environment?

@abrom
Copy link
Contributor

abrom commented Aug 22, 2023

Hi @jetienne

Please have a read of the documentation regarding relative paths:

https://github.com/Studiosity/grover#relative-paths

Put simply, when rendering the HTML using Puppeteer a display URL must be provided. If you don't provide one, Grover will default to using http://example.com. That's why you're seeing the error with the asset attempting to load from example.com!

You have two options:

  1. specify the display_url eg display_url: 'http://localhost:3000'
  2. replace the relative asset URLs with absolute URLs eg:
absolute_html = Grover::HTMLPreprocessor.process relative_html, 'http://localhost:3000/', 'http'

@abrom abrom closed this as completed Mar 9, 2024
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