【发布时间】:2010-10-06 21:42:59
【问题描述】:
我想使用可以将 Html 转换为 PDF 的组件将 aspx 页面转换为 PDF。是否可以在回发期间重定向 aspx 页面的输出并将其作为流或字符串发送到 HtmlToPdf 方法?
【问题讨论】:
我想使用可以将 Html 转换为 PDF 的组件将 aspx 页面转换为 PDF。是否可以在回发期间重定向 aspx 页面的输出并将其作为流或字符串发送到 HtmlToPdf 方法?
【问题讨论】:
您是否尝试发送从“HttpContext.Current.Response.OutputStream;”返回的值?在回发中?
【讨论】:
您好,我认为这样做的方法是使用 Reponse.Filter 属性来拦截和更改发送到页面的 HTML。
在 ASP.net 网站的这个页面上有一个 VB.net 和 C# 的教程视频和示例代码:
【讨论】:
您将编写一个附加到请求的 HttpFilter。这是在 ASP.NET 页面的渲染步骤编写后可以更改输出的代码。
This article 展示了如何做到这一点(他们将输出从 HTML 更改为有效的 XHTML,但想法是一样的)。
【讨论】:
protected override void Render(HtmlTextWriter writer)
{
// setup a TextWriter to capture the page markup
TextWriter tw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(tw);
// render the page into our surrogate TextWriter
base.Render(htw);
// convert the TextWriter markup to a string
string pageSource = tw.ToString();
if (convertToPDF)
{
// convert the page markup to a pdf
// eg, byte[] pdfBytes = HtmlToPdf(pageSource);
}
// write the page markup into the output stream
writer.Write(pageSource);
}
【讨论】: