NET ASP.NET Code MVC网站程序使用Magicodes.IE.Pdf 导出Pdf的具体方法
本文主要说明如何使用Magicodes.IE.Pdf完成Pdf 报告单的生成
要点
导出PDF数据
自定义PDF模板
导出单据
如何批量导出单据
导出特性说明
PdfExporterAttribute
PdfExporterAttribute特性用于设置Pdf导出的总体设置,比如方向、纸张等。主要包含如下设置:
Name:文档标题
FontSize:字体大小
Orientation: 方向(横向或纵向)
PaperKind: 纸张类型(默认A4)
IsWriteHtml: 是否输出HTML模板
IsEnablePagesCount:是否启用分页数
Encoding:编码,默认utf-8
HeaderSettings:导出头部设置
FooterSettings:导出底部设置
ExporterHeaderAttribute
ExporterHeaderAttribute 特性用于设置列头信息,在Pdf表格中,可用于设置显示名称。
DisplayName: 显示名称
使用步骤
1.安装包Magicodes.IE.Pdf
2.导出PDF数据
2.1创建RepotPDFList类
public class RepotPDFList { public int RepotPDFId { get; set; } public int RepotPDFChildenId { get; set; } public string RepotPDFChildenName { get; set; } public string RepotPDFDoctorName { get; set; } public string RepotPDFSex { get; set; } public DateTime RepotPDFBrithDate { get; set; } public DateTime RepotPDFExamDate { get; set; } public decimal RepotPDFMonthAge { get; set; } public decimal RepotPDFDYD { get; set; } public decimal RepotPDFJXDZ { get; set; } public decimal RepotPDFYY { get; set; } public decimal RepotPDFSYNL { get; set; } public decimal RepotPDFSHXW { get; set; } public decimal RepotPDFMA { get; set; } public decimal RepotPDFDQ { get; set; } public string RepotPDFztyxt { get; set; } public string RepotPDFhzph { get; set; } public string RepotPDFyygt { get; set; } public string RepotPDFtcyjl { get; set; } public string RepotPDFjy { get; set; } public string RepotPDFYear { get; set; } public string RepotPDFMonth { get; set; } public string RepotPDFDay { get; set; } public string RepotPDFUrl { get; set; } }
2.2 配置导出设置
纵向,A4,不分页
[PdfExporter(Orientation = Orientation.Portrait, PaperKind = PaperKind.A4, IsEnablePagesCount = false)]
2.3 实例化导出PDF文件
public async Task ExportPdf() { var repotPDFList = new RepotPDFList(); var result = await repotPDFList .ExportListByTemplate("test.pdf", new List<RepotPDFList>() { new Student { RepotPDFChildenId = ChildrenId, RepotPDFChildenName = repotList[0].RepotPDFChildenName, RepotPDFDoctorName = repotList[0].RepotPDFDoctorName, RepotPDFSex = repotList[0].RepotPDFSex, RepotPDFBrithDate = Convert.ToDateTime(repotList[0].RepotPDFBrithDate.ToString().Substring(0, li_i)), RepotPDFExamDate = repotList[0].RepotPDFExamDate, RepotPDFMonthAge = Math.Round(repotList[0].RepotPDFMonthAge, 2), RepotPDFDYD = Math.Round(repotList[0].RepotPDFDYD, 2), RepotPDFJXDZ = Math.Round(repotList[0].RepotPDFJXDZ, 2), RepotPDFYY = Math.Round(repotList[0].RepotPDFYY, 2), RepotPDFSYNL = Math.Round(repotList[0].RepotPDFSYNL, 2), RepotPDFSHXW = Math.Round(repotList[0].RepotPDFSHXW, 2), RepotPDFMA = Math.Round(repotList[0].RepotPDFMA, 2), RepotPDFDQ = Math.Round(repotList[0].RepotPDFDQ, 2), RepotPDFztyxt = repotList[0].RepotPDFztyxt, RepotPDFhzph = repotList[0].RepotPDFhzph, RepotPDFyygt = repotList[0].RepotPDFyygt, RepotPDFtcyjl = repotList[0].RepotPDFtcyjl, RepotPDFjy = repotList[0].RepotPDFjy, RepotPDFYear = DateTime.Now.Year.ToString(), RepotPDFMonth = DateTime.Now.Month.ToString(), RepotPDFDay = DateTime.Now.Day.ToString(), RepotPDFUrl = " " }, new Student { RepotPDFChildenId = ChildrenId, RepotPDFChildenName = repotList[0].RepotPDFChildenName, RepotPDFDoctorName = repotList[0].RepotPDFDoctorName, RepotPDFSex = repotList[0].RepotPDFSex, RepotPDFBrithDate = Convert.ToDateTime(repotList[0].RepotPDFBrithDate.ToString().Substring(0, li_i)), RepotPDFExamDate = repotList[0].RepotPDFExamDate, RepotPDFMonthAge = Math.Round(repotList[0].RepotPDFMonthAge, 2), RepotPDFDYD = Math.Round(repotList[0].RepotPDFDYD, 2), RepotPDFJXDZ = Math.Round(repotList[0].RepotPDFJXDZ, 2), RepotPDFYY = Math.Round(repotList[0].RepotPDFYY, 2), RepotPDFSYNL = Math.Round(repotList[0].RepotPDFSYNL, 2), RepotPDFSHXW = Math.Round(repotList[0].RepotPDFSHXW, 2), RepotPDFMA = Math.Round(repotList[0].RepotPDFMA, 2), RepotPDFDQ = Math.Round(repotList[0].RepotPDFDQ, 2), RepotPDFztyxt = repotList[0].RepotPDFztyxt, RepotPDFhzph = repotList[0].RepotPDFhzph, RepotPDFyygt = repotList[0].RepotPDFyygt, RepotPDFtcyjl = repotList[0].RepotPDFtcyjl, RepotPDFjy = repotList[0].RepotPDFjy, RepotPDFYear = DateTime.Now.Year.ToString(), RepotPDFMonth = DateTime.Now.Month.ToString(), RepotPDFDay = DateTime.Now.Day.ToString(), RepotPDFUrl = " " } }); }
上述导出方式,导出的是以表格(类似于Excel形式)
2.4 使用模板导出PDF
模板文件的代码:
@model IEnumerable<ENLBWebApp.Models.ResultMaster> @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>@ViewBag.ChildrenName - @ViewBag.ChildrenId - 小儿神经心理发育报告单 - 菏泽市XX民医院</title> <style> body { font-family: SimSun !important; } /*全局外包框*/ .enlb-body-div { width: 100%; max-width: 790px; height: auto; color: #000; position: relative; margin: 0px auto; padding: 0px 0px 0px 0px; } /*这里是评估报表展示样式*/ .enlb-body-div h1 { width: 100%; height: auto; line-height: 1.1em; font-size: 2.2em; text-align: center; } .enlb-body-div h4 { width: 100%; height: auto; line-height: 1.1em; font-size: 1.5em; text-align: center; } .enlb-body-table { width: 100%; } .enlb-body-table tr td { height: 1.5em; line-height: 1.5em; border: 1px solid #000000; margin: 0px auto; padding: 5px 0px 5px 0px; font-size: 1em; } .footer-main { width: 100%; max-width: 790px; height: auto; color: #000; position: relative; margin: 0px auto; padding: 0px 0px 0px 0px; } .footer-main p { text-align: right; line-height: 1.5em; height: 1.5em; font-size: 1.2em; } </style> </head> <body> <div class="enlb-body-div" id="enlb-body-div"> <br /> <h1>菏泽市XX民医院</h1> <h4>《0~6岁小儿神经心理发育检查表》报告单</h4> @foreach (var item in Model) { <table border="1" cellspacing="0" cellpadding="2" class="enlb-body-table"> <tr> <td valign="middle" align="center"> 姓名 </td> <td valign="middle" align="center">@item.ChildrenName</td> <td valign="middle" align="center"> 出生日期 </td> <td valign="middle" align="center">@ViewBag.ChildrenBirthDate</td> <td valign="middle" rowspan="3" colspan="1" align="center"> 早产年龄无需矫正 </td> </tr> <tr> <td valign="middle" align="center"> 性别 </td> <td valign="middle" align="center">@ViewBag.ChildrenSex</td> <td valign="middle" align="center"> 检查日期 </td> <td valign="middle" align="center">@item.ResultMasterDate</td> </tr> <tr> <td valign="middle" align="center"> 地址 </td> <td valign="middle" align="center"> 菏泽 </td> <td valign="middle" align="center"> 生理年龄 </td> <td valign="middle" align="center">@ViewBag.ChildrenMonthAge</td> </tr> <tr> <td valign="middle" rowspan="1" colspan="5" align="center"> 检查记录 </td> </tr> <tr> <td valign="middle" align="center"> 项目 </td> <td valign="middle" align="center"> 实际得分 </td> <td valign="middle" align="center"> 智龄(MA) </td> <td valign="middle" align="center"> 结果(DQ) </td> <td valign="middle" align="center"> 总体评价 </td> </tr> <tr> <td valign="middle" align="center"> 大运动 </td> <td valign="middle" align="center">@item.ResultMasterDYD</td> <td valign="middle" rowspan="5" colspan="1" align="center">@item.ResultMasterMA</td> <td valign="middle" rowspan="5" colspan="1" align="center">@item.ResultMasterDQ</td> <td valign="middle" align="center"> 优秀≥ 130 </td> </tr> <tr> <td valign="middle" align="center"> 精细动作 </td> <td valign="middle" align="center">@item.ResultMasterJXDZ</td> <td valign="middle" align="center"> 良好 110-129 </td> </tr> <tr> <td valign="middle" align="center"> 适应能力 </td> <td valign="middle" align="center">@item.ResultMasterSYNL</td> <td valign="middle" align="center"> 中等80-109 </td> </tr> <tr> <td valign="middle" align="center"> 语言 </td> <td valign="middle" align="center">@item.ResultMasterYY</td> <td valign="middle" align="center"> 临界偏低 70-79 </td> </tr> <tr> <td valign="middle" align="center"> 社会行为 </td> <td valign="middle" align="center">@item.ResultMasterSHXW</td> <td valign="middle" align="center"> 智力发育障碍≤69 </td> </tr> <tr> <td valign="middle" colspan="1" rowspan="1" align="center"> 行为观察 </td> <td valign="middle" colspan="2" rowspan="1" align="left" style=""> <br /> <p style="padding-left:20px"> 肢体与协调:@ViewBag.Resultztyxt </p> <p style="padding-left:20px"> 合作配合:@ViewBag.Resulthzph </p> <p style="padding-left:20px"> 语言沟通:@ViewBag.Resultyygt </p> <p style="padding-left:20px"> 听从与交流:@ViewBag.Resulttcyjl </p> <br /> </td> <td valign="middle" colspan="1" rowspan="1" align="center"> 其他检查 </td> <td valign="middle" colspan="1" rowspan="1" align="center"> 无 </td> </tr> <tr> <td valign="middle" colspan="1" rowspan="1" align="center"> 建议 </td> <td colspan="4" valign="middle" align="left" rowspan="1"> <p style="padding-left:20px">@ViewBag.ResultJY</p> </td> </tr> </table> } <div class="footer-main"> <p> <span style="width:33.333%; float:left; height:1em; text-align:center">测 试 者: @ViewBag.ChildrenName</span> <span style="width:33.333%; float:left; height:1em; text-align:center">医生签字:@ViewBag.DoctorName</span> <span style="width:33.333%; float:left; height:1em; text-align:center">@ViewBag.Resultyear 年 @ViewBag.Resultmonth 月 @ViewBag.Resultday 日</span> <span style="clear:both;"></span> </p> <br /> </div> </div> </body> </html>
导出类代码:
public async Task<FileResult> reportPdf(int ChildrenId) { using (var db = new EfDbContext()) { //根据传入参数获取结果的LIST对象,默认认为仅一套数据 var repotList = db.RepotPDFList.FromSqlRaw($"SELECT * FROM RepotPDFList").Where(x => x.RepotPDFChildenId == ChildrenId).ToList(); int li_i = repotList[0].RepotPDFBrithDate.ToString().IndexOf(" "); //构建数据实例 RepotPDFList repotPDFList = new RepotPDFList() { RepotPDFChildenId = ChildrenId, RepotPDFChildenName = repotList[0].RepotPDFChildenName, RepotPDFDoctorName = repotList[0].RepotPDFDoctorName, RepotPDFSex = repotList[0].RepotPDFSex, RepotPDFBrithDate = Convert.ToDateTime(repotList[0].RepotPDFBrithDate.ToString().Substring(0, li_i)), RepotPDFExamDate = repotList[0].RepotPDFExamDate, RepotPDFMonthAge = Math.Round(repotList[0].RepotPDFMonthAge, 2), RepotPDFDYD = Math.Round(repotList[0].RepotPDFDYD, 2), RepotPDFJXDZ = Math.Round(repotList[0].RepotPDFJXDZ, 2), RepotPDFYY = Math.Round(repotList[0].RepotPDFYY, 2), RepotPDFSYNL = Math.Round(repotList[0].RepotPDFSYNL, 2), RepotPDFSHXW = Math.Round(repotList[0].RepotPDFSHXW, 2), RepotPDFMA = Math.Round(repotList[0].RepotPDFMA, 2), RepotPDFDQ = Math.Round(repotList[0].RepotPDFDQ, 2), RepotPDFztyxt = repotList[0].RepotPDFztyxt, RepotPDFhzph = repotList[0].RepotPDFhzph, RepotPDFyygt = repotList[0].RepotPDFyygt, RepotPDFtcyjl = repotList[0].RepotPDFtcyjl, RepotPDFjy = repotList[0].RepotPDFjy, RepotPDFYear = DateTime.Now.Year.ToString(), RepotPDFMonth = DateTime.Now.Month.ToString(), RepotPDFDay = DateTime.Now.Day.ToString(), RepotPDFUrl = " " }; //设置使用的导出模板 var tplPath = Path.Combine(Directory.GetCurrentDirectory(), "Template", "PdfTemplate.html"); //读取模板文件 var tpl = System.IO.File.ReadAllText(tplPath); //新建导出对象 var exporter = new PdfExporter(); //新建文件名称 string fileName = $"{Guid.NewGuid().ToString()}.pdf"; //组建导出目录 string downloadName = $"{Path.Combine(Directory.GetCurrentDirectory())}\\{fileName}"; //匹配数据,填入模板 var result = await exporter.ExportByTemplate(downloadName, repotPDFList, tpl); //因为生成的模板默认在当前项目下,需要移动到wwwroot System.IO.File.Move(result.FileName, $"{_webHostEnvironment.WebRootPath}\\Download\\{fileName}"); //获取文件实际地址 string addrUrl = $"{_webHostEnvironment.WebRootPath}\\Download\\{fileName}"; //读取PDF文件数据流 var stream = System.IO.File.OpenRead(addrUrl); //返回数据文件,实际弹出下载窗口 return File(stream, result.FileType, Path.GetFileName(addrUrl)); } }
以上代码经测试,不适合CENTOS 7搭建的环境,windows环境可正常导出,LINUX环境搭建的站点请参考JS导出PDF的方法,地址如下:
http://JS 使用js库将网页导出为PDF(仅限一页A4)