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(); }
}