HTML to PDF pechkin

1. Goto Nuget 下载 Pechkin 控件

2. 创建需要打印的的PDF controller 和 Action, 这里会调用其他页面的内容进行打印。

  public ActionResult ExportPDF(int id=0)
        {
            SynchronizedPechkin sc = new SynchronizedPechkin(new GlobalConfig()
                .SetMargins(new Margins() { Left = 0, Right = 0, Top = 0, Bottom = 0 })); //设置边距

            ObjectConfig oc = new ObjectConfig();

            string sCurrentUrl = Request.Url.ToString();
            string sBook = sCurrentUrl.Substring(0, sCurrentUrl.IndexOf("/Book"));
            string sPath = sBook + "/Book/BookToPdf/" + id;

            oc.SetPrintBackground(true).SetRunJavascript(true).SetScreenMediaType(true)
                .SetLoadImages(true)
                .SetPageUri(sPath);

            byte[] buf = sc.Convert(oc);

            return File(buf, "application/pdf", "download.pdf");

        }

  3. 调用页面代码

 public ActionResult BookToPdf(int id=0)
        {
            BookService BookBS = new BookService();
            var sModel = BookBS.GetBook(id);
            ViewData["BookModel"] = sModel;
            return View();
        }

  

@model List<LibrarySys.Web.Models.BookModel>

@{ 
    Layout = null;
}

<table class="table table-hover table-bordered">
    <thead>
        <tr>
            <td class="col-md-1">
                <p>图书编码</p>
                <p>出版日期</p>
            </td>
            <td>名称</td>
            <td>价格</td>
            <td class="col-md-1">作者</td>
            
            <td>出版社</td>
            <td>操作</td>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    <p>@item.BookNumber</p>
                    <p>@item.BookPublish.ToShortDateString()</p>
                </td>
                <td><a href="/Book/BookEdit/@item.BookId" target="_self">@item.BookName</a></td>
                <td>@item.BookPrice</td>
                <td>@item.BookAuthor</td>
               
                <td>@item.Publisher</td>
                <td><a href="/Book/ExportPDF/@item.BookId" class="btn btn-xs btn-primary">导出PDF</a></td>
            </tr>
        }
    </tbody>
</table>

  4. 打印预览

 

5. 部署网络时问题解决。

6. 

 

转载于:https://www.cnblogs.com/VirtualMJ/p/6560779.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值