iTextSharp add ( css style or a css file) and download pdf file

18.5k views Asked by At

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();
1

There are 1 answers

3
Bruno Lowagie On

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.