html文件转换成pdf和word

1、html文件转成pdf

采用jar包有itext-asian.jar、itextpdf-5.5.5.jar、itext-pdfa-5.5.5.jar、itext-xtra-5.5.5.jar,为了保持html页面的全部格式,需要进行相关设置,代码如下:

private boolean convert2Pdf(InputStream htmlInputStream, String pdfFile, String padding) throws FileNotFoundException, DocumentException  {
        String[] paddings = padding.split(",");
        float a1 = (float) (Float.valueOf(paddings[0]) * 0.77);  //为了设置pdf的上下左右页边距
        float a2 = (float) (Float.valueOf(paddings[1]) * 0.77);
        float a3 = (float) (Float.valueOf(paddings[2]) * 0.77);
        float a4 = (float) (Float.valueOf(paddings[3]) * 0.77);
        com.itextpdf.text.Document document = new com.itextpdf.text.Document(
                PageSize.A4, a1, a2, a3, a4);
        PdfWriter pdfwriter = PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
        
        document.open();
        CSSResolver cssResolver = new StyleAttrCSSResolver();
        
        // HTML
        XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
        fontProvider.register("resources/garial.ttf", "Linrial");  //前面的ttf字体需要系统支持,后一个参数是html文件中的字体格式
        fontProvider.register("resources/fzst_gb18030_20101201.ttf", "FZSongTi_GB18030");
        fontProvider.register("resources/fzfangsong.ttf", "FZFangSong");
        fontProvider.register("resources/fzheiti.ttf", "FZHeiTi");
        fontProvider.register("resources/fzkaiti.ttf", "FZKaiTi");
        fontProvider.register("resources/gtimes.ttf", "LinTimes");
        CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
        htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
        
        final String IMG_PATH = pdfFile.substring(0, pdfFile.indexOf("/temp")) + "temp/";  //指定html文件的图片路径
        htmlContext.setImageProvider(new AbstractImageProvider() {
            public String getImageRootPath() {
                return IMG_PATH;
            }
        });
        // Pipelines
        PdfWriterPipeline pdf = new PdfWriterPipeline(document, pdfwriter);
        HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
        CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
        
        // XML Worker
        XMLWorker worker = new XMLWorker(css, true);
        XMLParser p = new XMLParser(worker);
        try {
            p.parse(htmlInputStream, Charset.forName("UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            document.close();
        }
        return true;
    }

使用方式

convert2Pdf(new ByteArrayInputStream(html.getBytes()), "/root/123.pdf", padding),其中html为html文件,需要转换为inputstream

2、html转为word

采用openoffice或者中标office,需要最新版本,jar包为jodconverter-2.2.2.jar、jodconverter-cli-2.2.2.jar需要把openoffice和web服务安装在同一个机器上

    private boolean startOfficeService() {
        final String paramStr = "\'socket,host=localhost,port=8100;urp;\'";
        String pro = "/opt/neoshineoffice/program/soffice.bin -headless -accept="
                + paramStr;
        System.out.println("---中标Office后台服务启动中...>>>");
        try {
            Runtime.getRuntime().exec(new String[] { "/bin/sh", "-c", pro }); // 创建一个office服务进程;
        } catch (Exception e) {
            System.out.println("中标Office后台服务启动失败<<<");
            e.printStackTrace();
            return false;
        }
        System.out.println("中标Office后台服务启动成功<<<");
        return true;
    }
    private com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection connectOfficeService() {
        System.out.println("开始准备连接OFFICE服务...>>>");
        com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection connection = new com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection(
                "localhost", 8100);
        Boolean flag = false;
        int count = 0; // 初始连接OFFICE失败后再次连接的次数;
        while (!flag) {
            try {
                count++;
                Thread.sleep((count + 1) * 1000);
                connection.connect();
                flag = true;
                System.out.println(connection);

            } catch (Exception e) {
                System.out.println("OFFICE服务连接失败<<<");
                System.out.println("第" + count + "次重启OFFICE服务连接");
                if (count > 5) { // 连接OFFICE失败5次后,停止连接;
                    e.printStackTrace();
                    System.out.println("-----无法启动OFFICE服务连接,请检查OFFICE安装情况!----错误信息:"+ e.getMessage());
                    return null; 
                }
            }
        }
        System.out.println("OFFICE服务连接成功<<<");
        return connection;
    }
if (startOfficeService()) {
                    OpenOfficeConnection conn = connectOfficeService();
                    if (conn != null) {
                        DocumentConverter convert = new OpenOfficeDocumentConverter(conn);
                        convert.convert(new File(rootPath + "/temp/resulted.html"), new File(rootPath + "/temp/" + reportName + ".doc"));
                        conn.disconnect();
                    }
                }

 

posted @ 2015-05-28 21:00  飞天0407  阅读(2368)  评论(0编辑  收藏  举报