由HTML生成PDF
阿新 • • 發佈:2018-11-02
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.MalformedURLException; import javax.xml.parsers.ParserConfigurationException; import org.xhtmlrenderer.pdf.ITextFontResolver; import org.xhtmlrenderer.pdf.ITextRenderer; import org.xml.sax.SAXException; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Font; import com.lowagie.text.Image; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfPCell; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfWriter; public class IText { public static void main(String[] args) throws Exception { test(); } public static void test() throws ParserConfigurationException, SAXException, IOException, DocumentException { String inputFile = "E:\\html\\c.html"; String outputFile = "E:\\html\\test.pdf"; InputStreamReader fr=new InputStreamReader(new FileInputStream(inputFile),"UTF-8"); BufferedReader br = new BufferedReader(fr); StringBuffer sb = new StringBuffer(); String s; while ((s = br.readLine()) != null) { sb.append(s); } fr.close(); ITextRenderer renderer = new ITextRenderer(); ITextFontResolver fontResolver = renderer.getFontResolver(); fontResolver.addFont("E:/fonts/msyh.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); renderer.setDocumentFromString(sb.toString()); OutputStream os = new FileOutputStream(outputFile); renderer.layout(); renderer.createPDF(os); os.close(); } private static void test2() throws MalformedURLException, IOException, DocumentException { Document doc = new Document (PageSize.A4); PdfWriter.getInstance (doc, new FileOutputStream ("e:/html/test.pdf")); doc.open (); //標題字型 BaseFont bfTitle = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font titleFont = new Font(bfTitle, 18, Font.NORMAL); //內容字型 BaseFont bfComic = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font font = new Font(bfComic, 9, Font.NORMAL); Paragraph titleP=new Paragraph("兒童資訊 Child Information\n\n",titleFont); doc.add(titleP); //生成4列的表格 PdfPTable table = new PdfPTable (4); table.setWidthPercentage(100); table.setWidthPercentage(100); table.addCell (new Paragraph ("Children-id",font)); PdfPCell cell = new PdfPCell (new Paragraph ("09140800002",font)); cell.setColspan (3); table.addCell (cell); // 新增第一行 table.addCell (new Paragraph ("Name(CN)",font)); table.addCell (new Paragraph ("黨寧生",font)); table.addCell (new Paragraph ("Name(EN)",font)); table.addCell (new Paragraph ("DANG NING SHENG",font)); //新增第二行 table.addCell (new Paragraph ("Sex(CN)",font)); table.addCell (new Paragraph ("男",font)); table.addCell (new Paragraph ("Sex(EN)",font)); table.addCell (new Paragraph ("MALE",font)); //新增第8行 table.addCell (new Paragraph ("Note",font)); cell = new PdfPCell (new Paragraph ("兒童資料",font)); cell.setColspan (3); table.addCell (cell); //新增第9行 table.addCell (new Paragraph ("Pictures",font)); Image photo=Image.getInstance("c:/test/pdf/1246588678828.jpg"); cell = new PdfPCell (photo); cell.setColspan (3); table.addCell (cell); doc.add (table); doc.newPage(); doc.close (); } public static void test3() throws DocumentException, IOException { String inputFile = "E:\\html\\a.html"; String outputFile = "E:\\html\\a.pdf"; OutputStream os = new FileOutputStream(outputFile); ITextRenderer renderer = new ITextRenderer(); String url = new File(inputFile).toURI().toURL().toString(); renderer.setDocument(url); // 解決中文支援問題 ITextFontResolver fontResolver = renderer.getFontResolver(); fontResolver.addFont("E:/fonts/msyh.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); // 解決圖片的相對路徑問題 // renderer.getSharedContext().setBaseURL("file:///E:/workspace/PDF/WebContent/WEB-INF/"); renderer.layout(); renderer.createPDF(os); os.flush(); os.close(); System.out.println("轉換完成!"); } }
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>9.1.11</version>
</dependency>
需求:有一html頁面,需要將其轉化為pdf檔案。
html的生成可以不用關注,只需注意轉化的問題。
最簡單的方式就是使用Flying-Saucer,使用MAVEN官方庫的包就可以了。可以解決CSS的支援。
但是呢,對於中文和一些特殊字元的支援不好,需要我們手動的引入一個字型檔案,也可以在網上找一個網友自己修改的asian包來解決。
然後在html中對使用到中文和特殊字元的地方寫好字型。html中的字型名需要和java中的字型名相同。
tips:可以在doc中敲出這個字元,然後看doc用的是什麼字型,再去系統字型庫中找到該字型檔案。
對於某些html的頁面大小比較特殊,可以再style中加入 @page{size:297mm 210mm;}來控制生成pdf的頁面大小。
對於本地的html檔案,使用的是setDocument(url)。
對於接受的引數html,可以讀取為String 然後setDocumentFromString(String)。
也可以使用lowagie的包,自己去拼接pdf檔案(test3)