練功房推薦書單

  • 猛虎出柙雙劍合璧版--最新 OCA / OCP Java SE 7 Programmer 專業認證 (電子書)
  • 流浪教師存零股存到3000萬(全新增修版)(書+DVD)
  • 開始在關西自助旅行(京都‧大阪‧神戶‧奈良)(全新增訂版)
  • 不敗教主的300張股票存股術

iText在產生pdf檔時,遇到一些特殊中文字(例如:珉、峯、喆等),都會有unicode字碼(&#xxxxx;)輸出,該怎麼辦? RSS feed
討論區首頁 » 網頁程式設計 Web Development
發表人 內容
collectWu

十級學員
[Avatar]

註冊時間: 2007/6/14
文章: 2
離線
我的資料庫裡頭儲存是Big5資料,想從資料庫SELECT一些資料,然後用iText來產生pdf檔,但遇到一些特殊中文字時,就會產生如附件(樣本檔)所示錯誤現象,請問版主該如何解決?謝謝!
 檔案名稱 sample.pdf [Disk] 下載
 描述 樣本檔
 檔案大小 23 Kbytes
 下載次數:  11 次

andowson

七段學員
[Avatar]

註冊時間: 2007/1/2
文章: 711
來自: 台北
離線
我猜測這個現象會發生的原因可能如下,
1.如果資料庫內碼是使用Big5編碼時,透過網頁上輸入的中文字(UTF-8)進入到應用伺服器時,被轉了一次碼(ISO-8859-1),再存到資料庫時,又轉了一次碼(Big5),例如下列的順序:
UTF-8->ISO-8859-1->Big5
當UTF-8轉成ISO-8859-1時,Unicode被轉成10進制的HTML Entity格式(如珉),然後跟其他可以轉成Big5的字元一起存到資料庫去。

2.等到從資料庫取回這個字串時,就會得到一個夾雜著HTML Entity格式的Big5字串,如果把這個字串直接傳給iText 去產生 pdf 檔,這些HTML Entity字串應該就會當做英文字母一樣輸出。於是就產生類似您所附上的sample.pdf 般的內容了。

好,了解原因後,要解決這個問題就很簡單了,只要在步驟2時補上將HTML Entity字串轉碼回原始編碼,再傳給iText去產生pdf即可。而要將HTML Entity字串轉碼回原始編碼只要呼叫
Apache Commons LangStringEscapeUtils.unescapeHtml(java.lang.String) method即可。

在此以修改iText in Action 2nd Edition所提供的HelloWorld.java作為範例
HelloWorld.java:

/*
* This class is part of the book "iText in Action - 2nd Edition"
* written by Bruno Lowagie (ISBN: 9781935182610)
* For more info, go to: http://itextpdf.com/examples/
* This example only works with the AGPL version of iText.
*/

package com.andowson.pdf;

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.lang.StringEscapeUtils;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

/**
* First iText example: Hello World.
*/
public class HelloWorld {

/** Path to the resulting PDF file. */
public static final String RESULT
= "D:/hello.pdf";

/**
* Creates a PDF file: hello.pdf
* @param args no arguments needed
*/
public static void main(String[] args)
throws DocumentException, IOException {
new HelloWorld().createPdf(RESULT);
}

/**
* Creates a PDF document.
* @param filename the path to the new PDF document
* @throws DocumentException
* @throws IOException
*/
public void createPdf(String filename)
throws DocumentException, IOException {
// step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();

// Assumes that source string comes from database
String source = "測試物品-珉彣峯喆献";
// Unescapes the Unicode characters from HTML entities (decimal format) to string
String output = StringEscapeUtils.unescapeHtml(source);

// Specifies the file path to the Chinese font file
BaseFont bfChinese = BaseFont.createFont("C:/Windows/Fonts/kaiu.ttf",
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font font = new Font(bfChinese, 12); // The font size is 12.

// step 4
document.add(new Paragraph(output, font));
// step 5
document.close();
}
}


下載函式庫:
http://itextpdf.com/
http://commons.apache.org/lang/

參考網址:
* Unicode 編碼字集
http://theorem.ca/~mvcorks/cgi-bin/unicode.pl.cgi?start=4E00&end=9FFF
* 解決iText輸出中文問題
http://blog.yam.com/rexmen/article/1888474
* Hello World example
http://itextpdf.com/examples/iia.php?id=12
 檔案名稱 HelloWorld.java [Disk] 下載
 描述 產生PDF的範例檔
 檔案大小 2 Kbytes
 下載次數:  5 次

 檔案名稱 hello.pdf [Disk] 下載
 描述 產生的範例pdf檔
 檔案大小 23 Kbytes
 下載次數:  7 次


分享經驗 累積智慧
[WWW]
 
討論區首頁 » 網頁程式設計 Web Development
前往:   
行動版