1

I have an html file, which is generated as a response by my site. While opening the html file in my browser, all the images and other elements are displaying properly. Moreover, I want to convert this html file into a pdf file. So am using iText API to generate the pdf file. In the html file images and texts are located horizontally.

But in the generated pdf output the images are displaying vertically.

code

 String k = convertString();
            OutputStream file = new FileOutputStream(new File("f:\\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();

private  static String convertString() {
        StringBuilder contentBuilder = new StringBuilder();
        try {
            BufferedReader in = new BufferedReader(new FileReader("f://testPage.html"));
            String str;
            while ((str = in.readLine()) != null) {
                contentBuilder.append(str);
            }
            in.close();
        } catch (IOException e) {
        }
        String content = contentBuilder.toString();
        return content;
    }

How can I generate a pdf that looks exactly the same as the appearance of the html UI? Also, in Jfiddle, the html page is not displaying properly. What am I missing? Is the problem with my HTML, or is there some deeper issue with Jfiddle or something else at work?

Psl
  • 3,830
  • 16
  • 46
  • 84
  • 1
    I see that you are using `HTMLWorker`. That has been deprecated, you should use `XMLWorker`. Also, which version of iText are you using? – Amedee Van Gasse May 25 '16 at 16:06
  • i tried using itextpdf 5.5.4 and itext 5.4.2 same output is getting – Psl May 25 '16 at 16:08
  • tried with xmlworker also – Psl May 25 '16 at 16:10
  • You should still use `XMLWorker`. And try with iText 5.5.9. Or iText 7.0.0. – Amedee Van Gasse May 25 '16 at 16:10
  • ok i will try with that version and let u know. – Psl May 25 '16 at 16:12
  • I see that you edited your question and are using `XMLWorker` now. I will leave it to others to answer your question. – Amedee Van Gasse May 25 '16 at 16:12
  • one more doublt is this issue is due to any css style in the html page???i think styles are not properly applied in the generated pdf that y it is arranged vertically. i dont know my observation is true or not..please advice – Psl May 25 '16 at 16:13
  • yes i used XMLWorker also,,,,,But using the itextpdf version 5.5.4.. i have to try with version 5.5.9 also – Psl May 25 '16 at 16:14
  • If you have doubts then you should ask questions! One more thing, use the W3C Validator to check that your generated page is valid HTML. iText may do unexpected things with invalid HTML. – Amedee Van Gasse May 25 '16 at 16:16

0 Answers0