由HTML生成PDF

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)

猜你喜欢

转载自blog.csdn.net/wsgytwsgyt/article/details/79354551