繁体   English   中英

如何将.aspx页面转换为PDF格式? 没有使用网站的网址

[英]How to convert .aspx page to pdf? without using a url from the site

我需要从.aspx创建一个pdf,但不使用wkHtmlToPdf或iTextSharp,因为他们需要页面中的url并且它对我不起作用因为我在我的网站中有安全性所以当我尝试打印它发送的网站时我到默认页面。

有人知道我该怎么办?

先感谢您。

我使用EvoPdf所以我的解决方案将与您的解决方案不同,但我使用表单身份验证与应用程序(不幸的是vb.net)遇到了同样的问题。 我的解决方案如下。 关键是将Pdf库的cookie设置为经过身份验证的帐户的cookie。

Private Sub uiPdf_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uiPdf.Click

    Dim urlToConvert As String = "ExportPdf.aspx"

    '// get the html string for the report
    Dim htmlStringWriter As New System.IO.StringWriter()
    Server.Execute(urlToConvert, htmlStringWriter)
    Dim htmlCodeToConvert As String = htmlStringWriter.GetStringBuilder().ToString()
    htmlStringWriter.Close()


    '// Create the PDF converter. 
    Dim pdfConverter As New EvoPdf.HtmlToPdf.PdfConverter()

    '************
    'Add the cookie so we don't get bounced to the home page
    '************ 
    If Not Request.Cookies(System.Web.Security.FormsAuthentication.FormsCookieName) Is Nothing Then
        pdfConverter.HttpRequestCookies.Add(System.Web.Security.FormsAuthentication.FormsCookieName, Request.Cookies(System.Web.Security.FormsAuthentication.FormsCookieName).Value)
    End If

    '// set the license key - required
    pdfConverter.LicenseKey = ConfigurationManager.AppSettings("EvoPdfLicenseKey")

    '// set the converter options - optional
    '... code omitted

    '// be saved to a file or sent as a browser response
    Dim baseUrl As String = HttpContext.Current.Request.Url.AbsoluteUri
    Dim pdfBytes As Byte() = pdfConverter.GetPdfBytesFromHtmlString(htmlCodeToConvert, baseUrl)

    '// send the PDF document as a response to the browser for download
    Dim response As System.Web.HttpResponse = System.Web.HttpContext.Current.Response
    Response.Clear()
    Response.AddHeader("Content-Type", "application/pdf")
    response.AddHeader("Content-Disposition", String.Format("attachment; filename=GettingStarted.pdf; size={0}", pdfBytes.Length.ToString()))
    response.BinaryWrite(pdfBytes)
    response.End()


End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM