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地址