EVO HTML to PDF Converter

Convert HTML Pages with Authentication

EVO HTML to PDF Converter for .NET Documentation

The web page you want to convert might be protected by different types of authentication. The most common authentication methods are Integrated Windows Authentication, Forms Authentication and custom Login pages. EVO HTML to PDF Converter offers support for resolving all these types of authentication.

Integrated Windows Authentication (NTLM)

The converter will automatically use the credentials of the user running the converter to resolve the NTLM authentication. This user can be the currently logged in user when using the converter in a desktop application or the user set as IIS pool identity when using the converter in an ASP.NET application. If the default automatic credentials cannot resolve the authentication you have the possibility to explicitly set the Username and Password in HtmlToPdfConverterAuthenticationOptions, HtmlToPdfElementAuthenticationOptions, HtmlToImageConverterAuthenticationOptions, HtmlToImageElementAuthenticationOptions or HtmlToSvgConverterAuthenticationOptions objects, function of the interface you are using to convert HTML to PDF or to images.

Code Sample - Explicitly Set the Authentication Options

// Create the HTML to PDF converter
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
// Set authentication options
htmlToPdfConverter.AuthenticationOptions.Username = username;
htmlToPdfConverter.AuthenticationOptions.Password = password;

// Create a HTML to PDF element
HtmlToPdfElement htmlToPdfElement = new HtmlToPdfElement();
// Set authentication options
htmlToPdfElement.AuthenticationOptions.Username = username;
htmlToPdfElement.AuthenticationOptions.Password = password;

// Create the HTML to Image converter
HtmlToImageConverter htmlToImageConverter = new HtmlToImageConverter();
// Set authentication options
htmlToImageConverter.AuthenticationOptions.Username = username;
htmlToImageConverter.AuthenticationOptions.Password = password;

// Create a HTML to Image element
HtmlToImageElement htmlToImageElement = new HtmlToImageElement();
// Set authentication options
htmlToImageElement.AuthenticationOptions.Username = username;
htmlToImageElement.AuthenticationOptions.Password = password;

// Create the HTML to SVG converter
HtmlToSvgConverter htmlToSvgConverter = new HtmlToSvgConverter();
// Set authentication options
htmlToSvgConverter.AuthenticationOptions.Username = username;
htmlToSvgConverter.AuthenticationOptions.Password = password;
Forms Authentication

The ASP.NET forms authentication implementation usually stores the forms authentication ticket in a cookie which should be sent back to server each time a resource is requested. The forms authentication cookie ( .ASPXAUTH ) can be sent back to server using the HtmlToPdfConverterHttpRequestCookies, HtmlToPdfElementHttpRequestCookies, HtmlToImageConverterHttpRequestCookies, HtmlToImageElementHttpRequestCookies or HtmlToSvgConverterHttpRequestCookies properties, function of the interface you are using to convert HTML to PDF or to images.

Code Sample - Explicitly Set the Forms Authentication Cookie

C#
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();

// Add the Forms Authentication cookie to request
if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
{
    htmlToPdfConverter.HttpRequestCookies.Add(FormsAuthentication.FormsCookieName,
         Request.Cookies[FormsAuthentication.FormsCookieName].Value);
}

htmlToPdfConverter.ConvertUrl(urlToConvert);
Login Page Authentication

Authentication implemented at application level using a login page can be resolved by getting the HTML code of the web page to be converted using the Server.Execute(Url) method from ASP.NET or another method and then convert that string to PDF.

The Server.Execute(Url) method is executed in your application session so all the session data and existing authentication should be valid. However, the CSS files and images referenced by the HTML code to be converted should be placed in a location which doesn't require authentication or otherwise you'll have combine with one of the authentication methods above to resolve the resources.

Code Sample - Convert a HTML Page to PDF in Same Session

protected void convertToPdfButton_Click(object sender, EventArgs e)
{
    // Save variables in Session object
    Session["firstName"] = firstNameTextBox.Text;
    Session["lastName"] = lastNameTextBox.Text;
    Session["gender"] = maleRadioButton.Checked ? "Male" : "Female";
    Session["haveCar"] = haveCarCheckBox.Checked;
    Session["carType"] = carTypeDropDownList.SelectedValue;
    Session["comments"] = commentsTextBox.Text;

    // Execute the Display_Session_Variables.aspx page and get the HTML string 
    // rendered by this page
    TextWriter outTextWriter = new StringWriter();
    Server.Execute("Display_Session_Variables.aspx", outTextWriter);

    string htmlStringToConvert = outTextWriter.ToString();

    // Create a HTML to PDF converter object with default settings
    HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();

    // Set license key received after purchase to use the converter in licensed mode
    // Leave it not set to use the converter in demo mode
    htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=";

    // Use the current page URL as base URL
    string baseUrl = HttpContext.Current.Request.Url.AbsoluteUri;

    // Convert the page HTML string to a PDF document in a memory buffer
    byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlStringToConvert, baseUrl);

    // Send the PDF as response to browser

    // Set response content type
    Response.AddHeader("Content-Type", "application/pdf");

    // Instruct the browser to open the PDF file as an attachment or inline
    Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Convert_Page_in_Same_Session.pdf; size={0}", outPdfBuffer.Length.ToString()));

    // Write the PDF document buffer to HTTP response
    Response.BinaryWrite(outPdfBuffer);

    // End the HTTP response and stop the current page processing
    Response.End();
}
Code Sample - Display Session Variables in Converted HTML Page

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        firstNameLabel.Text = Session["firstName"] != null ? (String)Session["firstName"] : String.Empty;
        lastNameLabel.Text = Session["lastName"] != null ? (String)Session["lastName"] : String.Empty;
        genderLabel.Text = Session["gender"] != null ? (String)Session["gender"] : String.Empty;

        bool iHaveCar = Session["haveCar"] != null ? (bool)Session["haveCar"] : false;
        haveCarLabel.Text = iHaveCar ? "Yes" : "No";
        carTypePanel.Visible = iHaveCar;
        carTypeLabel.Text = iHaveCar && Session["carType"] != null ? (String)Session["carType"] : String.Empty;

        commentsLabel.Text = Session["comments"] != null ? (String)Session["comments"] : String.Empty;
    }
}
See Also

Reference

Other Resources