博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
动态将ASPX生成HTML网页并将网页导出PDF
阅读量:5171 次
发布时间:2019-06-13

本文共 4210 字,大约阅读时间需要 14 分钟。

1.首先要找到wnvhtmlconvert.dll这个文件,并引入项目中。

2.Server.Execute("pos.aspx?id=" + ids); 执行相应的aspx网页

3.pos.aspx网页中有这样的一个方法,此方法用来生成html页面

protected override void Render(HtmlTextWriter writer)

{
string htmlname = "POSDemo";
string fileName = string.Empty;

string content = string.Empty;
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
System.IO.StreamWriter sw = null;
try
{
// 将当前页面的内容呈现到临时的 HtmlTextWriter 对象中
base.Render(htmlWriter);
htmlWriter.Close();
// 得到当前页面的全部内容
content = stringWriter.ToString();
fileName = htmlname + ".html";
//sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
//sw.Write(str);
//sw.Flush();
//System.IO.StringWriter creaObj = new System.IO.StringWriter(Server.MapPath("html/") + fileName, false, content);

string path = HttpContext.Current.Server.MapPath("../Upload/");

System.Text.Encoding code = System.Text.Encoding.GetEncoding("utf-8");

sw = new System.IO.StreamWriter(path + fileName, false, code);
//sw = new System.IO.StreamWriter(fileName, false, code);
sw.Write(content);
sw.Flush();
string website = "http://" + HttpContext.Current.Request.Url.Authority;

Response.Write(content);

}
catch { }
finally
{
sw.Dispose();
stringWriter.Dispose();
htmlWriter.Close();
htmlWriter.Dispose();
}

}

 

4.运用下面的类来生成pdf字节

public class MyPDFGenerator

{
public static byte[] Create(string sUrl)
{
try
{
PdfConverter pdfConverter = new PdfConverter();
// set the license key - required
//pdfConverter.LicenseKey = "P38cBx6AWW7b9c81TjEGxnrazP+J7rOjs+9omJ3TUycauK+cLWdrITM5T59hdW5r";//test key, add by tieli

pdfConverter.LicenseKey = "oYqQgZCBlpeBlY+RgZKQj5CTj5iYmJg=";

// set the converter options - optional
pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4;
pdfConverter.PdfDocumentOptions.PdfCompressionLevel = PdfCompressionLevel.NoCompression;
pdfConverter.PdfDocumentOptions.PdfPageOrientation = PDFPageOrientation.Portrait;
pdfConverter.PdfDocumentOptions.ShowHeader = false;
pdfConverter.PdfDocumentOptions.ShowFooter = false;
pdfConverter.PdfDocumentOptions.AutoSizePdfPage = true;
// set to generate selectable pdf or a pdf with embedded image - optional
pdfConverter.PdfDocumentOptions.GenerateSelectablePdf = true;
// set the embedded fonts option - optional, by default is false
pdfConverter.PdfDocumentOptions.EmbedFonts = false;
// enable the live HTTP links option - optional, by default is true
pdfConverter.PdfDocumentOptions.LiveUrlsEnabled = true;
// enable the support for right to left languages , by default false
pdfConverter.RightToLeftEnabled = false;

pdfConverter.PdfDocumentInfo.AuthorName = "Telamon Global, Inc.";

// add HTML header

//if (cbAddHeader.Checked)
// AddHeader(pdfConverter);
add HTML footer
//if (cbAddFooter.Checked)
// AddFooter(pdfConverter);

// Performs the conversion and get the pdf document bytes that you can further

// save to a file or send as a browser response
byte[] pdfBytes = pdfConverter.GetPdfFromUrlBytes(sUrl);
//byte[] pdfBytes = pdfConverter.GetPdfBytesFromHtmlFile(@"D:\Peter\Projects\TGSAPSystem\Code\HKOMS\Web Site\Quotes\aaaa.html");
//byte[] pdfBytes = pdfConverter.GetPdfBytesFromHtmlString(html);

// get the html string for the report
//StringWriter htmlStringWriter = new StringWriter();
//Server.Execute("InvoiceTemplate.aspx", htmlStringWriter);
//string htmlCodeToConvert = htmlStringWriter.GetStringBuilder().ToString();
//htmlStringWriter.Close();

//string baseUrl = GetAbsoluteUrl(url);

//SplendidError.SystemError(new StackTrace(true).GetFrame(0), baseUrl);

// get the pdf bytes from html string
//byte[] pdfBytes = pdfConverter.GetPdfBytesFromHtmlString(url);

return pdfBytes;

}
catch
{
return null;
}
}
}

 

5.调用方法

byte[] bytes = MyPDFGenerator.Create(Server.MapPath("../Upload/POSDemo.html"));

 

6.最后导出pdf

 

System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;

response.Clear();
response.AddHeader("Content-Type", "binary/octet-stream");
response.AddHeader("Content-Disposition",
"attachment; filename=" + fileName + ".pdf; size=" + bytes.Length.ToString());
response.Flush();
response.BinaryWrite(bytes);
response.Flush();
response.End();

转载于:https://www.cnblogs.com/linxianfeng/p/4710241.html

你可能感兴趣的文章
setImageBitmap和setImageResource
查看>>
springMVC4 注解配置实例
查看>>
单片机编程
查看>>
Filter in Servlet
查看>>
Linux--SquashFS
查看>>
Application Pool Identities
查看>>
2017-3-24 开通博客园
查看>>
【MySQL性能优化】MySQL常见SQL错误用法
查看>>
Vue2全家桶之一:vue-cli(vue脚手架)超详细教程
查看>>
Struts 2 常用技术
查看>>
树形DP
查看>>
python flask解决上传下载的问题
查看>>
语法测试
查看>>
CES1
查看>>
CES2
查看>>
文件方式实现完整的英文词频统计实例
查看>>
单个SWF文件loading加载详解(转)
查看>>
SQLServer中的CTE通用表表达式
查看>>
C# 3.0 LINQ的准备工作
查看>>
静态代码审查工具FxCop插件开发(c#)
查看>>