Intellipaat 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.

2 Answers

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. 

0 votes
ago by (2.8k points)

Add the below dependency in your project.

implementation 'com.itextpdf:html2pdf:3.0.2'

Java Code :

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter; 

import com.itextpdf.kernel.geom.PageSize; 

import com.itextpdf.kernel.pdf.PdfDocument; 

import com.itextpdf.kernel.pdf.PdfWriter;

import java.io.ByteArrayOutputStream; 

import java.io.FileOutputStream;

import java.io.IOException; 

import java.nio.file.Files;

import java.nio.file.Path; 

public class HtmlToPdfConvertor 

public static void main(String[] args) throws IOException 

String html_Name = index.html; 

String outpuFileName = output.pdf; 

final String html_String = Files.readAllBytes(Path.of(html_Name)); 

try (FileOutputStream fos = new FileOutputStream(outputFileName)) 

{

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

PdfDocument doc = new PdfDocument(new PdfWriter(baos)); doc.setDefaultPageSize(PageSize.A4); 

ConverterProperties props = new ConverterProperties(); 

HtmlConverter.convertToPdf(html, doc, props); fos.write(baos.toByteArray()); 

}

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

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...