Question

I've been working with wicked_pdf to generate some PDFs in Rails, and it's been working fine in my dev environment, but I get a 500 error (but no specific errors in my log) when I try and generate one on my production environment. The first thing I noticed was that the wkhtmltopdf binary was in a different location on my production box, so I've added the following to my wicked_pdf.rb initializer:

if Rails.env == "production"
    WickedPdf.config = {
        :exe_path => '/usr/bin/wkhtmltopdf'
    }
end

Here's how I'm calling it in my controller:

  def certificate
    @inspection = Inspection.find(params[:id])
    @council = Council.find(@inspection.councilid)  
    respond_to do |format|
        format.pdf do
            render :pdf => @inspection.slug,
                   :show_as_html => params[:debug].present?,
                   :margin => {:top            => 0,
                               :bottom         => 0,
                               :left           => 0,
                               :right          => 0}
        end
    end
  end

And here's the contents of my view:

# certificate.pdf.erb

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <style type="text/css">
        body {
            margin: 0;
            padding: 0;
            font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, sans-serif;
        }

        img#bg {
            width: 800px;
            height: 1130px;
            position: absolute;
        }

        #date p, #council p {
            line-height: 17px;
            font-size: 12px;
        }

        #council {
            position: absolute;
            top: 650px;
            left: 445px;        
        }

        #logo {
            position: absolute;
            top: 965px;
            left: 98px;
        }

        #logo img {
            height: 65px;
        }

        #address {
            position: absolute;
            top: 425px;
            left: 300px;        
        }

        #address p {
            font-size: 22px;
            line-height: 27px;
        }

        #date {
            position: absolute;
            top: 650px;
            left: 98px;
        }

    </style>
  </head>
  <body>
      <%= wicked_pdf_image_tag "certificate#{@inspection.rating}.jpg", :id => "bg" %>

      <div id="address">
      <p><%= @inspection.name %><br />
      <%= @inspection.address("<br />").html_safe %>      </p>
      </div>

      <div id="date">
      <p><%= @inspection.date.strftime("%B %d %Y") %></p>
      </div>

      <div id="council">
      <p><%= @council.address.html_safe %><br /><br />
      <strong>Tel: </strong><%= @council.tel %></p>
      </div>

      <div id="logo">
      <%= wicked_pdf_image_tag "certificates/#{@council.logo}.png" %>
      </div>

  </body>
</html>

When I add debug=true to the query string it seems to generate OK (and the wicked_pdf_image_tag helper seems to generate the correct location, which seemed to be a gotcha in Rails 3.1). Any ideas? I am pretty new to Ruby / Rails, so please be gentle!

Was it helpful?

Solution

Its much much much easier to do this if you let bundler worry about sourcing the dependency to wkthmltopdf. You can do that by installing this:

gem "wkhtmltopdf-binary"

and then running bundle install. After that you should be able to remove your custom exe_path specification and it should work correctly. If that doesn't work let me know.

Joe

OTHER TIPS

If still image not showing in production environment then please try

image_tag wicked_pdf_asset_base64('image.png')

I'm using wicked-pdf as an attachment to the email. In development, it is working fine but in production, it shows

ActionView::Template::Error: uninitialized constant AssetCdnHosts

attachments["purchase_order.pdf"] = WickedPdf.new.pdf_from_string(
        render_to_string(pdf: 'purchase_order', template: "device_orders/_preview_purchase_order.html.erb",locals: {vendor_info: vendor_info,device_order: device_order})
      )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top