2
votes

I'm trying to render a PDF of my page that contains a google map but it always comes out as empty. Strange thing is when I call the url using the wkhtmltopdf binary it actually work. Could I be missing something??? Here's my current setup

Home Controller

class HomeController < ApplicationController
  def index
    respond_to do |format|
        format.html
        format.pdf do
            render pdf: "Example PDF",
                layout: 'pdf.html.erb',
                show_as_html: params.key?('debug'),
                javascript_delay: 3000
        end
    end
  end
end

index.pdf.erb

<%- content_for :js do %>
  <script src="//code.jquery.com/jquery-1.12.4.min.js"></script>
  <script src = "//maps.googleapis.com/maps/api/js?&key=GOOGLE_API_KEY"></script>
  <script src="//cdn.rawgit.com/mahnunchik/markerclustererplus/master/dist/markerclusterer.min.js"></script>
  <script>
      var map;
      function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: -34.397, lng: 150.644},
          zoom: 8
        });
      }
      $(window).load(function(){
        initMap();
      })
  </script>
<% end %>

<h1>Google Map</h1>
<div id="map" style="width: 800px; height: 400px;"></div>

With debug set to true (to see the html that is used to render the pdf) enter image description here

When I load the pdf url the map section is not rendered enter image description here

When I used the wkhtmltopdf command line utitlity. It correctly renders the pdf.

$ wkhtmltopdf --javascript-delay 3000 http://localhost:4000/home/index.pdf?debug  output.pdf 

enter image description here

Is there anything i'm doing wrong inside the rails app?? Some pointers would really help. Thank you!

1
do you see any error in the rails log? - Arup Rakshit
No errors in the logs. Everything runs fine - Joseph N.
Double-check that you don't have two different versions of wkhtmltopdf installed and are executing a different version on the terminal. This could be the case if you have: rubygems.org/gems/wkhtmltopdf-binary. Look at the command executed in the rails logs for more details, and try replicating that command exactly in the terminal. - Brendon Muir
@BrendonMuir the command executed in the rails log "***[\"/usr/local/bin/wkhtmltopdf\", \"-q\", \"--javascript-delay\", \"3000\", \"file:////var/folders/y4/18x7npks3w5b_yr8jz304wzr0000gn/T/wicked_pdf20180723-2048-1h1m8w7.html\", \"/var/folders/y4/18x7npks3w5b_yr8jz304wzr0000gn/T/wicked_pdf_generated_file20180723-2048-u71ska.pdf\"]*****" and doing which wkhtmltopdf returns /usr/local/bin/wkhtmltopdf which is the exact binary being used. Everything looks to be as it should - Joseph N.
Perhaps the javascript-delay is necessary in order to wait for the iframe with the map to load? Have you tried that on your command line test? - Brendon Muir

1 Answers

0
votes

Have you tried using WickedPdf.new.pdf_from_string?

It would look something like this:

Home Controller

class HomeController < ApplicationController
  def index
    respond_to do |format|
        format.html
        format.pdf do
            html = render_to_string(layout: 'pdf.html.erb')
            pdf = WickedPdf.new.pdf_from_string(html, {show_as_html: params.key?('debug'), javascript_delay: 3000})
            send_data pdf
        end
    end
  end
end

Not the best approach but you can inspect the string to see if it was generated properly before it gets pdf'd