2013-03-13 27 views
1

,直到我開始使用PDFKit創建Rails中從HTML PDF文件,我已成功嵌入谷歌的字體是這樣的:如何嵌入與PDKFit和資產管道字體Rails中

= stylesheet_link_tag 'http://fonts.googleapis.com/css?family=Rokkitt:400,700' 

然而,PDFKit沒有嵌入字體在我的PDF文件中。它生成PDF並使用我的打印樣式表格式化,但使用常規字體,而不是我的嵌入式樣式。

於是,我把EOT,WOFF,在/app/assets/fonts每個字體的TTF和SVG版本,並在我的屏幕和打印樣式表稱爲一個新的字體樣式切換到這種格式:

@font-face { 
    font-family: 'rokkittregular'; 
    src: font-url('rokkitt-regular-webfont.eot'); 
    src: local('rokkittregular') 
     font-url('rokkitt-regular-webfont.eot?#iefix') format('embedded-opentype'), 
     font-url('rokkitt-regular-webfont.woff') format('woff'), 
     font-url('rokkitt-regular-webfont.ttf') format('truetype'), 
     font-url('rokkitt-regular-webfont.svg#rokkittregular') format('svg'); 
    font-weight: normal; 
    font-style: normal;} 

同樣,這個工作對我的常規HTML頁面來說很好。再次,PDFKit沒有在我的PDF中嵌入字體。

假設這可能與PDFKit無法找到字體有關,我創建了一個單獨的打印字體樣式表以嵌入到我的打印樣式表中。在這裏面,我切換到這個符號:

@font-face { 
    font-family: "rokkittregular"; 
    src: url("/assets/rokkitt-regular-webfont.eot"); 
    src: url("/assets/rokkitt-regular-webfont.eot?#iefix") format("embedded-opentype"), 
     url("/assets/rokkitt-regular-webfont.woff") format("woff"), 
     url("/assets/rokkitt-regular-webfont.ttf") format("truetype"), 
     url("/assets/rokkitt-regular-webfont.svg#rokkittregular") format("svg"); 
    font-weight: normal; 
    font-style: normal;} 

後來到這個符號:

@font-face { 
    font-family: "rokkittregular"; 
    src: url("#{Rails.root}/assets/rokkitt-regular-webfont.eot"); 
    src: url("#{Rails.root}/assets/rokkitt-regular-webfont.eot?#iefix") format("embedded-opentype"), 
     url("#{Rails.root}/assets/rokkitt-regular-webfont.woff") format("woff"), 
     url("#{Rails.root}/assets/rokkitt-regular-webfont.ttf") format("truetype"), 
     url("#{Rails.root}/assets/rokkitt-regular-webfont.svg#rokkittregular") format("svg"); 
    font-weight: normal; 
    font-style: normal;} 

仍然沒有奏效。

奇怪的是,如果您將瀏覽器指向http://localhost:5000/assets/rokkitt-regular-webfont.eot,它會將您帶到字體文件。

如何獲得這些字體在PDFKit中工作?

+0

哪上述網址方法到底的工作?有同樣的問題,並希望你能更新這個答案;) – 2013-10-07 12:48:06

+1

第一個。但從那時起,我已經轉向使用DocRaptor,並將樣式表中的字體作爲Base64嵌入到樣式表中。乾杯。 – 2013-10-08 21:05:53

+0

太棒了,非常感謝您花時間更新答案。看起來Base64是最好的前進方向。 – 2013-10-09 06:47:49

回答

0

解決的辦法是改變我叫我的打印樣式表的字體:

h1 {font-family: "rokkittregular";} 

h1 {font-family: "Rokkitt", serif;}