flySaucer生成pdf文件实现

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();     
}     
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值