1
<tr><td style='border-bottom:1px solid #333;'><p style='font-family:arial; font-size:8px; margin:0px 0px 10px 0px;  line-height:10px;'><strong>Customer Details </strong><br>Lawakush Kurmi<br>8285998390<br>lkurmi@craterzone.com<br> </p></td><td valign='top' style='font-family:arial; font-size:8px;  text-align:right; border-bottom:1px solid #333;'>&nbsp;</td></tr>

I am trying to convert these html code in pdf by using i-text library.But border-bottom:1px is not drawing any border in pdf. Please suggest what is the best option to draw horizontal lines borders in pdf using html.
NOTE : I am using HTMLWorker to convert HTML to PDF page.

Lawakush Kurmi
  • 2,726
  • 1
  • 16
  • 29
  • The behavior you describe is typical for the deprecated `HTMLWorker`. You should use XML Worker instead. I'm downvoting the question for now because you're not showing what you're using, `HTMLWorker` or XML Worker. See http://developers.itextpdf.com/faq/category/parsing-xml-and-xhtml and discover that table borders do work. – Bruno Lowagie Dec 27 '16 at 22:04
  • Yes I am using HTMLWorker it 's my mistake, But the fact is HTMLWorker is not supporting tb borders and so many attributes of html. So can you please suggest me the best way to convert html to pdf with open source library. – Lawakush Kurmi Dec 28 '16 at 06:21
  • I have posted an answer that proves that my previous comment is correct. iText's XML Worker supports `border` in CSS. You confirm that using `HTMLWorker` is a mistake, and I explained that `HTMLWorker` has been abandoned in favor of XML Worker, so why aren't you using XML Worker? – Bruno Lowagie Dec 28 '16 at 08:37

1 Answers1

1

Allow me to reuse the code from iTextSharp add ( css style or a css file) and download pdf file and slightly change some values:

public static final String CSS = "th { border-top: 5px solid green; } "
    + "td { font-size: 10pt; border-color: gray; border: 3px}";
public static final String HTML = "<html><body><table  class='table-bordered'>"
    + "<thead><tr><th>Customer Name</th><th>Customer's Address</th> </tr></thead>"
    + "<tbody><tr><td> XYZ </td><td> Bhubaneswar </td></tr>"
    + "<tr><td> MNP </td><td> Cuttack </td></tr></tbody>"
    + "</table></body></html>";

/**
 * @param file
 * @throws IOException
 * @throws DocumentException
 */
public void createPdf(String file) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
    document.open();


    CSSResolver cssResolver = new StyleAttrCSSResolver();
    CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(CSS.getBytes()));
    cssResolver.addCss(cssFile);

    // HTML
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

    // Pipelines
    PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
    HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
    CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

    // XML Worker
    XMLWorker worker = new XMLWorker(css, true);
    XMLParser p = new XMLParser(worker);
    p.parse(new ByteArrayInputStream(HTML.getBytes()));
    document.close();
}

The resulting PDF looks like this:

enter image description here

If you look at the CSS, you see that we defined the border for the <th> tag as border-top: 5px solid green; and the border for the <td> tag as font-size: 10pt; border-color: gray; border: 3px. This proves that iText supports CSS for table borders.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Hi i have copied your same code but still border-top is not working i can share you screenshot also. So can you please suggest me the atcual problem behind it why border-bottom and border-top is not working. For more information my system configuration is, OS : ubuntu, java-version : 1.8, project type maven (Dropwizard framework) – Lawakush Kurmi Dec 29 '16 at 07:48
  • 1
    Maybe you're not using the latest version of iText and XML Worker. – Bruno Lowagie Dec 29 '16 at 07:54
  • Yes i have upgraded xmlworker version to 5.5.9 and now it's working fine. Thank you So much for the help. thanks a lot !!! – Lawakush Kurmi Dec 29 '16 at 12:00