温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

html页面转换成PDF文件

发布时间:2020-06-14 05:24:50 来源:网络 阅读:613 作者:robinmars 栏目:开发技术
package pdftest;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import com.lowagie.text.pdf.BaseFont;

/**
 * 将html页面转换成PDF文件
 * 应用包 itext-2.0.8.jar core-renderer-R8.jar
 * 不支持中文,若支持中文需对内容进行改动
 *
 */
public class RendererTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		try{

			ITextRenderer renderer = new ITextRenderer();  
			
//			String blogURL = "http://;
			String blogURL = "http://";
			//指定模板地址              
//			renderer.setDocument("http://"); 
			
			//根据网址,获取网页内容
			String htmlBody = getHtmlContent(blogURL);
			
			//将网页内容进行格式校验及调整,以便于renderer能够解析,根据内容调整
			htmlBody = htmlBody.replaceAll("<br>", "<br/>");
			htmlBody = "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"zh-CN\"><head>"+htmlBody.substring(htmlBody.indexOf("-->")+3);
			htmlBody = htmlBody.substring(0, htmlBody.indexOf("</html>")+7);
			htmlBody = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" + htmlBody;

			System.out.println(htmlBody);
			
			//将调整完内容设置到renderer中
			renderer.setDocumentFromString(htmlBody);
			
			//字符处理,本处需要处理中文
			ITextFontResolver fontResolver = renderer.getFontResolver();  
//			if (StringUtils.isOSWindow())  
//				fontResolver.addFont("C:/Windows/Fonts/ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);  
//			else  
				fontResolver.addFont("/leo-work/workspace/mytest/lib/msyh.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);  
			renderer.layout();  

			String pdfFile2 = "/Users/leo/Desktop/a6.pdf";
//			OutputStream os = response.getOutputStream();      //输出到网页
			OutputStream os = new FileOutputStream(pdfFile2);  //输出到文件
			
			//根据renderer内容,创建PDF文件
			renderer.createPDF(os); 
			
			//关闭输出柳
			os.close(); 
            
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
	}

	/**
	 * 通过网址获取网页内容
	 * @param htmlurl
	 * @return
	 */
	public static String getHtmlContent(String htmlurl) {  
        URL url;  
        String temp;  
        StringBuffer sb = new StringBuffer();  
        try {  
            url = new URL(htmlurl);  
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));// 读取网页全部内容  
            while ((temp = in.readLine()) != null) {  
                sb.append(temp);  
            }  
            in.close();  
        } catch (final MalformedURLException me) {  
            System.out.println("你输入的URL格式有问题!");  
            me.getMessage();  
        } catch (final IOException e) {  
            e.printStackTrace();  
        }  
        return sb.toString();  
    }  

}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI