Wicked_pdf working fine in development, but not in production

两盒软妹~` 提交于 2019-12-21 21:36:24

问题


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!


回答1:


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




回答2:


If still image not showing in production environment then please try

image_tag wicked_pdf_asset_base64('image.png')



回答3:


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})
      )


来源:https://stackoverflow.com/questions/9482834/wicked-pdf-working-fine-in-development-but-not-in-production

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!