Question

In Rails I have correctly setup a view to display a captured signature using Signature Pad

I would now like to generate a pdf displaying the signature. The signature is json saved as a string in my postgresql db and as mentioned it will generate fine in a html view, the issue is generating in pdf.

I believe I have included the necessary assets correctly for wicked_pdf:

<%= wicked_pdf_stylesheet_link_tag "application" %>
<%= wicked_pdf_javascript_include_tag "application" %>

I have also tried to include the js directly in the pdf view:

<head>
  <script>
    function draw_signature() {
      var sig = $('#signature').val();
      if (typeof(sig) != 'undefined' || sig != null)
      {
        $('.sigPad').signaturePad({displayOnly:true}).regenerate(sig);
      }
    }
  </script>
</head>
<body onload="draw_signature()">
  <%= hidden_field_tag("signature", @customer.signature, :id => "signature") %>
    <div class="sigPad signed">
      <canvas class="pad" width="400" height="140"></canvas>
    </div>
Was it helpful?

Solution

I was able to get your above example working with gem version: wicked_pdf (1.0.6)

Modifying the include tags to reference the latest Jquery CDN, also the Jquery signature library made available in the vendor/assets directory.

Please see below for the working code:

<%= wicked_pdf_javascript_include_tag "https://code.jquery.com/jquery-3.1.1.min.js" %>
<%= wicked_pdf_javascript_include_tag "jquery.signaturepad.min" %>
<script type="text/javascript">
  $(document).ready(function(){
    var signature = $('#signature').val();
    if (typeof(signature) != 'undefined' && signature != null && signature != "")
    {
        $('.sigPad').signaturePad({displayOnly:true}).regenerate(signature);
    }
});
</script>


<body>
  <%= hidden_field_tag("signature", @customer.signature, :id => "signature") %>
    <div class="sigPad signed">
      <canvas class="pad" width="400" height="140"></canvas>
    </div>
</body>

The Jquery signature library was also included in the assets precompile listing to be made available as an individual asset:

config/initializers/assets.rb Rails.application.config.assets.precompile += ['jquery.signaturepad.min.js']

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top