简体   繁体   中英

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

I need to create a pdf from .aspx, but without using wkHtmlToPdf or iTextSharp, because they need the url from the page and it dosen't work to me because I have security in my site so when I try to print the site it send me to default page.

Someone has any idea how can I do it?

Thank you in advance.

I use EvoPdf so my solution is going to be different to yours, but I ran into the same problem with an app (vb.net unfortunately) using forms authentication. My solutions was as follows. The key piece was setting the cookie of the Pdf library to those of the authenticated account.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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