2012-10-10 108 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) –