Pez*_*lio 2 ruby pdf-generation ruby-on-rails wicked-pdf
我一直在使用wicked_pdf在Rails中生成一些PDF,并且它在我的开发环境中工作正常,但是当我尝试在我的生产环境中生成一个错误时,我得到500错误(但我的日志中没有特定错误).我注意到的第一件事是wkhtmltopdf二进制文件位于我的生产盒上的不同位置,所以我将以下内容添加到我的wicked_pdf.rb初始化程序中:
if Rails.env == "production"
WickedPdf.config = {
:exe_path => '/usr/bin/wkhtmltopdf'
}
end
Run Code Online (Sandbox Code Playgroud)
这是我在控制器中调用它的方式:
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
Run Code Online (Sandbox Code Playgroud)
这是我的观点的内容:
# 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>
Run Code Online (Sandbox Code Playgroud)
当我添加debug=true到查询字符串时,它似乎生成OK(并且wicked_pdf_image_tag帮助程序似乎生成了正确的位置,这似乎是Rails 3.1中的陷阱).有任何想法吗?我是Ruby/Rails的新手,所以请保持温和!
如果让Bundler担心将依赖关系源化为wkthmltopdf,那么这样做要容易得多.你可以通过安装这个来做到这一点:
gem "wkhtmltopdf-binary"
Run Code Online (Sandbox Code Playgroud)
然后运行bundle install.之后,您应该能够删除自定义exe_path规范,它应该可以正常工作.如果这不起作用,请告诉我.
乔
| 归档时间: |
|
| 查看次数: |
1936 次 |
| 最近记录: |