iTextPDF の XMLWorkerHelper を使用して PDF ファイルを生成しようとしています。もともと、私は Apache Velocity Template を使用して HTML コードを生成しています (したがって、XMLWorkerHelper が使用されています) が、この質問では、日本語の文字を含む単純な HTML 行を提供します。
public class iTextJapChars {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileOutputStream fos = new FileOutputStream("iTextJapChars.pdf");
(new iTextJapChars()).generate(fos);
fos.close();
System.out.println("done!");
}
public void generate(OutputStream os) throws IOException
{
// create a document
Document document = new Document();
try
{
String content = "<html><head></head><body><div><p>begin てすと end</p></div></body></html>";
InputStream stream = new ByteArrayInputStream(content.getBytes("UTF-8"));
System.out.println(content);
System.out.println(Charset.defaultCharset());
// create a writer with an output stream passed as an argument
PdfWriter writer = PdfWriter.getInstance(document, os);
document.addTitle("身 元 保 証 書");
document.addAuthor("れお");
document.addSubject("");
document.addKeywords("iText, PDF");
document.addCreator("test.iTextPDF.generate() using iText");
document.open(); // open the document now
XMLWorkerHelper.getInstance().parseXHtml(writer, document, stream);
}
catch (DocumentException de) { throw new IOException(de.getMessage()); }
finally { document.close(); } // close the document
}
問題は、結果の PDF ファイルに日本語の文字が表示されないことです。空白しか表示されません。以下に注意事項を示します。
- CentOS 6.4
- iTextPDF 5.4.3
- XML ワーカー 5.4.1
- アドビ リーダー 9.5.5
1 週間以上ネットを検索しており、XMLWorkerHelper を使用して動作するソリューションが得られないため、ここで私を助けてくれることを願っています。言及し忘れた情報がある場合、または間違った情報を言及した場合は、更新できるようにお知らせください。どんな助けでも大歓迎です。
どうもありがとうございました。