0

Finally got it working

var inputString = @"<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>";


List<string> cssFiles = new List<string>();
cssFiles.Add(@"/Content/bootstrap.css");

var output = new MemoryStream();

var input = new MemoryStream(Encoding.UTF8.GetBytes(inputString));

var document = new Document();
var writer = PdfWriter.GetInstance(document, output);
writer.CloseStream = false;

document.Open();
var htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(iTextSharp.tool.xml.html.Tags.GetHtmlTagProcessorFactory());

ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
cssFiles.ForEach(i => cssResolver.AddCssFile(System.Web.HttpContext.Current.Server.MapPath(i), true));

var pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));
var worker = new XMLWorker(pipeline, true);
var p = new XMLParser(worker);
p.Parse(input);
document.Close();
output.Position = 0;


Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=myfile.pdf");
Response.BinaryWrite(output.ToArray());
// myMemoryStream.WriteTo(Response.OutputStream); //works too
Response.Flush();
Response.Close();
Response.End();
Arun Prasad E S
  • 9,489
  • 8
  • 74
  • 87
  • @AnupSharma I want to decrease the font size of the table inside. – Arun Prasad E S Dec 27 '16 at 08:13
  • 1
    `HTMLWorker` was abandoned in favor of XML Worker. `HTMLWorker` doesn't support CSS (and will never support CSS as its use has been discontinued). Read more about [XML Worker](http://developers.itextpdf.com/faq/category/parsing-xml-and-xhtml) on the official iText site. – Bruno Lowagie Dec 27 '16 at 08:13
  • @BrunoLowagie I have gone through that question already. Couldn't solve my issue. – Arun Prasad E S Dec 27 '16 at 08:26
  • You can use :- static Font FontSubHeading = FontFactory.GetFont("gothic", 10, Font.NORMAL, new BaseColor(System.Drawing.ColorTranslator.FromHtml("#404041").ToArgb())); Paragraph DearName = new Paragraph("test", FontSubHeading); document.Add(DearName); – Anand Systematix Dec 27 '16 at 08:29
  • You aren't showing your HTML + CSS, so it's impossible to answer your question. See for instance [How to convert an HTML table to PDF?](http://developers.itextpdf.com/question/how-convert-html-table-pdf) where you have the HTML, the CSS and the resulting PDF. Saying "it doesn't work" or "Couldn't solve my issue" without explaining what doesn't work or what the issue is, usually makes it impossible for people to help. – Bruno Lowagie Dec 27 '16 at 08:48
  • Also: if you really had read the answer to the duplicate question, you wouldn't be using `HTMLWorker`. You'd be using XML Worker instead. – Bruno Lowagie Dec 27 '16 at 08:49
  • @BrunoLowagie the html content just have a table inside, just a method to add any css would solve my issue. – Arun Prasad E S Dec 27 '16 at 08:51
  • 1
    Well, show us the HTML and the CSS. There are **plenty of working examples** on [the web site](http://developers.itextpdf.com/faq/category/parsing-xml-and-xhtml). However, using `HTMLWorker` as is done in your question **will not work**. Change your question so that I can see that your using XML Worker and I might vote to reopen it. However, in its current state, the answer to your question is: what you want doesn't work with `HTMLWorker`, use XML Worker instead. (That requires an extra DLL next to the iTextSharp DLL.) – Bruno Lowagie Dec 27 '16 at 08:55
  • @BrunoLowagie I am working on adding XmlWorker method, if with a simple fix, this can be made working. it will solve – Arun Prasad E S Dec 27 '16 at 08:55
  • 1
    I don't see you working with XML Worker. Update your question to show your XML Worker code and also show your HTML and CSS. – Bruno Lowagie Dec 27 '16 at 08:56
  • @BrunoLowagie can you please take a look at this. – Arun Prasad E S Dec 27 '16 at 09:17
  • 1
    The input isn't valid HTML. – Bruno Lowagie Dec 27 '16 at 09:27
  • @BrunoLowagie Got is working thanks. – Arun Prasad E S Dec 27 '16 at 10:13
  • OK, I reopened the question (although it's no longer a question now) and I posted the code I wrote to test the HTML. – Bruno Lowagie Dec 27 '16 at 10:31

1 Answers1

1

As you have discovered yourself, the main problem you experienced was caused by feeding invalid HTML to XML Worker. I have written a Java example that is equivalent to your (updated) C# example:

public static final String CSS = "th { background-color: #C0C0C0; font-size: 16pt; } "
    + "td { font-size: 10pt; }";
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 table looks like this:

enter image description here

You can tweak the values stored in CSS to create a nicer appearance for the table.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • @Brunno Lowagie Thanks – Arun Prasad E S May 24 '17 at 08:06
  • @Bruno I didnt see new ByteArrayInputStream(HTML.getBytes()) method. what it does ? – Selman Jan 17 '18 at 15:41
  • The `ByteArrayInputStream` is a standard Java class in the JDK. It's an `InputStream` implementation that takes a `byte[]` and that allows you to use it as an `InputStream`. There is nothing special about it. It's just Java. By the way: if you're looking to convert HTML to PDF, please read [Converting HTML to PDF using iText](https://stackoverflow.com/questions/47895935/converting-html-to-pdf-using-itext) before you start writing code. – Bruno Lowagie Jan 17 '18 at 15:44