Reputation: 11
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.
Upvotes: 0
Views: 1472
Reputation: 5487
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
Upvotes: 2