我一直在使用 wicked_pdf 在 Rails 中生成一些 PDF,它在我的开发环境中运行良好,但是当我尝试在生产环境中生成 PDF 时,我收到 500 错误(但我的日志中没有具体错误)。我注意到的第一件事是 wkhtmltopdf 二进制文件位于我的生产盒上的不同位置,因此我将以下内容添加到我的 wicked_pdf.rb 初始值设定项中:
if Rails.env == "production"
WickedPdf.config = {
:exe_path => '/usr/bin/wkhtmltopdf'
}
end
这是我在控制器中调用它的方式:
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
这是我的观点的内容:
# 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>
当我添加debug=true
对于查询字符串,它似乎生成正常(并且wicked_pdf_image_tag
helper 似乎生成了正确的位置,这似乎是 Rails 3.1 中的一个陷阱)。有任何想法吗?我对 Ruby / Rails 还很陌生,所以请温柔一点!