如何将.aspx转换为html? [英] How do I convert an .aspx to html?

查看:105
本文介绍了如何将.aspx转换为html?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将我的.aspx页面转换为html才能将其转换为pdf,但我不知道如何将其转换为html并将其保存为字符串。 PDF生成器工作但现在它给出一个空白的pdf文件,因为htmlmessage是空的。以下是我的代码:



 受保护的  Sub  Button1_Click( ByVal  sender  As  对象 ByVal  e 作为 System.EventArgs)句柄 Button1.Click 

Dim customernumber As 字符串 = txtcustomer.Text
Dim accountnumber As 字符串 = txtaccountnumber.Text

Dim htmlmessage As 字符串 = 字符串 .Empty


D. im pdfBytes 作为 字节()= 新的 HtmlToPdfConverter()。GeneratePdf(htmlMessage)
' Dim pdfBytes As New HtmlToPdfConverter()'' (New NReco.PdfGenerator.HtmlToPdfConverter())。GeneratePdf(HELLO)





Dim 响应 As HttpResponse = .Response
回复.Clear()
response.BufferOutput = True
response.AppendCookie( New HttpCookie( fileDownloadToken String 。格式( {0} {1},customerNumber,accountNumber)))
回复.AddH eader( Content-Type binary / octet-stream
response.AddHeader( Content-Disposition inline; filename = + 字符串 .Format( {0} {1} .pdf,customerNumber,accountNumber))
response.ContentType = application / pdf
response.BinaryWrite(pdfBytes)
response。 End ()
' response.Redirect

End Sub

解决方案

我可以建议



  var  htmlContent =  String  .Format( < body> Hello world:{0}< / body>
DateTime.Now);
var pdfBytes =( new NReco.PdfGenerator.HtmlToPdfConverter())。GeneratePdf(htmlContent);


以下代码有效:
Dim sr As StringWriter = StringWriter()
HttpContext.Current.Server.Execute( Print.aspx ,sr)
Dim htmlmessage 作为 String = sr.ToString


这不是转换。 HTML源自服务器端;它基于.aspx及其代码隐藏生成生成并在HTTP响应中发送给客户端。因此,实际上,您应该运行支持ASP.NET的HTTP服务器并使用某些浏览器加载您的页面。如果您使用浏览器的查看页面源(或类似的东西),您可以看到纯HTML代码。或者,您可以使用类 HttpWebRequest ,发送适当的HTTP请求,获取原始HTTP响应并使用它执行任何操作,进一步转换为PDF,依此类推。 (对我来说这似乎不是一个好主意,但我们假设你真的需要它。)



请参阅:

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest。 aspx [ ^ ],

http://msdn.microsoft。 com / en-us / library / system.net.httpwebresponse.aspx [ ^ ]。



另见我过去的答案:

从网页获取特定数据 [ ^ ],

如何从其他网站获取数据 [ ^ ]。



-SA

I need to convert my .aspx page to html in order to convert it to pdf, but i do not know how to go about converting it to html and saving it to a string. The PDF genrator works but right now its giving a blank pdf file because htmlmessage is empty. Below is my code:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim customernumber As String = txtcustomer.Text
    Dim accountnumber As String = txtaccountnumber.Text

    Dim htmlmessage As String = String.Empty


    Dim pdfBytes As Byte() = New HtmlToPdfConverter().GeneratePdf(htmlMessage)
    'Dim pdfBytes As New HtmlToPdfConverter() '' (New NReco.PdfGenerator.HtmlToPdfConverter()).GeneratePdf("HELLO")





    Dim response As HttpResponse = Me.Response
    response.Clear()
    response.BufferOutput = True
    response.AppendCookie(New HttpCookie("fileDownloadToken", String.Format("{0}{1}", customerNumber, accountNumber)))
    response.AddHeader("Content-Type", "binary/octet-stream")
    response.AddHeader("Content-Disposition", "inline; filename=" + String.Format("{0}{1}.pdf", customerNumber, accountNumber))
    response.ContentType = "application/pdf"
    response.BinaryWrite(pdfBytes)
    response.End()
    'response.Redirect

End Sub

解决方案

May I suggest

var htmlContent = String.Format("<body>Hello world: {0}</body>",
        DateTime.Now);
var pdfBytes = (new NReco.PdfGenerator.HtmlToPdfConverter()).GeneratePdf(htmlContent);


the following code works:
Dim sr As StringWriter = New StringWriter()
          HttpContext.Current.Server.Execute("Print.aspx", sr)
          Dim htmlmessage As String = sr.ToString


This is not "conversion". HTML is originated on the server side; it is generated based on the .aspx and its code-behind on the fly and sent to the client in the HTTP response. So, practically, you should run the HTTP server supporting ASP.NET and load your page with some browser. You can see pure HTML code if you use browser's "View page source" (or something similar). Alternatively, you can use the class HttpWebRequest, to send appropriate HTTP request, obtain raw HTTP response and do with it whatever you have to, further convert to PDF, and so on. (It never seems to be a good idea to me, but let's assume you really need it.)

Please see:
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx[^],
http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.aspx[^].

See also my past answers:
get specific data from web page[^],
How to get the data from another site[^].

—SA


这篇关于如何将.aspx转换为html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆