簡體   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