flySaucer生成pdf文件实现
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.pdf.BaseFont;
/**
* 用flySaucer生成pdf文件
* 引入jar包:
* flying-saucer-core-9.1.0.jar flying-saucer-pdf-9.1.0.jar
* itext-2.1.7.jar .ttc语言包 itextpdf-5.3.2.jar
* @version 1.0
* 创建时间:2016年12月2日 下午6:08:27
* 类说明
*/
public class TestFlySaucer {
// public static final String HTML = "/home/weblogic/test/amo.html";
// public static final String DEST = "/home/weblogic/test/";
// public static final String HTML = "E:\\tt_test\\amo.html";
// public static final String DEST = "E:\\tt_test\\";
// public static void main2(String[] args) {
// g();
// System.out.println("over!");
// }
// public static void g(){
// File parent = new File(DEST);
// //pdf保存路径不存在,则创建路径
// if(!parent.exists()){
// parent.mkdirs();
// }
//
// //生成pdf文件
createPdf( ebizAsset, ebizOrder, cusInfoDTO.getRealName());
// createPdf();
//
// }
// public static void createPdf() {
// try{
// //String tempHtml = DEST+ebizAsset.getOrderNo()+".html";
// InputStream is = new FileInputStream(HTML);
// // step 1
// Document document = new Document();
// //2.pdf生成路径
// String pdfPath = DEST+"t22.pdf";
// // step 2
// PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
// String cfontPath = "";
// // step 3
// BaseFont cbf = BaseFont.createFont(cfontPath,BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
// Font cfont = new Font(cbf,64);
// Phrase ph = new Phrase("中文测试",cfont);
// document.add(ph);
// document.open();
// // step 4
// XMLWorkerHelper.getInstance().parseXHtml(writer, document,
// is, Charset.forName("UTF-8"));
// // step 5
// document.close();
//
//
// }catch(Exception e){
// e.printStackTrace();
// }
// }
public static void main(String[] args) throws Exception {
// String inputFile = "E:\\tt_test\\amo2.html";
// String outputFile = "E:\\tt_test\\amo2.pdf";
String inputFile = "/home/weblogic/test/amo.html";
String outputFile = "/home/weblogic/test/amo.pdf";
String url = new File(inputFile).toURI().toURL().toString();
System.out.println(url);
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
// 解决中文支持问题
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont("/home/weblogic/test/msyh.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
// 解决图片的相对路径问题
// renderer.getSharedContext().setBaseURL("file:/D:/z/temp/");
renderer.layout();
renderer.createPDF(os);
os.close();
}
}