日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 生成pdf 乱码_利用java处理fop导出pdf的中文乱码问题解决方案

發布時間:2024/9/27 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 生成pdf 乱码_利用java处理fop导出pdf的中文乱码问题解决方案 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文的作用是,生成帶中文的加密pdf格式的文件,防止被人修改。

在項目下建立docbook-xsl, fo-res, out, sample四個文件夾

docbook-xsl目錄: 放從sourceforge上下載的docbook-xsl.zip解壓的文件

fo-res目錄: 放一些apache fop需要配置的文件(下面會說到)

out目錄: 放導出結果

sample目錄: 放xml模板

從apache fop項目下載fop,目前是fop0.95, 只要下載binary版就行了,后面的代碼都是以fop0.95進行測試的。

解壓后將build目錄下的fop.jar以及lib目錄下的包都加到你項目里,然后將conf下的fop.xconf配置文件放到fo-res目錄下,記住這個文件,等會用它。

以宋體 為例,解決中文亂碼問題:

第一步

private static void test1() {

String[] parameters = {

“-ttcname”,

“SimSun”,

“c:\\WINDOWS\\Fonts\\simsun.ttc”, “E:\\project-java\\p-eclipse\\newtest\\fo-res\\simsun.xml”, };

TTFReader.main(parameters);

}

執行test1方法,會在fo-res目錄下生成一個simsun.xml

常用的中文字體:

simsun.ttc 宋體

simkai.ttf 宋楷

simhei.ttf 黑體

第二步、修改fo-res目錄下的fop.xconf文件,在fonts節點下加入或替換下列內容

metrics-url后面的值可以是絕對路徑;

經常我們的項目會部署到linux下,沒有simsun.ttc文件怎么辦呢,只需要把windows下的這個文件拷貝到linux就可以了。

第三步、把模板中使用的宋體字體 修改為font-family =SimSun。

第四步、執行下面的方法生成pdf;

private static void test2(){

try {

System.out.println(“Preparing…”);

File baseDir = new File(“.”);

File outDir = new File(baseDir, “out”);

outDir.mkdirs();

// Setup input and output files

File xmlfile = new File(baseDir, “/fo-res/p.xml”);

File xsltfile = new File(baseDir, “/fo-res/p.xsl”);

File pdffile = new File(outDir, “/sample.pdf”);

File conffile = new File(baseDir, “/fo-res/fop.xconf”);

System.out.println(“Input: XML (” + xmlfile + “)”);

System.out.println(“Stylesheet: ” + xsltfile);

System.out.println(“Output: PDF (” + pdffile + “)”);

System.out.println();

System.out.println(“Transforming…”);

FopFactory fopFactory = FopFactory.newInstance();

FOUserAgent foUserAgent = fopFactory.newFOUserAgent();

/* 1. userPassword: String, may be null

2. ownerPassword: String, may be null

3. allowPrint: true if printing is allowed

4. allowCopyContent: true if copying content is allowed

5. allowEditContent: true if editing content is allowed

6. allowEditAnnotations: true if editing annotations is allowed

*/

foUserAgent.getRendererOptions().put(“encryption-params”, new PDFEncryptionParams(

null, “password”, true, false, false, false));

// Setup output

OutputStream out = new java.io.FileOutputStream(pdffile);

out = new java.io.BufferedOutputStream(out);

try {

//加載配有中文配置的文件

fopFactory.setUserConfig(conffile);

Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);

// Setup XSLT

TransformerFactory factory = TransformerFactory.newInstance();

Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));

// Setup input for XSLT transformation

Source src = new StreamSource(xmlfile);

Result res = new SAXResult(fop.getDefaultHandler());

// Start XSLT transformation and FOP processing

transformer.transform(src, res);

} finally {

out.close();

}

System.out.println(“Success!”);

} catch (Exception e) {

e.printStackTrace();

}

}

總結

以上是生活随笔為你收集整理的java 生成pdf 乱码_利用java处理fop导出pdf的中文乱码问题解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。