java画pdf

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.ArrayList;

import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.html.simpleparser.StyleSheet;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;


 

public class HtmlToPdf {
 
 


 public static void main(String[] args) {HtmlToPdf ih = new HtmlToPdf();  
   try {
    ih.htmlCodeComeFromFile("D://test.html", "D://1.pdf");
    //ih.htmlCodeComeString("<table width='100%' border='0'> <tr> <td>供应商名称_________供应商编号_________ 采购____年度___________ </td><td><table width='20%' border='1'> <tr><td>1</td><td>2</td> <td>3</td> </tr> <tr><td>4</td><td>5</td><td>6</td></tr></table></td> </tr></table>", "D://2.pdf");
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 
 }
 
 /**
  * 通过html文件生成pdf文件
  * @param filePath
  * @param pdfPath
  * @throws Exception
  */
 public void htmlCodeComeFromFile(String filePath, String pdfPath) throws Exception { 
        File f=new File(filePath);
        toPdf(f,pdfPath);
    } 
 
 
 /**
  * 通过html格式代码生成pdf
  * 注意:默认会生成临时html文件,读取后会自动删除
  * @param htmlCode
  * @param pdfPath
  * @throws Exception
  */
 public void htmlCodeComeString(String htmlCode, String pdfPath)  throws Exception{  
  String filename=System.currentTimeMillis()+Math.random()+".html";
  File f=new File(filename);
  BufferedOutputStream Buff = null; 

  FileOutputStream outSTr = null;  
  byte[] bs = htmlCode.getBytes();
  outSTr = new FileOutputStream(f);  
  Buff = new BufferedOutputStream(outSTr);  
  long begin0 = System.currentTimeMillis();  
  Buff.write(bs);  
  Buff.flush();  
  Buff.close(); 
  
  try {
   toPdf(f,pdfPath);
  }catch(Exception e){
   throw e;
  }finally {
   if(f.isFile()){
    f.delete();
   }
  }

 }

 
 private void toPdf(File file, String pdfPath) throws Exception{
         Document document = new Document();
         StyleSheet st = new StyleSheet(); 
         st.loadTagStyle("td", "encoding", "Identity-H");
      st.loadTagStyle("body", "encoding", "Identity-H");
      st.loadTagStyle("body", "leading", "6,0");
         
         st.loadTagStyle("body", "leading", "6,0"); 
         st.loadTagStyle("ol", "leading", "6.0");
         st.loadTagStyle("li", "face", "SIMHEI");
         st.loadTagStyle("span", "size", "6px");
         st.loadStyle("sf", "color", "blue");
         st.loadStyle("sf", "b", "");
         st.loadStyle("classic", "color", "red");
         st.loadStyle("classic", "i", "");
         st.loadTagStyle("td", "face", "SIMHEI");
         st.loadTagStyle("td", "leading", "18,0");
         st.loadTagStyle("td", "size", "10px");
         st.loadTagStyle("body", "face", "SIMHEI");
         st.loadTagStyle("body", "encoding", "Identity-H");
         st.loadTagStyle("body", "leading", "6,0");
             /*
         
             BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
             Font font = new Font(bf, 10);
             FontSelector selector = new FontSelector();
             selector.addFont(FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", false, 10));
            */
      FontFactory.registerDirectory(System.getenv("SystemRoot")+"//Fonts");
         BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);  
         Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);  
          
         PdfWriter.getInstance(document, new FileOutputStream(pdfPath)); 
         document.open(); 
         FileReader r= new FileReader(file);
        
         ArrayList p = HTMLWorker.parseToList(r, st); 
         for(int k = 0; k < p.size(); ++k) { 
          Paragraph myPara = new Paragraph();
          myPara.setFont(FontChinese);
          myPara.add((Element)p.get(k));
          document.add(myPara); 
         } 
         document.close();
         r.close();
     } 

 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值