Table Text Breaks in PDF File Using HTML Renderer

3.4k views Asked by At

I am working on export functionality using HTML Renderer for PDFsharp and my table text breaks at the end of the page. I have to find solutions like:

https://github.com/ArthurHub/HTML-Renderer/pull/41

but they were not helpful.

my pdf look: Screenshot

View :

<html>
<head>
<title></title>
<link href="~/css/styles.css" rel="stylesheet" />
</head>
<body>
@{
    dynamic st;
    if (1 == 1)
    {
        st = TempData["VendorLst"] as List<abcd>;
        TempData.Keep("VendorLst");
    }
}


<style type="text/css">
    table {
        border-collapse: collapse;
        width: 100% !important;
    }

    table, th, td {
        border: 1px solid black;
    }

    td {
        page-break-inside: avoid !important;
    }
</style>
@*<img src="~/Images/image1.jpg" />*@
<div>
    <img src="@Server.MapPath("~/Images/image1.jpg")" width="120" 
 height="50" />
</div>

<table id="mytable" style="font-size:10px">

    <thead>
        @if (1 == 1)
        {
            <tr>
                <th style="width:20%">Vednor Name</th>
                <th style="width:20%">Contanct Name</th>
                <th style="width:20%">Office Phone</th>
                <th style="width:20%">Phone</th>
                <th style="width:20%">Email</th>
            </tr>
        }
        else
        {
            <tr><td>No Record Found</td></tr>
        }
    </thead>
    <tbody>
        @foreach (var item in st)
        {
            <tr>
                <td style="width:20%">@item.VendorName</td>
                <td style="width:20%">@item.ContactName</td>
                <td style="width:20%">@item.OfficePhoneNo</td>
                <td style="width:20%">@item.PhoneNo</td>
                <td style="width:20%">@item.EmailAddress</td>
            </tr>

        }
    </tbody>
</table>

Method: here is my method in this I am getting the string of my rendered HTML and I am passing it with this parameter and it will give me the PDF report

public bool ExportPdf(string htmlcontenttbl)
    {
        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", 
"attachment;filename=Myfile.pdf");
        Response.ContentType = "application/pdf";

        Response.Charset = "";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);

        //PdfDocument document = 
PdfGenerator.GeneratePdf(htmlcontenttbl.ToString(),PdfSharp.PageSize.A4,30);
        PdfDocument document = PdfGenerator.GeneratePdf(htmlcontenttbl, 
PdfSharp.PageSize.A4);

        byte[] bytes = null;
        using (MemoryStream stream = new MemoryStream())
        {
            document.Save(stream, true);
            bytes = stream.ToArray();
        }

        Response.BinaryWrite(bytes);
        Response.End();

        return true;

    }
1

There are 1 answers

12
user3759748 On

take a look at this answer. Link to Answer

If that doesn't work please share some code with us.

PdfGenerator.GeneratePdf(htmlcontenttbl.ToString(),PdfSharp.PageSize.A4,30);

change to

PdfGenerator.GeneratePdf(htmlcontenttbl.ToString(),PdfSharp.PageSize.A4);

remove the margin.