2012-10-10 110 views
8

我生成PDF使用Grails导出插件(基本上,飞碟)。我的GSP页面是一个UTF-8页面(或者至少属性显示它是UTF-8,在GSP页面的开头还有一个<?xml version="1.0" encoding="UTF-8"?>指令)。起初生成的PDF格式包含了变音符号“äöüõ”,但西里尔文字符从PDF中丢失(根本不显示)。然后我按照文档中所述添加以下内容来更改我的css文件:飞碟字体为Unicode字符

@font-face { 
    src: url(ARIALUNI.TTF); 
    -fs-pdf-font-embed: embed; 
    -fs-pdf-font-encoding: UTF-8; 
} 
body { 
     font-family: "Arial Unicode MS", Arial, sans-serif; 
} 

ArialUni.ttf也部署到服务器。但现在,我将变音字符和西里尔字符都变成了盒子。如果我将-fs-pdf-encoding属性值更改为Identity-H,则变音符字符将正确呈现,但西里尔字符会呈现为问号。

的可用于正确渲染两个元音和西里尔字母什么字体任何想法?或者可能是我的CSS有点不对?任何提示将不胜感激。

UPD 1: 我也尝试下面的CSS(其通过http://fontface.codeandmore.com/生成):

@font-face { 
    font-family: 'ArialUnicodeMS'; 
    src: url('arialuni.ttf'); 
    src: url('arialuni.eot?#iefix') format('embedded-opentype'), 
     url('arialuni.woff') format('woff'), 
     url('arialuni.ttf') format('truetype'), 
     url('arialuni.svg#arialuni') format('svg'); 
    font-weight: normal; 
    font-style: normal; 
    -fs-pdf-font-embed: embed; 
    -fs-pdf-font-encoding: UTF-8; 
} 

body { 
    font-family:'ArialUnicodeMS'; 
} 

我已经添加<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 我还试图运行的grails与-Dfile.encoding = UTF-8,正如这里所提到的:http://grails.1312388.n4.nabble.com/PDF-plugin-Having-problems-with-instalation-td2297840.html,但没有什么帮助。西里尔字符根本不显示。任何其他想法可能是什么问题?

* BTW: *我打包我的PDF作为拉链和类似的响应发送回到浏览器:

response.setHeader "Content-disposition", "attachment; filename=test.zip" 
response.setHeader "Content-Encoding", "UTF-8" 
response.contentType = 'application/zip' 
response.outputStream << zip 
response.outputStream.flush() 
response.outputStream.close() 

我需要以某种方式考虑编码而荏苒????,我做这样的:

public static byte[] zipBytes(Map<String, ByteArrayOutputStream> fileNameToByteContentMap) throws IOException { 
     ByteArrayOutputStream zipBaos = new ByteArrayOutputStream(); 
     ZipOutputStream zos = new ZipOutputStream(zipBaos); 
     fileNameToByteContentMap.eachWithIndex {String fileName, ByteArrayOutputStream baos, i -> 
      byte[] content = baos.buf 
      ZipEntry entry = new ZipEntry(fileName) 
      entry.setSize(content.length) 
      zos.putNextEntry(entry) 
      zos.write(content) 
      zos.closeEntry() 
     } 
     zos.close() 
     return zipBaos.toByteArray(); 
    } 
+0

如果你的内容类型定义为UTF-8呢? –

+0

您的意思是HTML内容类型? –

回答

6

出于某种原因,开始与下面的CSS和.TTF文件,这是由面部-KIT-发生器产生的工作:

@font-face { 
    src: url('arialuni.ttf'); 
    -fs-pdf-font-embed: embed; 
    -fs-pdf-font-encoding: Identity-H; 
} 

body { 
    font-family: Arial Unicode MS, Lucida Sans Unicode, Arial, verdana, arial, helvetica, sans-serif; 
    font-size: 8.8pt; 
} 

奇怪的事情是,如果我把字体到某个文件夹,让说的“字体”,会找到它的字体,但字符将不会被渲染。

+3

The -fs-pdf-font-encoding:Identity-H; 是关键;它告诉飞碟,这是一个Unicode字体,而不是一个字体,受限于某些代码页 – jaygooby

+0

把字体文件夹中我用'源:URL(“$ {}的baseUrl /assets/ARIALUNI.TTF”); '在哪里baseUrl通过'grailsLinkGenerator.serverBaseURL'在模型中传递 – fego

8

我设法“使能”的Unicode字符Java代码内(西里尔或捷克)并且还提供在我的资源(CALIBRI.TTF)True Type字体。

import org.w3c.dom.Document; 
import org.xhtmlrenderer.pdf.ITextRenderer; 
import com.lowagie.text.pdf.BaseFont; 

... 
    ITextRenderer renderer = new ITextRenderer(); 
    URL fontResourceURL = getClass().getResource("fonts/CALIBRI.TTF"); 
    //System.out.println("font-path:"+fontResourceURL.getPath()); 

    /* HERE comes my solution: */ 
    renderer.getFontResolver().addFont(fontResourceURL.getPath(), 
       BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 

    renderer.setDocument(doc, null); 
    renderer.layout(); 
    baos = new ByteArrayOutputStream(); 
    renderer.createPDF(baos); 
    baos.flush(); 
    result = baos.toByteArray(); 
... 

最后我加入了字体家族“宋体”我的文档的CSS部分:

... 
<style type="text/css"> 
    span { font-size: 11pt; font-family: Calibri; } 
... 
+1

我在使用这个解决方案时遇到了问题。我固定它通过使用'URL fontResourceURL =的getClass()的getResource( “/字体/ CALIBRI.TTF”);'(请注意在字体资源路径的初始斜线)和'fontResolver。addFont(fontResourceURL.toString(),BaseFont.IDENTITY_H,BaseFont.EMBEDDED);'(注意toString方法,而不是getPath) –