相同的代码,Wicked PDF 在两台不同的计算机上生成两个不同的报告
Same code, Wicked PDF produces two different reports on two different computers
我在 Rails 上使用 Ruby 使用 WickedPDF 生成 PDF 并通过电子邮件发送。我在两台计算机上有完全相同的代码(代码存储在 GitHub 上,本地实例是最新的并且完全相同),但无论出于何种原因,PDF 报告中提供的字体都不同.一个是Arial,另一个好像是Verdana。
这是创建 PDF 的代码:
header_html = av.render(template: "layouts/report_header", locals: {company_name: company_name, layout: nil)
footer_html = av.render(template: "layouts/report_footer", locals: {company_name: company_name, layout: nil)
pdf_html = av.render(:template => "reports/report", locals: {data: data})
body_pdf = WickedPdf.new.pdf_from_string(
pdf_html,
footer: {
content: footer_html
},
header: {
content: header_html
},
margin: {
top: 25,
bottom: 17,
left: 10,
right: 10
},
)
body_path = Rails.root.join("public/#{@project_id}/body.pdf")
File.open(body_path, "wb") do |file|
file << body_pdf
end
pdf_html
的内容(内容正文)显示为:
<!DOCTYPE html>
<html>
<head>
<style>
p {
color: #545658;
font-size: 12pt;
font-family: "Arial";
font-weight: 500;
}
li {
color: #545658;
font-size: 12pt;
font-family: "Arial";
font-weight: 500;
}
h1 {
color: #ED1C24;
font-weight: normal;
font-family: "Arial";
}
th {
padding-top: 12px;
padding-bottom: 12px;
text-align: center;
background-color: black;
color: white;
}
td {
color: #545658;
padding-top: 5px;
padding-left: 10px;
padding-right: 10px;
padding-bottom: 5px;
}
table.bordered {
border-collapse: collapse;
}
table.bordered td {
border: 1px solid black;
}
table.bordered tr:first-child td {
border-top: 0;
}
table.bordered tr td:first-child {
border-left: 0;
}
table.bordered tr:last-child td {
border-bottom: 0;
}
table.bordered tr td:last-child {
border-right: 0;
}
tr.bordered:nth-child(even) {background-color: #f2f2f2;}
img.finding {
position:absolute;
width:60%;
height: 40px;
margin-left: -20px;
max-width: 100%;
z-index:-1;
}
p.finding {
display: inline;
color: white;
font-weight: bold;
font-size: 16pt;
line-height: 1.75em;
}
pre code {
background-color: #eee;
border: 1px solid #999;
display: block;
padding: 5px;
font-family: "Consolas";
font-size: 10pt;
color: black;
}
</style>
</head>
<body>
<p>Hello world</p>
</body>
</html>
这在一台计算机上工作得很好,但在另一台计算机上就不行了。我感觉这个问题发生在 Rails.
之外
关于为什么这可能会在两台不同的计算机上给我不同的结果有什么建议吗?
wicked_pdf
使用 wkhtmltopdf。如果要在所有计算机上获得相同的结果,wkhtmltopdf
版本必须在所有计算机上相同。可能wkhtmltopdf
在电脑上的版本不一样。
更新
所有计算机都必须有 Arial
字体。如果 Arial
字体丢失,可能会导致此错误。
我在 Rails 上使用 Ruby 使用 WickedPDF 生成 PDF 并通过电子邮件发送。我在两台计算机上有完全相同的代码(代码存储在 GitHub 上,本地实例是最新的并且完全相同),但无论出于何种原因,PDF 报告中提供的字体都不同.一个是Arial,另一个好像是Verdana。
这是创建 PDF 的代码:
header_html = av.render(template: "layouts/report_header", locals: {company_name: company_name, layout: nil)
footer_html = av.render(template: "layouts/report_footer", locals: {company_name: company_name, layout: nil)
pdf_html = av.render(:template => "reports/report", locals: {data: data})
body_pdf = WickedPdf.new.pdf_from_string(
pdf_html,
footer: {
content: footer_html
},
header: {
content: header_html
},
margin: {
top: 25,
bottom: 17,
left: 10,
right: 10
},
)
body_path = Rails.root.join("public/#{@project_id}/body.pdf")
File.open(body_path, "wb") do |file|
file << body_pdf
end
pdf_html
的内容(内容正文)显示为:
<!DOCTYPE html>
<html>
<head>
<style>
p {
color: #545658;
font-size: 12pt;
font-family: "Arial";
font-weight: 500;
}
li {
color: #545658;
font-size: 12pt;
font-family: "Arial";
font-weight: 500;
}
h1 {
color: #ED1C24;
font-weight: normal;
font-family: "Arial";
}
th {
padding-top: 12px;
padding-bottom: 12px;
text-align: center;
background-color: black;
color: white;
}
td {
color: #545658;
padding-top: 5px;
padding-left: 10px;
padding-right: 10px;
padding-bottom: 5px;
}
table.bordered {
border-collapse: collapse;
}
table.bordered td {
border: 1px solid black;
}
table.bordered tr:first-child td {
border-top: 0;
}
table.bordered tr td:first-child {
border-left: 0;
}
table.bordered tr:last-child td {
border-bottom: 0;
}
table.bordered tr td:last-child {
border-right: 0;
}
tr.bordered:nth-child(even) {background-color: #f2f2f2;}
img.finding {
position:absolute;
width:60%;
height: 40px;
margin-left: -20px;
max-width: 100%;
z-index:-1;
}
p.finding {
display: inline;
color: white;
font-weight: bold;
font-size: 16pt;
line-height: 1.75em;
}
pre code {
background-color: #eee;
border: 1px solid #999;
display: block;
padding: 5px;
font-family: "Consolas";
font-size: 10pt;
color: black;
}
</style>
</head>
<body>
<p>Hello world</p>
</body>
</html>
这在一台计算机上工作得很好,但在另一台计算机上就不行了。我感觉这个问题发生在 Rails.
之外关于为什么这可能会在两台不同的计算机上给我不同的结果有什么建议吗?
wicked_pdf
使用 wkhtmltopdf。如果要在所有计算机上获得相同的结果,wkhtmltopdf
版本必须在所有计算机上相同。可能wkhtmltopdf
在电脑上的版本不一样。
更新
所有计算机都必须有 Arial
字体。如果 Arial
字体丢失,可能会导致此错误。