简体   繁体   中英

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

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

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:

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM