Provide Credentials During HTML To PDF

Introduction

In the world of software development, converting HTML to PDF is a common requirement. Whether you’re generating reports, invoices, or any other document, having a reliable library to handle this task can save you a lot of time and effort. Enter Aspose.PDF for .NET, a powerful library that allows developers to create, manipulate, and convert PDF documents with ease. In this tutorial, we’ll walk you through the process of using Aspose.PDF to convert HTML to PDF while providing credentials for secure access. So, grab your coding hat, and let’s dive in!

Prerequisites

Before we get started, there are a few things you need to have in place:

  1. Visual Studio: Make sure you have Visual Studio installed on your machine. This will be our development environment.
  2. Aspose.PDF for .NET: You can download the library from the website. If you want to try it out first, you can also get a free trial.
  3. Basic Knowledge of C#: Familiarity with C# programming will help you understand the examples better.
  4. Internet Access: Since we’ll be fetching HTML content from a URL, ensure you have an active internet connection.

Import Packages

To get started with Aspose.PDF, you need to import the necessary packages into your project. Here’s how you can do it:

  1. Open your Visual Studio project.
  2. Right-click on your project in the Solution Explorer and select “Manage NuGet Packages.”
  3. Search for “Aspose.PDF” and install the latest version.
using System.IO;
using Aspose.Pdf;
using System;
using System.Net;

Now that we have everything set up, let’s break down the process of converting HTML to PDF with credentials into manageable steps.

Step 1: Set Up Your Document Directory

Before we can convert HTML to PDF, we need to specify where our output PDF will be saved. This is done by defining a path to the documents directory.

string dataDir = "YOUR DOCUMENT DIRECTORY";

Replace "YOUR DOCUMENT DIRECTORY" with the actual path where you want to save your PDF file. This could be a folder on your desktop or any other location on your system.

Step 2: Create a Web Request

Next, we need to create a request to fetch the HTML content from a specific URL. This is where we’ll use the WebRequest class.

WebRequest request = WebRequest.Create("http://My.signchart.com/Report/PrintBook.asp?ProjectGuid=6FB9DBB0-");

Here, we’re creating a request to the URL that contains the HTML we want to convert. Make sure to replace the URL with the one you intend to use.

Step 3: Set Credentials (If Required)

If the server requires credentials to access the content, we need to set them. This is done using the CredentialCache.DefaultCredentials.

request.Credentials = CredentialCache.DefaultCredentials;

This line ensures that the request uses the default credentials of the current user. If you need to provide specific credentials, you can create a new NetworkCredential object.

Step 4: Get the Response

Now that we have our request set up, it’s time to get the response from the server.

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

This line sends the request and waits for the server to respond. If everything goes well, we’ll receive the HTML content we need.

Step 5: Read the Response Stream

Once we have the response, we need to read the content returned by the server. This is done using a StreamReader.

Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();

Here, we’re reading the entire content of the response stream into a string variable called responseFromServer. Don’t forget to close the reader and the stream to free up resources.

Step 6: Convert HTML to PDF

Now comes the exciting part! We’ll convert the HTML content into a PDF document using Aspose.PDF.

MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(responseFromServer));
HtmlLoadOptions options = new HtmlLoadOptions("http://My.signchart.com/");
options.ExternalResourcesCredentials = CredentialCache.DefaultCredentials;

Document pdfDocument = new Document(stream, options);

In this step, we’re creating a MemoryStream from the HTML content and setting up HtmlLoadOptions. This allows us to specify the base URL for any external resources (like images or stylesheets) that the HTML might reference.

Step 7: Save the PDF Document

Finally, we need to save the generated PDF document to the specified directory.

pdfDocument.Save(dataDir + "ProvideCredentialsDuringHTMLToPDF_out.pdf");

This line saves the PDF file with the name ProvideCredentialsDuringHTMLToPDF_out.pdf in the directory we specified earlier.

Conclusion

And there you have it! You’ve successfully converted HTML to PDF using Aspose.PDF for .NET while providing credentials for secure access. This powerful library makes it easy to handle PDF documents, and with just a few lines of code, you can generate professional-looking PDFs from HTML content.

FAQ’s

What is Aspose.PDF for .NET?

Aspose.PDF for .NET is a library that allows developers to create, manipulate, and convert PDF documents in .NET applications.

How do I install Aspose.PDF?

You can install Aspose.PDF via NuGet Package Manager in Visual Studio or download it from the website.

Can I use Aspose.PDF for free?

Yes, Aspose offers a free trial version that you can use to evaluate the library before purchasing.

What types of documents can I create with Aspose.PDF?

You can create a wide range of documents, including reports, invoices, and forms, using Aspose.PDF.

Where can I find support for Aspose.PDF?

You can find support and ask questions on the Aspose support forum.