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();

动态将ASPX生成HTML网页并将网页导出PDF的更多相关文章

  1. [转]把动态页面.aspx 生成静态页面.html

    本文转自:http://blog.csdn.net/csb5201314/article/details/5391688 如果要把主页Index.aspx 生成静态页面 Index.html后输出会提 ...

  2. 减少HTTP请求之将图片转成二进制并生成Base64编码,可以在网页中通过url查看图片(大型网站优化技术)

    在网站开发过程中,对于页面的加载效率一般都想尽办法求快.那么,怎么让才能更快呢?减少页面请求 是一个优化页面加载速度很好的方法.上一篇博文我们讲解了 “利用将小图标合成一张背景图来减少HTTP请求”, ...

  3. Java中动态代理技术生成的类与原始类的区别 (转)

    用动态代理的时候,对它新生成的类长什么样子感到好奇.有幸通过一些资料消除了心里的疑惑. 平时工作使用的Spring框架里面有一个AOP(面向切面)的机制,只知道它是把类重新生成了一遍,在切面上加上了后 ...

  4. Java中动态代理技术生成的类与原始类的区别

    用动态代理的时候,对它新生成的类长什么样子感到好奇.有幸通过一些资料消除了心里的疑惑. 平时工作使用的Spring框架里面有一个AOP(面向切面)的机制,只知道它是把类重新生成了一遍,在切面上加上了后 ...

  5. Angular动态表单生成(八)

    动态表单生成之拖拽生成表单(下) 我们的动态表单,最终要实现的效果与Form.io的在线生成表单的效果类似,可以参考它的demo地址:https://codepen.io/travist/full/x ...

  6. Angular动态表单生成(七)

    动态表单生成之拖拽生成表单(上) 这个功能就比较吊炸天了,之前的六篇,都是ng-dynamic-forms自带的功能,可能很多的说明官方的文档都已经写了,我只是个搬运工,而在这篇文章中,我将化身一个工 ...

  7. Angular动态表单生成(五)

    动态表单生成之布局 到上面的篇章为止,我们已经把表单比较完整的生成出来了,也实现了一些验证功能,可以说,我们截止这里,就已经可以满足我们的大部分表单生成需求了~ 但是: 目前来说,我们对于表单的布局只 ...

  8. 自动化测试尝试 动态Linq表达式生成 ftp上传

    自动化测试尝试   1. Selenium IDE Selenium IDE is a Chrome and Firefox plugin which records and plays back u ...

  9. Angular动态表单生成(一)

    好久不写博客了,手都生了,趁着最近老大让我研究动态表单生成的时机,撸一发博客~~ 开源项目比较 老大丢给我了两个比较不错的开源的动态表单生成工具,这两个项目在github上的star数量基本持平: h ...

随机推荐

  1. Socket与WebScoket

    socket 英文socket的意思是插座,网络中的Socket是一个抽象的接口,可以理解为网络中连接的两端.通常被叫做套接字接口,其意义在对传输层进行封装屏蔽了传输层的复杂性.它并不是一个协议,是为 ...

  2. Cordova入门系列(二)分析第一个helloworld项目 转发 https://www.cnblogs.com/lishuxue/p/6015420.html

    Cordova入门系列(二)分析第一个helloworld项目   版权声明:本文为博主原创文章,转载请注明出处 上一章我们介绍了如何创建一个cordova android项目,这章我们介绍一下创建的 ...

  3. ReSharper2018破解详细方法

    下载地址: 主程序官网下载链接:https://download.jetbrains.com/resharper/ReSharperUltimate.2018.3.3/JetBrains.ReShar ...

  4. FineUIMvc的分部视图连接器 --VS-- FineUIPro的用户控件连接器

    [FineUIPro]用户控件 在FineUIPro中,我们可以将公用的部分定义为一个用户控件,然后添加到页面中. 一个简单的示例:http://pro.fineui.com/#/usercontro ...

  5. html简单的知识

    分布式版本控制git       pwd查询当前目录 ls ls -la   git config --global user.name xxx   git config --global user. ...

  6. Android Studio自定义注释模板

    一.自定义新建文件时生成的注释 setting->Editor->File and Code Templates->Includes->File Header,在这里输入自定义 ...

  7. Python----逻辑回归

    逻辑回归 1.逻辑函数 sigmoid函数就出现了.这个函数的定义如下: sigmoid函数具有我们需要的一切优美特性,其定义域在全体实数,值域在[0, 1]之间,并且在0点值为0.5. 那么,如何将 ...

  8. Vue — 微信公众号内置h5支付相关

    首先,在公众号后台配置h5页面地址 开发流程 1.通过配置h5地址,获取code.再通过code,获取openid getOpenid(){ let url = 'https://open.weixi ...

  9. MongoDB系列:三、springboot整合mongoDB的简单demo

    在上篇 MongoDB常用操作练习 中,我们在命令提示符窗口使用简单的mongdb的方法操作数据库,实现增删改查及其他的功能.在本篇中,我们将mongodb与spring boot进行整合,也就是在j ...

  10. Java静态代码块、构造代码块执行顺序问题

    package com.zxl.staticdemo; public class BlockTest { static { System.out.println("BlockTest静态代码 ...