0

I am having issues getting javascript to execute in my wicked_pdf template. I have a large template with styling and some charts in which everything but the charts (that was the only javascript on that page) working. After spending a long time debugging I came to a conclusion that nothing in the Javascript was running, so I removed everything from the template except one h1 tag and some javascript to remove it. Here is what my project currently looks like:

app/views/frontend/pdf/page_report.pdf.erb

<!doctype html>
<html>
  <head>
    <meta charset='utf-8'/>
    <script type="text/javascript">
      function this_is_a_test() {
        document.getElementById('debug').remove();
      };
    </script>
  </head>
    <body onload="this_is_a_test()">
    <h1 id="debug">Test</h1>
  </body>
</html>

app/controllers/frontend_controller.rb

   def pdf_report
     respond_to do |format|
       format.html
       format.pdf do
         render pdf: "report_file", template: 'frontend/pdf/page_report', javascript_delay: 3000
       end
     end
   end

config/initializers/mime_types.rb

Mime::Type.register "application/pdf", :pdf

Even with something as simple as this, the h1 tag is not removed, keep in mind I have tried other simple javascript things that also did not work. Anyone have any suggestions?

4
  • did you add javascript helper to layout file? Aug 13, 2020 at 8:10
  • I'm not 100% certain what you mean by javascript helper, the whole template file is in the question though, under app/views/frontend/pdf/page_report.pdf.erb
    – nicks6853
    Aug 13, 2020 at 12:03
  • If you plan to use any CSS, JavaScript, or image files, you must modify your layout so that you provide an absolute reference to these files. The best option for Rails without the asset pipeline is to use the wicked_pdf_stylesheet_link_tag, wicked_pdf_image_tag, and wicked_pdf_javascript_include_tag helpers or to go straight to a CDN (Content Delivery Network) for popular libraries such as jQuery. Aug 14, 2020 at 7:57
  • 1
    @Roman Alekseiev: He's talking about the inline function this_is_a_test, not about something pulled from a different file. I've got the same problem, some simple JS defined in a script tag in the same file, and it simply won't execute.
    – MDickten
    Dec 8, 2022 at 10:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.