[英]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: 我看到两种可能性:
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
在这种情况下,我仍然可以使用图标的字体颜色
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)
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); }
.ttf
to base64
. .ttf
转换为base64
。 .ttf
from github and converted it here .ttf
并将其转换为此处 vfs_fonts.js
as described on github : vfs_fonts.js
: 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' } };
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.