Click or drag to resize
Pdf Library for .NET

Web Page Authentication

Select.Pdf HTML to PDF Converter can convert any web page a browser can open, however some web pages may require authentication before access can be granted. In such cases additional information must be supplied to the converter so that the converter can login to be granted access to the page.

Authentication Types:

HTTP Authentication

HTTP authentication is handled on the HTTP protocol level. When a page that requires HTTP authentication is accessed, the browser will display a login dialog where the user can enter an username and password. Once the correct user name and password is entered, access is granted and the page is displayed.

Select.Pdf HTML to PDF converter can automatically access pages that require HTTP authentication. To enable this feature, the Username and Password properties of HtmlToPdfAuthenticationOptions object need to be set with a valid user name and password for the page. Select.Pdf HTML to PDF converter will automatically use this information to login and access the page.

The following sample code shows how to convert an url that requires HTTP authentication:

// instantiate a html to pdf converter object
HtmlToPdf converter = new HtmlToPdf();

// set authentication options
converter.Options.Authentication.Username = username;
converter.Options.Authentication.Password = password;

// create a new pdf document converting an url
PdfDocument doc = converter.ConvertUrl(url);

// save pdf document
doc.Save(Response, false, "Sample.pdf");

// close pdf document
doc.Close();

Integrated Windows Authentication

Select.Pdf HTML to PDF supports Integrated Windows Authentication if the NTML provider is used. The current user credentials will be used by the converter to authenticate automatically.

If the automatic windows authentication does not work and the converter cannot resolve the authentication using the current user credentials, the HtmlToPdfAuthenticationOptions object can be used to specify an username and a valid password, like in the above HTTP Authentication section.

Forms Authentication

Select.Pdf Library for .NET can convert web pages that use the ASP.NET Forms Authentication mechanism to login.

In this case, if the Forms Authentication is set to use cookies to store the forms-authentication ticket, the corresponding cookie (identified by FormsAuthentication.FormsCookieName) needs to be sent by the converter to the page that will be converted.

This sample code shows how to convert an url that requires Forms authentication from a page of the same application:

// instantiate a html to pdf converter object
HtmlToPdf converter = new HtmlToPdf();

// set authentication cookie
converter.Options.HttpCookies.Add(
    System.Web.Security.FormsAuthentication.FormsCookieName,
     Request.Cookies[FormsAuthentication.FormsCookieName].Value);

// create a new pdf document converting an url
PdfDocument doc = converter.ConvertUrl(url);

// save pdf document
doc.Save(Response, false, "Sample.pdf");

// close pdf document
doc.Close();

Custom Login Page Authentication

In case the web page to be converted is protected by a general login mechanism, the Select.Pdf Html To Pdf Converter will not be able to access the page. To work around this problem, instead of converting the url directly, the page html code should be retrieve using some other method and that html code should be converted to pdf instead of the full url.

In ASP.NET, Server.Execute is the method that can be used to get the html code from an url.

IMPORTANT: If external resources (such as javascript/css files or images) are referenced in the html code, they must be placed into a location where anonymous access is granted, otherwise they will have the same authentication problems and they will not be rendered.

This sample code shows how to get the html code from an url and convert it to pdf:

// instantiate a html to pdf converter object
HtmlToPdf converter = new HtmlToPdf();

// get html code from url
TextWriter writer = new StringWriter();
Server.Execute(url, writer);
string htmlString = writer.ToString();

// get base url (to resolve relative links to external resources)
string baseUrl = HttpContext.Current.Request.Url.AbsoluteUri;

// create a new pdf document converting the html string
PdfDocument doc = converter.ConvertHtmlString(htmlString, baseUrl);

// save pdf document
doc.Save(Response, false, "Sample.pdf");

// close pdf document
doc.Close();

In case the web page to be converted is protected by a general login mechanism and Blink rendering engine is used, the SelectPdf Html To Pdf Converter can get past the login page if correct username/password and page fields are provided.

This sample code shows how to get past the login page of Facebook:

string url = @"https://www.facebook.com";

// instantiate a html to pdf converter object
HtmlToPdf converter = new HtmlToPdf();

// set converter rendering engine
converter.Options.RenderingEngine = RenderingEngine.Blink;

// set login parameters
converter.Options.LoginOptions.LoginPage = @"https://www.facebook.com";
converter.Options.LoginOptions.UsernameFieldSelector = @"#email";
converter.Options.LoginOptions.PasswordFieldSelector = @"#pass";
converter.Options.LoginOptions.SubmitButtonSelector = @"#u_0_2";
converter.Options.LoginOptions.Username = @"PUT YOUR USERNAME HERE";
converter.Options.LoginOptions.Password = @"PUT YOUR PASSWORD HERE";
converter.Options.LoginOptions.DelayBefore = 1000;
converter.Options.LoginOptions.DelayAfter = 2000;

// create a new pdf document converting an url
PdfDocument doc = converter.ConvertUrl(url);

// save pdf document
doc.Save(Response, false, "Sample.pdf");

// close pdf document
doc.Close();
See Also