Rendering js generated SVG in PDF in Rails

105 Views Asked by At

Trying to render an SVG generated with svg.js in a PDF (wkhtmltopdf) in Rails 5 but nothing comes out. All other rendering in the PDF works so it's likely the JS not being called but I can't understand why. Rendering the JS in a plain HTML view works so I'm sure the actual JS works.

In my show.pdf.erb

<head>
  <script src="https://cdn.jsdelivr.net/npm/@svgdotjs/[email protected]/dist/svg.min.js"></script>
  <%= wicked_pdf_stylesheet_link_tag "application" %>
  <%= wicked_pdf_javascript_include_tag "svg_graph" %>
</head>

In assets/javascripts/svg_graph.js

var svg_graph = ({
     .....
     lots of js (a function called initialize among others)
     .....
);

In show.print.erb:

  <div class="pdf-page">
    <div class="content">
        <%= render 'svg_graph', location_id: location.id %>
    </div>
    <%= render 'footer' %>
  </div>

And in svg_graph.html.erb:

<div class="svg-graph mt-3" id="svg-graph-<%= location_id %>"></div>
<script>
    svg_graph.initialize(document.getElementById("svg-graph-" + <%= location_id %>), <%= location_id %>);
</script>

Any idea where I'm going wrong?

0

There are 0 best solutions below