使用HTML呈现器在PDF文件中打破表格文本

时间:2017-09-07 10:22:53

标签: c# asp.net-mvc html-renderer

我正在使用HTML Renderer for PDFsharp处理导出功能,我的表格文本在页面末尾中断。我找到了类似的解决方案:

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

但他们没有帮助。

my pdf look

查看:

<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>

方法:这是我的方法,我得到了我呈现的HTML的字符串,我用这个参数传递它,它会给我PDF报告

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 个答案:

答案 0 :(得分:0)

看看这个答案。 Link to Answer

如果不起作用,请与我们分享一些代码。

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

更改为

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

删除保证金。