数据表 PDF 导出不完整



我有一个简单的html表格。分页处于关闭状态,因此页面上显示所有 50 行左右。Excel 导出效果很好。PDF 导出仅显示 1 页,约占内容的三分之一。有没有导出所有行的简单方法?即使分页设置为 true,导出仍然不完整。经过一些研究,我所拥有的应该有效。我只是想不通为什么它失败并且只导出一页。

<script>
$(document).ready(function() {
$('#summary_table').DataTable( {
dom: 'Bfrtip',
buttons: [
'excelHtml5',
{
extend: 'pdfHtml5',
orientation: 'landscape',
pageSize:'LEGAL'
}
],
paging:false
} );
} );
</script>

html 具有此结构

<table class="display">
<thead>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
<tr>
<th></th>
<th></th>
<th></th>
</tr> 
.
.
.
</table>

我不知道你包含的js和css。你可以试试这个。这是一个工作演示

<table id="example" class="display nowrap" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$320,800</td>
</tr>
</tbody>
</table>
$(document).ready(function() {
$('#example').DataTable( {
dom: 'Bfrtip',
buttons: [
'copyHtml5',
'excelHtml5',
'csvHtml5',
'pdfHtml5'
],
paging: false
} );
} );

并且不要忘记包含这些文件

https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js https://cdn.datatables.net/buttons/1.4.0/js/dataTables.buttons.min.js cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js cdn.rawgit.com/bpampuch/pdfmake/0.1.27/build/pdfmake.min.js cdn.rawgit.com/bpampuch/pdfmake/0.1.27/build/vfs_fonts.js cdn.datatables.net/buttons/1.4.0/js/buttons.html5.min.js https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css https://cdn.datatables.net/buttons/1.4.0/css/buttons.dataTables.min.css

数据表站点包含以下示例,其中包含以下pdfmake CDN的

<script src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.27/build/pdfmake.min.js"></script>
<script src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.27/build/vfs_fonts.js"></script>

我从我的代码中删除了上述内容,并通过 cdnjs 替换为 0.1.32 版本。PDF按钮现在生成了所有页面的完整导出!

<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/vfs_fonts.js"></script>

导出前设置页面设置。

protected void btnSubmit_Click(object sender, EventArgs e)
{       
WebClient wc = new WebClient();
string url = Request.Url.AbsoluteUri;
string fileContent = wc.DownloadString(url); 
List<string> tableContents = GetContents(fileContent, table_pattern);
string HTMLString = String.Join(" ", tableContents.ToArray());   
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);             
pdfDoc.Open();
pdfDoc.Add(new Paragraph("Welcome to dotnetfox"));
List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(HTMLString), null);
for (int k = 0; k < htmlarraylist.Count; k++)
{
pdfDoc.Add((IElement)htmlarraylist[k]);
} 
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;" +
"filename=sample.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();       
}   
}

最新更新