如何使用iText将HTML转换为PDF [英] How to convert HTML to PDF using iText

查看:91
本文介绍了如何使用iText将HTML转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  import java.io.File; 
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class GeneratePDF {
public static void main(String [] args){
try {

String k =< html><< ; body>这是我的项目< / body>< / html>;

OutputStream file = new FileOutputStream(new File(E:\\Test.pdf));

Document document = new Document();
PdfWriter.getInstance(document,file);

document.open();

document.add(新的(k)段);

document.close();
file.close();

} catch(Exception e){

e.printStackTrace();



code
$ b这是我的代码转换HTML到PDF。我能够转换它,但在PDF文件中,它保存为整个HTML,而我只需要显示文本。 < HTML><身体GT;这是我的项目< / body>< / html> 获取保存到PDF,而它应该只保存这是我的项目您可以使用 text / html / simpleparser / HTMLWorker.htmlrel =nofollow noreferrer> HTMLWorker class(deprecated)like this:

  import com.itextpdf.text.html.simpleparser.HTMLWorker; 
// ...
try {
String k =< html>< body>这是我的项目< / body>< / html>;
OutputStream file = new FileOutputStream(new File(C:\\Test.pdf));
Document document = new Document();
PdfWriter.getInstance(document,file);
document.open();
HTMLWorker htmlWorker = new HTMLWorker(document);
htmlWorker.parse(new StringReader(k));
document.close();
file.close();
} catch(Exception e){
e.printStackTrace();
}

或使用 XMLWorker ,(从 this jar ): com.itextpdf.tool.xml.XMLWorkerHelper;
// ...
try {
String k =< html>< body>这是我的项目< / body>< / html>;
OutputStream file = new FileOutputStream(new File(C:\\Test.pdf));
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document,file);
document.open();
InputStream is = new ByteArrayInputStream(k.getBytes());
XMLWorkerHelper.getInstance()。parseXHtml(writer,document,is);
document.close();
file.close();
} catch(Exception e){
e.printStackTrace();
}


import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class GeneratePDF {
    public static void main(String[] args) {
        try {

            String k = "<html><body> This is my Project </body></html>";

            OutputStream file = new FileOutputStream(new File("E:\\Test.pdf"));

            Document document = new Document();
            PdfWriter.getInstance(document, file);

            document.open();

            document.add(new Paragraph(k));

            document.close();
            file.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
    }
}

This is my code to convert HTML to PDF. I am able to convert it but in PDF file it saves as whole HTML while I need to display only text. <html><body> This is my Project </body></html> gets saved to PDF while it should save only This is my Project.

解决方案

You can do it with the HTMLWorker class (deprecated) like this:

import com.itextpdf.text.html.simpleparser.HTMLWorker;
//...
try {
    String k = "<html><body> This is my Project </body></html>";
    OutputStream file = new FileOutputStream(new File("C:\\Test.pdf"));
    Document document = new Document();
    PdfWriter.getInstance(document, file);
    document.open();
    HTMLWorker htmlWorker = new HTMLWorker(document);
    htmlWorker.parse(new StringReader(k));
    document.close();
    file.close();
} catch (Exception e) {
    e.printStackTrace();
}

or using the XMLWorker, (download from this jar) using this code:

import com.itextpdf.tool.xml.XMLWorkerHelper;
//...
try {
    String k = "<html><body> This is my Project </body></html>";
    OutputStream file = new FileOutputStream(new File("C:\\Test.pdf"));
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, file);
    document.open();
    InputStream is = new ByteArrayInputStream(k.getBytes());
    XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);
    document.close();
    file.close();
} catch (Exception e) {
    e.printStackTrace();
}

这篇关于如何使用iText将HTML转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆