简体   繁体   English

在pdfmake打印中使用Glyphicons或Font-Awesome

[英]Use Glyphicons or Font-Awesome in pdfmake prints

I am developing a webapp using pdfmake for the printing. 我正在使用pdfmake开发一个用于打印的webapp。 In more recent days I started using Glyphicons and Font-Awesome-Icons in my project, now I need those in the printout as well. 在最近的几天里,我开始在我的项目中使用Glyphicons和Font-Awesome-Icons,现在我也需要打印输出中的那些。

But I can't really imagine what would be the best way to achieve this. 但我无法想象实现这一目标的最佳方法是什么。

I see two possibilities: 我看到两种可能性:

  1. Include the respective Font in pdfmake and create something like a map which determines the Icons font-representation by it's class name (because this is what is used in the app). 在pdfmake中包含相应的Font,并创建类似map的地图,通过它的类名确定Icons字体表示(因为这是应用程序中使用的)。 In this case I could still use the font colors for the Icons 在这种情况下,我仍然可以使用图标的字体颜色

  2. I could use something like phantomJS to generate Images of the Icons, but I don't really like this idea, because I would loose the possibility to easily change the icon's color and i would have to maintain this picture collection somehow. 我可以使用像phantomJS这样的东西来生成图标的图像,但我真的不喜欢这个想法,因为我会失去轻易改变图标颜色的可能性,我将不得不以某种方式维护这个图片集。

Any Ideas, Comments or solutions? 任何想法,评论或解决方案? I would really appreciate it :) 我真的很感激 :)

I solved the problem as follows: 我解决了这个问题如下:
(I decided to use the first approach and narrowed the icons down to use Font-Awesome only) (我决定使用第一种方法并缩小图标以仅使用Font-Awesome)

  1. Find the symbol by its css-class 通过其css类找到符号
    I was quite unhappy with the idea of fetching data from eg https://fortawesome.github.io/Font-Awesome/cheatsheet/ , so I did as a co-worker suggested: I parsed the symbol directly from the stylesheet: 我对从https://fortawesome.github.io/Font-Awesome/cheatsheet/获取数据的想法非常不满意,所以我做了一个同事的建议:我直接从样式表中解析了这个符号:

 FontAwesomeMap = { findSymbolForClass: findSymbolForClass }; /** * Looks through all Stylesheets for css-selectors. Returns the content of the * first match. * * @param {string} selector The complete selector or part of it * (eg 'user-md' for '.fa-user-md') * @returns {string} The content of the 'content' attribute of the * matching css-rule <br> * or '' if nothing has been found */ function findSymbolForClass(selector) { var result = ''; var sheets = document.styleSheets; for (var sheetNr = 0; sheetNr < sheets.length; sheetNr++) { var content = findCSSRuleContent(sheets[sheetNr], selector); if (content) { result = stripQuotes(content); break; } } return result; } /** * Finds the first css-rule with a selectorText containing the given selector. * * @param {CSSStyleSheet} mySheet The stylesheet to examine * @param {string} selector The selector to match (or part of it) * @returns {string} The content of the matching rule <br> * or '' if nothing has been found */ function findCSSRuleContent(mySheet, selector) { var ruleContent = ''; var rules = mySheet.cssRules ? mySheet.cssRules : mySheet.rules; for (var i = 0; i < rules.length; i++) { var text = rules[i].selectorText; if (text && text.indexOf(selector) >= 0) { ruleContent = rules[i].style.content; break; } } return ruleContent; } /** * Strips one leading and one trailing Character from the given String. * * The 'content'-Tag is assigned by a quoted String, which will als be returned * with those quotes. * So we need to strip them, if we want to access the real content * * @param {String} string original quoted content * @returns {String} unquoted content */ function stripQuotes(string) { var len = string.length; return string.slice(1, len - 1); } 

  1. Import the Font in pdfMake Now I had to get pdfMake to know the Font, thus I needed to convert the .ttf to base64 . 在pdfMake中导入字体现在我必须得到pdfMake才能知道Font,因此我需要将.ttf转换为base64
    I used the .ttf from github and converted it here 我使用了来自github.ttf并将其转换为此处
    Afterwards I updated the vfs_fonts.js as described on github : 之后我按照github上的描述更新了vfs_fonts.js
    (stripped the base64s) (剥离base64s)

 window.pdfMake = window.pdfMake || {}; window.pdfMake.vfs = { "LICENSE.txt" : "...", "Roboto-Italic.ttf" : "...", "Roboto-Medium.ttf" : "...", "Roboto-Regular.ttf": "...", "sampleImage.jpg" : "...", "FontAwesome.ttf" : "..." }; window.pdfMake.fonts = { Roboto : { normal : 'Roboto-Regular.ttf', bold : 'Roboto-Medium.ttf', italics : 'Roboto-Italic.ttf', bolditalics: 'Roboto-Italic.ttf' }, FontAwesome: { normal : 'FontAwesome.ttf', bold : 'FontAwesome.ttf', italics : 'FontAwesome.ttf', bolditalics: 'FontAwesome.ttf' } }; 

  1. Set proper style-informations 设置正确的样式信息
    Last but not least, the font-information has to be set, so I made a simple style for that: 最后但并非最不重要的是,必须设置字体信息,所以我为此制作了一个简单的样式:

 styles = { /*...*/ symbol: { font: 'FontAwesome' }, /*...*/ } 

edit as @nicfo pointed out, I missed to show the actual use of all together: 编辑为@nicfo指出,我错过了显示实际使用的所有:
4. Use the symbols in pdfmake 4. 使用pdfmake中的符号

 value = { text : FontAwesomeMap.findSymbolForClass(symbol) + '', style: ['symbol'] }; 

where the magic FontAwesomeMap.findSymbolForClass(symbol) ist the one mentioned above 其中神奇的FontAwesomeMap.findSymbolForClass(symbol)就是上面提到的那个

That's all. 就这样。 Not exactly easy, but it's worth the effort. 不容易,但值得努力。

I'm pretty sure the same works for Glyphicons. 我很确定Glyphicons也能这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM