Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

I am trying to convert HTML to PDF using this code:

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();

        }

    }

}

I can convert it but in a 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.

1 Answer

0 votes
by (13.1k points)

You can do it with the HTMLWorker class 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, 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();

}

Want to learn Java? Check out the core java certification from Intellipaat. 

Related questions

0 votes
1 answer
asked Aug 14, 2019 in Web Technology by Sammy (47.6k points)
0 votes
1 answer
asked Aug 3, 2019 in Web Technology by Tech4ever (20.3k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...