IT教程 ·

Magicodes.IE基础教程之导出Pdf

9种分布式ID生成之 美团(Leaf)实战

 

申明

本教程重要申明怎样运用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

Install-Package Magicodes.IE.Pdf

2.导出PDF数据

  • 建立Dto类
public class Student
        {
            /// <summary>
            ///     姓名
            /// </summary>
            public string Name { get; set; }
            /// <summary>
            ///     岁数
            /// </summary>
            public int Age { get; set; }
        }
 public async Task ExportPdf()
        {
            var exporter = new PdfExporter();
            var result = await exporter.ExportListByTemplate("test.pdf", new List<Student>()
            {
                new Student
                {
                    Name = "MR.A",
                    Age = 18
                },
                new Student
                {
                    Name = "MR.B",
                    Age = 19
                },
                new Student
                {
                    Name = "MR.B",
                    Age = 20
                }
            });
        }

导出内容以下所示:

Magicodes.IE基础教程之导出Pdf IT教程 第1张

上述代码导出了一个简朴的Pdf表格,运用的Magicodes.IE中内置的表格模板。假如我们须要自定义标题,能够经由过程【ExporterHeader】特征来设置:

 [PdfExporter(Name = "门生信息")]
        public class Student
        {
            /// <summary>
            ///     姓名
            /// </summary>
            [ExporterHeader(DisplayName = "姓名")]
            [Display(Name = "Display姓名")]
            public string Name { get; set; }
            /// <summary>
            ///     岁数
            /// </summary>
            [ExporterHeader(DisplayName = "岁数")]
            public int Age { get; set; }
        }
  1. PdfExporter 经由过程Name属性来定义文档标题
  2. ExporterHeader DisplayName用来定义属性称号
  3. Display一样定义属性称号,然则优先级小于ExporterHeader

经由过程修正上述代码实行结果以下所示:

Magicodes.IE基础教程之导出Pdf IT教程 第2张

3.经由过程模板导出PDF收条

  • 建立导出模板
<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="utf-8"/>
    <title></title>
    <style type="text/css">
        body { font-family: SimSun !important; }

        p { margin: 0px; }

        footer {
            color: #333;
            font-size: 1.2rem;
            margin-bottom: 1.5rem;
            margin-right: 5%;
            margin-top: 1.2rem;
            text-align: right;
        }

        table,
        td {
            border: 1px solid #444;
            border-collapse: collapse;
            /* text-align: center; */
            height: 2rem;
            padding: 5px;
        }
    </style>
</head>

<body>
<p style="color: #000; font-size: 1.8rem; height: 32px; text-align: center;">
    @Model.Title
</p>
<p style="color: #333; font-size: 1.2rem; margin-left: 5%; margin-top: 1%;">
    <text>NO:@Model.Data.Code</text>
    <text style="padding-left: 52%; text-align: right;">生意业务时候:@Model.Data.TradeTime.ToString("yyyy-MM-dd HH:mm:ss")</text>
</p>

<table width="90%" style="margin-left: 5%; margin-top: 1%;">
    <tr style="font-size: 1rem;">
        <td style="text-align: center; width: 11%;">交款姓名</td>
        <td colspan="2">@Model.Data.Name</td>
        <td style="text-align: center; width: 13%;">身份证号码</td>
        <td colspan="3">@Model.Data.IdNo</td>
    </tr>
    <tr style="font-size: 1rem;">
        <td style="text-align: center">生意业务金额</td>
        <td colspan="6">
            <span>¥:@Model.Data.Amount</span>
            <span style="padding: 0 2% 0 2%;">人民币(大写):</span>
            <span>@Model.Data.UppercaseAmount</span>
        </td>
    </tr>
    <tr style="font-size: 1rem;">
        <td style="text-align: center">收款体式格局</td>
        <td colspan="2">@Model.Data.PaymentMethod</td>
        <td style="text-align: center; width: 13%;">生意业务状况</td>
        <td colspan="3">@Model.Data.TradeStatus</td>
    </tr>

    <tr style="font-size: 1rem;">
        <td style="text-align: center">收款事由</td>
        <tdstyle="width: 22.3%;">@Model.Data.Remark</td>
        <tdstyle="text-align: center; width: 11%;">入学年级</td>
        <tdstyle="width: 22.3%;" colspan="2">@Model.Data.Grade</td>
        <tdstyle="text-align: center; width: 11%;">专业</td>
        <tdstyle="width: 22.3%;">@Model.Data.Profession</td>
    </tr>
</table>
</body>
</html>

如上述代码所示,为了便于模板的编写,Magicodes.IE支撑HTML模板的编写。

  • 建立Dto类
      [Exporter(Name = "湖南心莱信息科技有限公司电子收款凭据")]
        public class ReceiptInfo
        {
            /// <summary>
            ///     生意业务时候
            /// </summary>
            public DateTime TradeTime { get; set; }
    
            /// <summary>
            ///     姓名
            /// </summary>
            public string Name { get; set; }
    
            /// <summary>
            ///     身份证
            /// </summary>
            public string IdNo { get; set; }
    
            /// <summary>
            ///     金额
            /// </summary>
            public decimal Amount { get; set; }
    
            /// <summary>
            ///     付出体式格局
            /// </summary>
            public string PaymentMethod { get; set; }
    
            /// <summary>
            ///     生意业务状况
            /// </summary>
            public string TradeStatus { get; set; }
    
            /// <summary>
            ///     备注
            /// </summary>
            public string Remark { get; set; }
    
            /// <summary>
            ///     年级
            /// </summary>
            public string Grade { get; set; }
    
            /// <summary>
            ///     专业
            /// </summary>
            public string Profession { get; set; }
    
            /// <summary>
            ///     收款人
            /// </summary>
            public string Payee { get; set; }
    
            /// <summary>
            ///     大写金额
            /// </summary>
            public string UppercaseAmount { get; set; }
    
            /// <summary>
            ///     编号
            /// </summary>
            public string Code { get; set; }
        }
  •     如上代码片断经由过程Exporter特征去指定模板中的Title,当然在我们现实开发中也能够不经由过程该属性去做指定,毕竟我们这一块也是完整自定义的
  • 怎样运用自定义模板导出?代码以下所示:
      public async Task ExportReceipt()
            {
                var tplPath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "ExportTemplates",
                    "receipt.cshtml");
                var tpl = File.ReadAllText(tplPath);
                var exporter = new PdfExporter();
                //此处运用默许模板导出
                var result = await exporter.ExportByTemplate("test.pdf",
                    new ReceiptInfo
                    {
                        Amount = 22939.43M,
                        Grade = "2019秋",
                        IdNo = "43062619890622xxxx",
                        Name = "张三",
                        Payee = "湖南心莱信息科技有限公司",
                        PaymentMethod = "微信付出",
                        Profession = "活动练习",
                        Remark = "学费",
                        TradeStatus = "已完成",
                        TradeTime = DateTime.Now,
                        UppercaseAmount = "贰万贰仟玖佰叁拾玖圆肆角叁分",
                        Code = "19071800001"
                    }, tpl);
            }

    经由过程上述代码我们须要举行指定通报模板内容,终究导出结果以下:

    Magicodes.IE基础教程之导出Pdf IT教程 第3张

4.批量导出PDF收条

在某些情况下,我们须要将多条数据导出到一个Pdf,就比方银行供应的批量收条。经由过程Magicodes.IE,也能够异常简朴的完成相似的需求。重要步骤以下所示:

  • 建立模板
    <!DOCTYPE html>
    
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
        <meta charset="utf-8"/>
        <title></title>
        <style type="text/css">
            body { font-family: SimSun !important; }
    
            p { margin: 0px; }
    
            footer {
                color: #333;
                font-size: 1.2rem;
                margin-bottom: 1.5rem;
                margin-right: 5%;
                margin-top: 1.2rem;
                text-align: right;
            }
    
            table,
            td {
                border: 1px solid #444;
                border-collapse: collapse;
                height: 2rem;
                padding: 5px;
            }
    
            .evenNum {
                margin-bottom: 8%;
                padding-top: 10%;
            }
        </style>
    </head>
    
    <body>
    @for (var i = 0; i < Model.Data.ReceiptInfoInputs.Count; i++)
    {
        <div style="height: 41%; width: 100%;" class="@(i % 2 == 0 ? "" : "evenNum")">
            <p style="color: #000; font-size: 1.8rem; height: 32px; text-align: center;">
                @Model.Data.Title
            </p>
            <p style="color: #333; font-size: 1.2rem; margin-left: 5%; margin-top: 1%;">
                <text>NO:@Model.Data.ReceiptInfoInputs[i].Code</text>
                <text style="padding-left: 52%; text-align: right;">生意业务时候:@Model.Data.ReceiptInfoInputs[i].TradeTime.ToString("yyyy-MM-dd HH:mm:ss")</text>
            </p>
    
            <table width="90%" style="margin-left: 5%; margin-top: 1%;">
                <tr style="font-size: 1rem;">
                    <td style="text-align: center; width: 11%;">交款姓名</td>
                    <td colspan="2">@Model.Data.ReceiptInfoInputs[i].Name</td>
                    <td style="text-align: center; width: 13%;">身份证号码</td>
                    <td colspan="3">@Model.Data.ReceiptInfoInputs[i].IdNo</td>
                </tr>
                <tr style="font-size: 1rem;">
                    <td style="text-align: center">生意业务金额</td>
                    <td colspan="6">
                        <span>¥:@Model.Data.ReceiptInfoInputs[i].Amount</span>
                        <span style="padding: 0 2% 0 2%;">人民币(大写):</span>
                        <span>@Model.Data.ReceiptInfoInputs[i].UppercaseAmount</span>
                    </td>
                </tr>
                <tr style="font-size: 1rem;">
                    <td style="text-align: center">收款体式格局</td>
                    <td colspan="2">@Model.Data.ReceiptInfoInputs[i].PaymentMethod</td>
                    <td style="text-align: center; width: 13%;">生意业务状况</td>
                    <td colspan="3">@Model.Data.ReceiptInfoInputs[i].TradeStatus</td>
                </tr>
    
                <tr style="font-size: 1rem;">
                    <tdstyle="text-align: center">收款事由</td>
                    <tdstyle="width: 22.3%;">@Model.Data.ReceiptInfoInputs[i].Remark</td>
                    <tdstyle="text-align: center; width: 11%;">入学年级</td>
                    <tdstyle="width: 22.3%;" colspan="2">@Model.Data.ReceiptInfoInputs[i].Grade</td>
                    <tdstyle="text-align: center; width: 11%;">专业</td>
                    <tdstyle="width: 22.3%;">@Model.Data.ReceiptInfoInputs[i].Profession</td>
                </tr>
            </table>
        </div>
    }
    </body>
    </html>
  • 建立Dto类
     /// <summary>
        ///     批量导出Dto
        /// </summary>
        [PdfExporter(Orientation = Orientation.Portrait, PaperKind = PaperKind.A5)]
        public class BatchReceiptInfoDto
        {
            /// <summary>
            ///     生意业务时候
            /// </summary>
            public DateTime TradeTime { get; set; }
    
            /// <summary>
            ///     姓名
            /// </summary>
            public string Name { get; set; }
    
            /// <summary>
            ///     身份证
            /// </summary>
            public string IdNo { get; set; }
    
            /// <summary>
            ///     金额
            /// </summary>
            public decimal Amount { get; set; }
    
            /// <summary>
            ///     付出体式格局
            /// </summary>
            public string PaymentMethod { get; set; }
    
            /// <summary>
            ///     生意业务状况
            /// </summary>
            public string TradeStatus { get; set; }
    
            /// <summary>
            ///     备注
            /// </summary>
            public string Remark { get; set; }
    
            /// <summary>
            ///     年级
            /// </summary>
            public string Grade { get; set; }
    
            /// <summary>
            ///     专业
            /// </summary>
            public string Profession { get; set; }
    
            /// <summary>
            ///     大写金额
            /// </summary>
            public string UppercaseAmount { get; set; }
    
            /// <summary>
            ///     编号
            /// </summary>
            public string Code { get; set; }
        }
  • 怎样运用
    public async Task BathExportReceipt()
            {
                var tplPath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "ExportTemplates",
                    "batchReceipt.cshtml");
                var tpl = File.ReadAllText(tplPath);
                var exporter = new PdfExporter();
    
                var input = new BatchReceiptInfoInput
                {
                    Payee = "湖南心莱信息科技有限公司",
                    ReceiptInfoInputs = new List<BatchReceiptInfoDto>()
                };
    
                for (var i = 0; i < 20; i++)
                    input.ReceiptInfoInputs.Add(new BatchReceiptInfoDto
                    {
                        Amount = 22939.43M,
                        Grade = "2019秋",
                        IdNo = "43062619890622xxxx",
                        Name = "张三",
                        PaymentMethod = "微信付出",
                        Profession = "活动练习",
                        Remark = "学费",
                        TradeStatus = "已完成",
                        TradeTime = DateTime.Now,
                        UppercaseAmount = "贰万贰仟玖佰叁拾玖圆肆角叁分",
                        Code = "1907180000" + i
                    });
    
                //此处运用默许模板导出
                var result = await exporter.ExportByTemplate("test.pdf", input, tpl);
            }

经由过程上述代码我们须要举行指定通报模板内容,终究导出结果以下:

注重事项

至此,全部教程到这里就完毕了。不过另有以下事项须要注重:

  • Pdf导出支撑全平台,包含Windows和Linux,x86以及x64
  • Pdf导出底层基于wkhtmltopdf库的封装,已包含wkhtmltopdf各平台的包,无需再次装置
  • 在Linux容器中,Pdf导出须要装置相干字体文件,详细见容器教程

[dubbo 源码之 ]1. 服务提供方如何发布服务

参与评论