ASP.NET HTML转PDF导出

该文章介绍了在INTER.BLL项目中,如何使用NReco.PdfGenerator库将HTML内容转换为PDF,并处理上传文件和响应输出,包括设置PDF页面布局、页眉和边距,以及异常处理。
摘要由CSDN通过智能技术生成
using Inter.DbAccess.Core;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Inter.WebPageBase.Core;
using System.Net;
using System.IO;
using System.Drawing.Printing;
using NReco.PdfGenerator;
using System.Drawing;
using System.Data;

namespace INTER.BLL
{
    public class UploadFilePdfHeadLease : WebPageBase
    {
        private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private byte[] bytePDF;

        protected override string getResponseStringContent(System.Web.HttpContext context)
        {
            string strhml = base.getResponseStringContent(context);
            string masterCuscd = context.Request["masterCuscd"] ?? string.Empty;
            var Msg = string.Empty;
            try
            {
                string htmlContent = strhml;
                htmlContent = htmlContent.Substring(htmlContent.IndexOf("<html>"), htmlContent.Length - htmlContent.IndexOf("<html>"));
               
                logger.Info("pdf内容" + htmlContent);
                var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
                //边距 上下左右
                htmlToPdf.Margins = new PageMargins { Top = (float)25.4, Bottom = (float)25.4, Left = (float)31.8, Right = (float)31.8 };
                //页码 page第多少页 topage 总页数 其他为样式
               htmlToPdf.PageFooterHtml = " <div  style='text-align:center;font-size:14.0pt;font-family:仿宋;color:black'>第<span class='page'></span>页 共<span class='topage'></span>页</div>";
                //string imgeestr = AppDomain.CurrentDomain.BaseDirectory + "Resources/Images/20220825131827.png";
                //imgeestr= ControlScreenshotCompletedEventCallback(imgeestr);

                if (masterCuscd == "2246")
                    htmlToPdf.Orientation = PageOrientation.Landscape;
                logger.Info("pdfNEW");
                htmlToPdf.TempFilesPath = AppDomain.CurrentDomain.BaseDirectory + "UpFile\\TEMP";
                //htmlToPdf.PdfToolPath = AppDomain.CurrentDomain.BaseDirectory + "App_Data\\wkhtmltopdf";
                bytePDF = htmlToPdf.GeneratePdf(htmlContent);
                logger.Info("pdf内容长度:" + bytePDF.Length);
            }
            catch (Exception ex)
            {
                logger.Error("异常描述:\t" + ex.Message);
                logger.Error("异常方法:\t" + ex.TargetSite);
                logger.Error("异常堆栈:\t" + ex.StackTrace);
            }
            return Msg;

        }
        protected override void ResponseOut(System.Web.HttpContext context)
        {
            try
            {
                context.Response.ClearHeaders();

                context.Response.BufferOutput = true;
                context.Response.Charset = "utf-8";
                context.Response.ContentType = "application/vnd.ms-excel";
                string filename = HttpUtility.UrlEncode(context.Request["filename"]);
                context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
                getResponseStringContent(context);
                context.Response.BinaryWrite(bytePDF);
            }
            catch (Exception ex)
            {
                logger.Error("异常描述:\t" + ex.Message);
                logger.Error("异常方法:\t" + ex.TargetSite);
                logger.Error("异常堆栈:\t" + ex.StackTrace);
            }

        }

       



    }
}

可参考 NReco: .NET REusable COmponents api地址

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
asp.net生成PDF详解 asp.net生成PDF PDF详解 用C#制作PDF文件全攻略 丽水市汽车运输集团有限公司信息中心 苟安廷 目 录 前 言 3 第一部分 iText的简单应用 4 第一章 创建一个Document 4 第一步 创建一个Document实例: 5 第二步 创建Writer实例 6 第三步 打开Document 6 第四步 添加内容 10 第五步,关闭 document 11 第二章 块、短句和段落 11 块 11 短句 12 段落 12 字体的延续 13 第三章 锚点、列表和注释 14 锚点 14 列表 14 注释 15 第四章 页眉页脚、章节、区域和绘图对象 16 页眉页脚 16 章节和区域 17 图形 17 第五章 表格 18 一些简单的表格 18 一些表格参数 18 大表格 20 内存管理 20 嵌套表格 21 表格偏移 21 表格的绝对位置 21 第六章 图片 21 Image对象 21 图片的位置 22 缩放和旋图片 23 原始图片数据 23 System.Drawing.Bitmap 23 TIFF和CCITT 24 图片和其他对象 24 第二部分 其他文档格式 25 第七章 XML和 (X)HTML 25 第八章 RTF文件 25 RTF包 25 创建一个RTF文档 25 不支持的特性 26 RTF中扩展的页眉和页脚 26 第三部分 iText的高级应用 27 第九章 字体 27 TrueType字体应用 27 TruType字体集合的应用 28 第十章 图象和文本的绝对位置 28 pdfContentByte 28 简单图形 29 文本 29 模板(Form xObjects) 30 分栏 31 PdfTable 32 颜色(SpotColors)和图案(Patterns) 33 第十一章 本地和异地向、目标和概要 33 本地向 33 异地向 33 第十二章 页面和表格事件 34
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值