如何在java中将HTML文件转换为PDF文件

时间:2017-05-12 21:39:53

标签: java html pdf

我有一个html文件,我想将其内容转换为pdf文件 我尝试过XMLWorkerHelper,它对我不起作用。 对于这两个函数我收到此错误:java.lang.NoSuchMethodError:com.itextpdf.tool.xml.html.pdfelement.NoNewLineParagraph.set MultipliedLeading(F)V 这里有我使用的代码(我尝试了两个函数,在这两个函数中我遇到了XML get实例行的问题,变量HTML是html文件源代码):

public void converttopdf(String html){  
    try{
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("PDFs/convert.pdf"));
    document.open();
    XMLWorkerHelper.getInstance().parseXHtml(writer, document,new FileInputStream("HTMLs/test.html")); 
    document.close();
    System.out.println( "PDF Created!" );
    }catch(Exception e){
         e.printStackTrace();
    }
}

public void HtmlTOPdf(String html){
    try {
        FileOutputStream file = new FileOutputStream(new File("PDFs/convert.pdf"));
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, file);
        document.open();
        ByteArrayInputStream is = new ByteArrayInputStream(html.getBytes());
        XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);
        document.close();
        file.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

0 个答案:

没有答案