【发布时间】:2019-09-18 20:28:36
【问题描述】:
我正在使用 Ruby on Rails 使用 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 之外。
关于为什么这可能会在两台不同的计算机上给我不同的结果有什么建议吗?
【问题讨论】:
-
如果您没有在项目中提供网络字体,则取决于浏览器从
font-family列表中解析的内容。font-family充当愿望清单:*我希望使用这些字体中的任何一种,如果它们在您的系统上可用。”这就是为什么您应该始终以通用字体结束您的font-family(serif,sans-serif、cursive、monospace、fantasy)。要在任何机器上呈现相同的内容,请使用网络字体。 -
我使用相同的浏览器在两台不同的服务器上测试相同的代码,但事实就是如此。有什么理由,只要代码相同,我会得到两个不同的结果吗?
-
是的,浏览器可以有不同的默认设置,并且浏览器不附带字体。他们从操作系统问他们。这可能会及时改变(我希望 Chrome 会在某个时候切换到使用一些默认的 Google 字体,因为它会提供完全一样的 - 在任何系统上的相同体验)。为了让网站在不同的系统上使用不同的字体呈现,将第一个
font-family安装在一个系统上而不是另一个系统上就足够了。
标签: html css ruby-on-rails wicked-pdf