邪恶的PDF +字体+ heroku + rails3.2 [英] Wicked PDF +fonts+heroku+rails3.2

查看:178
本文介绍了邪恶的PDF +字体+ heroku + rails3.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用rails 3.2.11和ruby 1.9.3使用 wicked_pdf 来生成PDF HTML和部署到Heroku。



我的pdf.css.scss.erb:

 <%app_fullhost =常量[app_fullhost]%> 

@ font-face {
font-family:'DosisMedium';字体风格:正常;字体重量:500;
src:url(<%= app_fullhost%> /app/font/dosis/Dosis-Medium.ttf)format('woff');

$ b,body {
font-family:DosisLight,Times New Roman,Arial,sans-serif;

其中 app_fullhost 是确切的主机,在开发或生产。



我的pdf版本包含其他内容:

 %html { :lang => I18n.locale} 
%head
%meta {:charset => utf-8}
%title = content_for?(:title)? yield(:title):Settings.app_name
= wicked_pdf_stylesheet_link_tagpdf

rb我有

  config.assets.precompile + =%w(pdf.css)

这在开发过程中没有问题,但是在Heroku中,pdf文件不加载所需的字体。我也尝试了不同的解决方案,例如在production.rb中添加这些解决方案:

  config.assets.paths<< $#$ b config.assets.precompile + =%w + =%w(.svg .eot .woff .ttf)

我也试着改变在pdf.css.scss.erb中):

  @ font-face {
font-family:'Dosis' ;字体风格:正常;字体重量:500;
src:url('Dosis-Medium.ttf')格式('woff');

code $

$ b $或b
$ b $ pre> @ font-face {
font-family:'Dosis';字体风格:正常;字体重量:500;
src:url(<%= asset_path('Dosis-Medium.ttf')%>)格式('woff');

字体位于 assets / fonts ,也可以在 public / app / font / dosis 和Heroku上的URL正确地响应:

  ..// myapp / app / font / dosis / Dosis-Medium.ttf和
..//myapp/assets/Dosis-Medium.ttf

如何在Heroku上加载字体?

解决方案

wkhtmltopdf ,底层 wicked_pdf 的程序是非常奇怪的通过CSS加载字体在某些系统上,它使用绝对路径,有时需要相对路径,即使你正确地得到了路径,它可能会被不准确的CSS减速等抛出。关于这个问题有几十个问题我已经找到了最好,最灵活和最便携的解决方案,就是Base64对你要使用的字体进行编码,并将其直接包含到CSS文件:

  @ font-face {
font-family:'OpenSans';
src:url(data:font / truetype; charset = utf-8; base64,AAEAAAATAQA ...


I'm using wicked_pdf with rails 3.2.11 and ruby 1.9.3 to generate a PDF from HTML and deploying to Heroku.

My pdf.css.scss.erb:

<% app_fullhost = Constants["app_fullhost"] %>

@font-face {
  font-family:'DosisMedium'; font-style:normal; font-weight:500;
  src: url(<%=app_fullhost%>/app/font/dosis/Dosis-Medium.ttf) format('woff');
}

*, body {
  font-family: "DosisLight", 'Times New Roman', 'Arial', sans-serif;
} 

where app_fullhost is the exact host, in development or production.

My pdf layout includes among other things :

%html{:lang => I18n.locale}
  %head
    %meta{:charset => "utf-8"}
    %title= content_for?(:title) ? yield(:title) : Settings.app_name
    = wicked_pdf_stylesheet_link_tag "pdf"

In production.rb I have

config.assets.precompile +=%w(pdf.css)

This works without problems in development, but on Heroku the pdf file doesn't load the desired font. I have also tried different solutions like adding these in production.rb:

config.assets.paths << "#{Rails.root}/app/assets/fonts"
config.assets.precompile += %w(*.svg *.eot *.woff *.ttf) 
config.assets.precompile += %w(.svg .eot .woff .ttf) 

and I tried also to change ( in pdf.css.scss.erb ) :

@font-face {
  font-family:'Dosis'; font-style:normal; font-weight:500;
  src: url('Dosis-Medium.ttf') format('woff');
}

or

@font-face {
  font-family:'Dosis'; font-style:normal; font-weight:500;
  src: url(<%= asset_path('Dosis-Medium.ttf')%>) format('woff');
}

The fonts are in assets/fonts and also in public/app/font/dosis and url on Heroku respond correctly with:

..//myapp/app/font/dosis/Dosis-Medium.ttf" and 
..//myapp/assets/Dosis-Medium.ttf 

How can I get the font to load on Heroku?

解决方案

wkhtmltopdf, the program underlying wicked_pdf, is notoriously funky when it comes to loading fonts through CSS. On some systems it works with absolute paths, sometimes it requires relative paths. Even if you get the paths correctly, it may get thrown off by inaccurate CSS deceleration, etc. There are dozens of questions regarding this just on SO alone.

The best, most flexible and most portable soltion that I've found is to Base64-encode the font you're trying to use, and include it directly into the CSS file:

@font-face {
    font-family: 'OpenSans';
    src: url(data:font/truetype;charset=utf-8;base64,AAEAAAATAQA...
}

这篇关于邪恶的PDF +字体+ heroku + rails3.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆