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

wicked_pdf_javascript_pack_tag appears to insert a generic html example document #961

Closed
mfrederickson opened this issue Jan 26, 2021 · 2 comments

Comments

@mfrederickson
Copy link

mfrederickson commented Jan 26, 2021

Issue description

It appears to be inserting an "example domain" html document instead of my javascript file.

When my layout contained stylesheet_pack_tag and javascript_pack_tag it produced the following:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Surveyor</title>
    <!-- stylesheet_pack_tag -->
    
    <!-- javascript_pack_tag -->
    <script src="/packs/js/application-4f9ed7c5710b7f159806.js"></script>
  </head>
  <body>
    <main role="main" class="container-fluid application-">
...

But when I added the wicked_pdf_ prefix to those, it produced:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Surveyor</title>
    <!-- wicked_pdf_stylesheet_pack_tag -->
    <style type='text/css'></style>
   
    <!-- wicked_pdf_javascript_pack_tag -->
    <script type='text/javascript'><!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style type="text/css">
    body {
        background-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
        
    }
    div {
        width: 600px;
        margin: 5em auto;
        padding: 2em;
        background-color: #fdfdff;
        border-radius: 0.5em;
        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
    }
    a:link, a:visited {
        color: #38488f;
        text-decoration: none;
    }
    @media (max-width: 700px) {
        div {
            margin: 0 auto;
            width: auto;
        }
    }
    </style>    
</head>

<body>
<div>
    <h1>Example Domain</h1>
    <p>This domain is for use in illustrative examples in documents. You may use this
    domain in literature without prior coordination or asking for permission.</p>
    <p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
</script>
  </head>

  <body>
    <main role="main" class="container-fluid application-">
...

I obtained this html with the following code in the rails console in development:

s = Survey.find(4836)
p = ApplicationController.render( template:'surveys/print', layout: 'wicked_pdf', assigns: { survey: s}, format: 'pdf' )
File.open('/tmp/bubkis.html', 'wb') { |f| f<< p }

and the wicked_pdf.html.erb layout file:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Surveyor</title>

    <!-- wicked_pdf_stylesheet_pack_tag -->
    <%= wicked_pdf_stylesheet_pack_tag 'application' %>
    
    <!-- wicked_pdf_javascript_pack_tag -->
    <%= wicked_pdf_javascript_pack_tag 'application' %>
  </head>

  <body>
    <main role="main" class="container-fluid <%= "#{controller_name}-#{action_name}" %>">
      <%= yield %>
    </main>
  </body>
</html>

The app/javascript/packs/application.js file has a line that imports the styles from ../stylesheets/application and the views all look fine when running the application in development, staging, and production. Trying out this gem in the development rails console is not producing the correct HTML needed before I can use this gem to convert to a PDF.

I am not using the asset pipeline for my stylesheets and have no asset_host defined.
rails webpacker:compile says everything is up to date.

It looks like the path coming back from the webpacker_source_url is http://example.org/pack/js/application-... which is probably why I am getting that "example domain" html document instead of the javascript I expect, but I'm not sure how to get it to load the javascript asset from the file path.

If I change the asset_pack_url in webpacker_source_url to asset_pack_path it seems to work much better, but I don't feel that's the right way to fix my problem.

Expected or desired behavior

I want to generate a PDF of the "print" view for the survey in a background job and save it to a file. I was hoping that the CSS styles would be included correctly and everything would look sweet-- I've been through the README several times, but I'm not sure what I am doing wrong.

System specifications

Rails 6.0.3.2 using Webpacker
wicked_pdf gem version: 2.1.0
wkhtmltopdf version: 0.12.6 (with patched qt)
wkhtmltopdf-binary gem version: (0.12.6.5)
Ubuntu 20.04.1 LTS

@vkalach
Copy link
Contributor

vkalach commented Apr 23, 2021

the problem is here /~https://github.com/mileszs/wicked_pdf/blob/2.1.0/lib/wicked_pdf/wicked_pdf_helper/assets.rb#L177
I found two solutions:

  1. add asset_host to your environment file
  2. patch this method like this
def webpacker_source_url(source)
    return unless defined?(Webpacker) && defined?(Webpacker::VERSION)

    # In Webpacker 3.2.0 asset_pack_url is introduced
    if Webpacker::VERSION >= '3.2.0'
      asset_pack_path(source, host: Rails.application.config.asset_host || root_url)
    else
      source_path = asset_pack_path(source)
      # Remove last slash from root path
      root_url[0...-1] + source_path
    end
  end```

@unixmonkey
Copy link
Collaborator

Looks like this was fixed in #973

Please reopen if this can be reproduced in the latest version.

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

3 participants