Why is my PDF being generated blank?

I am using ItextSharp and C #, asp.net MVC to generate a PDF report. However, when I generate the report, the PDF is returned as empty (except for the title, which works fine). I would love your input.

The code that generates the report looks like this:

                using (var writer = PdfWriter.GetInstance(doc, ms))
                {
                    // This sorts out the Header and Footer parts.
                    var headerFooter = new ReportHeaderFooter(reportsAccessor);
                    writer.PageEvent = headerFooter;

                    var rootPath = ConfigurationManager.AppSettings["SaveFileRootPath"];
                    string html = File.ReadAllText(rootPath + "/reports/report.html");                      

                    // Perform the parsing to PDF
                    doc.Open();

                    // The html needs to be sorted before this call.
                    StringReader sr = new StringReader(html);
                    XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, sr);
                    doc.Close();
                    writer.Close();
                    res = ms.ToArray();
                }

      

I know there are many hidden ones, but for the arguments as an example, the HTML that generates this and puts it in a StringReader is here:

    <table style="font-size:10pt">
        <tr>
            <td rowspan="4"> Address<br/> </td>
            <td>Phone: phone</td>
        </tr>
        <tr>
            <td>Fax: fax</td>
        </tr>
        <tr>
            <td>Email: email@example.com</td>
        </tr>
        <tr>
            <td>Website: example.com</td>
        </tr>
    <table>

    <table style="font-size:10pt; width:100%">
        <tr style="width:50%">
            <td>Settlement: 30 days from invoice</td>
        </tr>
        <tr>
            <td>Delivery Charge Notes: N/A</td>
        </tr>
    </table>
    <p style="width:100%; font-size:10pt">
    I love notes</p>

    <table>
    <tr style="font-weight:bold">
        <td>Item</td>
        <td>Item Code</td>
        <td>Description</td>
        <td>Unit Size</td>
        <td>Units Per Outer</td>
        <td>Units Per Pallet</td>
        <td>Invoice Price Volume Breaks</td>
        <td>Branding</td>
        <td>Notes</td>
    </tr>

    </table>

      

However, this html creates a nice blank PDF file (not what I want). I can't figure out what could be wrong with this, and I would like to contribute to two things:

[1] Why is the report empty [2] If it is an error in the parsing, why does it not throw an error and instead an empty report (is there a parameter or an argument that I can set that will cause errors that are much more useful than empty reports?)

UPDATE: I added code for header / footer

public string HeaderTitle {get; set; } public IReportsAccessor ReportsAccessor {get; set; } public ReportHeaderFooter (IReportsAccessor reportsAccessor) {this.ReportsAccessor = reportsAccessor; }

    public override void OnStartPage(PdfWriter writer, Document document)
    {
        base.OnStartPage(writer, document);
        var rootPath = ConfigurationManager.AppSettings["SaveFileRootPath"];
        GetReportImageResult imgInfo = ReportsAccessor.GetImage(4);

        byte[] header_img = imgInfo.ReportImage;
        string logoFn = rootPath + "/tmp/logo.png";
        File.WriteAllBytes(logoFn, header_img);
        string html = File.ReadAllText(rootPath + "/reports/report_header.html");
        html = html.Replace("{{ title }}", HeaderTitle);
        html = html.Replace("{{ logo_img }}", logoFn);

        StringReader sr = new StringReader(html);
        XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, sr);          
    }

      

+3


source to share


1 answer


When I run my HTML through my code, I get an exception because there is one hidden error in your HTML. There is no end tag in the first table: instead of </table>

specified <table>

. It's easy to miss. You can find the corrected HTML here: table10.html

This is what the resulting PDF looks like: html_table_10.pdf

enter image description here

One significant difference between your code and mine is that I was using iText / XML Worker 5.5.6 (Java) and you were using iTextSharp / XML Worker (C #). Although Java iText and XML Worker sync with C # iTextSharp and XML Worker (and therefore should have the same behavior), the underlying XML parser is different.



I know we chose to behave like browsers (*) by silently allowing some bad HTML constructs, but my experience with Java XML parsing is an exception. Perhaps the C # XML parsing is not working.

(*) I am the CEO of the iText Group, made up of companies in Belgium, USA and Singapore.

As for the "css resolver crying" exception, we will remove it in the next version: enter image description here

+3


source







All Articles