在Grails中的PDF呈现插件中嵌入字体
我想在PDF文档中嵌入“HelveticaNeueLTCom BdCn.ttf”。我正在使用Grails rendering 0.4.4插件生成PDF文件。 我试着跟着在Grails中的PDF呈现插件中嵌入字体,pdf,grails,Pdf,Grails,我想在PDF文档中嵌入“HelveticaNeueLTCom BdCn.ttf”。我正在使用Grails rendering 0.4.4插件生成PDF文件。 我试着跟着 @font-face { font-family: 'Helvetica'; src: url('${grailsApplication.config.grails.serverURL}/fonts/HelveticaNeueLTCom-BdCn.ttf'); -fs-pdf-font-embed: embed
@font-face {
font-family: 'Helvetica';
src: url('${grailsApplication.config.grails.serverURL}/fonts/HelveticaNeueLTCom-BdCn.ttf');
-fs-pdf-font-embed: embed;
-fs-pdf-font-encoding: Identity-H;
}
但是它不起作用。可能问题是您的
url
值被“
包围,而不是”
它们之间的区别在于,尽管在Groovy中,字符串文字可以同时使用这两种文字,但只有被
“
包围的文字才能创建GString,它计算${}
之间的语句。字体嵌入需要遵循以下步骤。这对我有用。
试着告诉我你的反馈
protected doRender(Map args, Document document, OutputStream outputStream)
{
def renderer = new ITextRenderer()
// add the real font path from the server to be deployed to.
//I have it in the assets folder of my project
def path=servletContext.getRealPath("/")+"assets/HelveticaNeueLTCom-BdCn.ttf"
ITextFontResolver fontResolver=renderer.getFontResolver();
//add the encoding and embedded types to the font
fontResolver.addFont(path,BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
configureRenderer(renderer)
renderer.setDocument(document, args.base)
renderer.layout()
renderer.createPDF(outputStream)
outputStream.close();
}
@font-face {
font-family: "Helvetica";
src: url("${grailsApplication.config.grails.serverURL}/assets/HelveticaNeueLTCom-BdCn.ttf") format("truetype"),
url("${grailsApplication.config.grails.serverURL}/assets/HelveticaNeueLTCom-BdCn.woff") format("woff"),
url("${grailsApplication.config.grails.serverURL}/assets/HelveticaNeueLTCom-BdCn.svg#HelveticaNeueLTCom-BdCn") format("svg");
-fs-pdf-font-embed: embed;
-fs-pdf-font-encoding: Identity-H;
}
@font-face {
src: url("path/to/KF-Kiran.ttf");
-fs-pdf-font-embed: embed;
-fs-pdf-font-encoding: cp1250;
}
div {
font-family: 'KF-Kiran'; // here give the same name of .ttf file.
}
@font-face{src:url(${grailsApplication.config.app.serverUrl}/arialuni.ttf);-fs-pdf字体嵌入:嵌入;-fs-pdf字体编码:Identity-H;}
这对我很有用。请你用更详细的解释来扩展你的答案好吗?这对理解非常有用。非常感谢。单引号在CSS中,而不是groovy代码中,因此它们是单引号还是双引号并不重要quotes@Cookalino好的,你能给我解释一下到底是什么技术将呈现这个
'${grailsApplication.config.grails.serverURL}
?CSS?你否决了我的回答吗?作为对你评论的回应。。。如果单引号/双引号在大括号内,那么你会有一点,但它们在大括号外,因此不是groovy代码的一部分。代码将解析为CSS中的src:url('myServerUrl')
或src:url(“myServerUrl”)
。CSS不关心它们是单引号还是双引号。不幸的是,如果你的答案没有意义,那么它将被否决,从而使最好的答案上升到顶部。也许你可以删除你的答案以避免它被否决?@Cookalino问题是什么以及如何评估${grailsApplication.config.grails.serverURL}
?因为我认为这是它将计算为Config.groovy中设置的任何值。例如,如果Config.groovy将grailsApplication.Config.grails.serverURL
设置为http://www.myserver.com
然后海报的代码将计算为src:url('http://www.myserver.com/fonts/HelveticaNeueLTCom-BdCn.ttf');代码>