简体   繁体   English

在 Grails 的 PDF 渲染插件中嵌入字体

[英]Embed font in PDF rendering plugin in Grails

I want to embed 'HelveticaNeueLTCom-BdCn.ttf' in a PDF document.我想在 PDF 文档中嵌入“HelveticaNeueLTCom-BdCn.ttf”。 I'm using Grails rendering 0.4.4 Plugin to generate PDF file.我正在使用 Grails 渲染 0.4.4 插件来生成 PDF 文件。 I tried following,我试着跟随,

@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;
}

but it doesn't work.但它不起作用。

The font embedding requires the below steps to be followed.字体嵌入需要遵循以下步骤。 This worked for me.这对我有用。 Try and tell me your feedback试着告诉我你的反馈

  1. The PdfRenderingService class present inside the plugin should be edited for this font simulation as below.插件中存在的 PdfRenderingService 类应针对此字体模拟进行编辑,如下所示。

     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(); }
  2. Add the below code in your template file在模板文件中添加以下代码

     @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(${grailsApplication.config.app.serverUrl}/arialuni.ttf) ; -fs-pdf-font-embed: embed; -fs-pdf-font-encoding: Identity-H; }

这对我有用。

Probably the problem is that you have your url value surrounded by ' instead of " .可能问题是你的url值被'而不是"包围。

The difference between them is that, though in Groovy string literals can be made with both, only the ones surrounded by " create GString, which evaluates statements between ${}它们之间的区别在于,虽然在 Groovy 中可以同时使用这两种字符串文字,但只有被" create GString 包围的文字才能对${}之间的语句求值

This worked for me这对我有用

@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.
}

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

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