Wicked_pdf 在开发中工作正常,但在生产中不工作 [英] Wicked_pdf working fine in development, but not in production

查看:63
本文介绍了Wicked_pdf 在开发中工作正常,但在生产中不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 wicked_pdf 在 Rails 中生成一些 PDF,它在我的开发环境中运行良好,但是当我尝试在我的生产环境.我注意到的第一件事是 wkhtmltopdf 二进制文件在我的生产机器上的不同位置,所以我在我的 wicked_pdf.rb 初始值设定项中添加了以下内容:

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

这是我在控制器中调用它的方式:

 def 证书@inspection = Inspection.find(params[:id])@council = Council.find(@inspection.councilid)response_to do |格式|格式.pdf做渲染:pdf =>@inspection.slug,:show_as_html =>params[:debug].present?,:边距 =>{:top =>0,:底部 =>0,:左 =>0,:对 =>0}结尾结尾结尾

这是我的观点的内容:

# 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"><头><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/><style type="text/css">身体 {边距:0;填充:0;字体系列:Lucida Grande"、Lucida Sans Unicode"、Helvetica、Arial、sans-serif;}图片#bg {宽度:800px;高度:1130px;位置:绝对;}#date p, #council p {行高:17px;字体大小:12px;}#理事会{位置:绝对;顶部:650px;左:445px;}#商标 {位置:绝对;顶部:965px;左:98px;}#logo img {高度:65px;}#地址 {位置:绝对;顶部:425px;左:300px;}#地址p{字体大小:22px;行高:27px;}#日期 {位置:绝对;顶部:650px;左:98px;}</风格><身体><%= wicked_pdf_image_tag "证书#{@inspection.rating}.jpg", :id =>"bg" %><div id="地址"><p><%=@inspection.name %><br/><%= @inspection.address("<br/>").html_safe %></p>

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

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

<div id="标志"><%= wicked_pdf_image_tag "证书/#{@council.logo}.png" %>

</html>

当我将 debug=true 添加到查询字符串时,它似乎生成了 OK(并且 wicked_pdf_image_tag 助手似乎生成了正确的位置,这似乎是一个问题在 Rails 3.1 中).有任何想法吗?我对 Ruby/Rails 还很陌生,所以请保持温和!

解决方案

如果让 bundler 担心依赖 wkthmltopdf,这样做会容易得多.你可以通过安装这个来做到这一点:

gem "wkhtmltopdf-binary"

然后运行 ​​bundle install.之后,您应该能够删除您的自定义 exe_path 规范,它应该可以正常工作.如果这不起作用,请告诉我.

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!

解决方案

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

这篇关于Wicked_pdf 在开发中工作正常,但在生产中不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆