0

i am converting html document to pdf using htmlworker (itext library )as follow

String path = "temp.pdf";
PdfWriter pdfWriter = null;

// create a new document
Document document = new Document();
pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(
                    path));
document.open();
HTMLWorker htmlWorker = new HTMLWorker(document);
String str = "";
StringBuilder contentBuilder = new StringBuilder();
BufferedReader in = null;
try {
    in = new BufferedReader(new FileReader("temp1.html"));

     while ((str = in.readLine()) != null) {

           contentBuilder.append(str);

    }
} catch (IOException e) {
    System.out.print("HTML file close problem:" + e.getMessage());
} finally {
    in.close();

}
String content = contentBuilder.toString();

htmlWorker.parse(new StringReader(content));

document.close();

now my question is i am able to convert html to pdf using above code. but if my html page contain table tag it will create td with same size. how can i set width of td tag in generated pdf? thanks in advance

1 Answer 1

2

Finally i got Solution :

try {

            // get Instance of the PDFWriter
            pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(
                    path));
            //pdfWriter.setPdfVersion(PdfWriter.PDF_VERSION_1_4);
            pdfWriter.setLinearPageMode();
            pdfWriter.setFullCompression();


            // document header attributes
            document.addAuthor("");
            document.addCreationDate();
            document.addProducer();
            document.addCreator("aaa");
            document.addTitle("");
            document.setPageSize(PageSize.A4);

            // open document
            document.open();

            HTMLWorker htmlWorker = new HTMLWorker(document);

            String str = "";
            StringBuilder contentBuilder = new StringBuilder();
            BufferedReader in = null;

            System.out.println("Html Content :");
            try {
                in = new BufferedReader(new FileReader(
                        htmlfile));

                while ((str = in.readLine()) != null) {

                    contentBuilder.append(str);
                    System.out.println(str);
                }
            } catch (IOException e) {
                System.out.print("HTML file close problem:" + e.getMessage());
            } finally {

                in.close();
                System.gc();
            }
            String content = contentBuilder.toString();

            htmlWorker.parse(new StringReader(content));

            document.close();

            pdfWriter.close();


        }

        catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.