ASP.NET has thousands of native classes that perform a variety of functions (CE, see reference 2). PDF conversion is not one of those functions. Fortunately, third party software developers have created class libraries that you can use to perform these conversions. Simply add one of these libraries to your project and reference it. Your ASP.NET web form will then be able to generate PDF documents using any HTML file as a source.
- Skill level:
Things you need
- Microsoft Visual Studio
Download an "HTML to PDF" .NET class library. The Resources section contains links to several popular brands.
Create a new ASP.NET website project and name it "MyProject". The page "Default.aspx" will appear in the solution explorer on the right of the screen.
Switch to design mode and drag an ASP.NET button control onto the page.
Double-click the button. This will add an event handler method named "Button1_Click" to the "Default.aspx.cs" code-behind page.
Right click "MyProject" in the solution explorer and select "Add Reference". This will open the "Add Reference" window.
Browse to the location of the library that you downloaded and select the DLL file located in that folder. All classes from the library will then be accessible from your ASP.NET project.
Add a reference to the library in the "using" section of the "Default.aspx.cs" code file.
Add "HTML to PDF" conversion code to the "Button1_Click" event handler. Different vendors will have different ways to set properties and access methods in their individual libraries. Usually the code block will look similar to this one:
/ create a new instance of the converter class / Converter converter = new Converter();
/ set properties for the PDF / converter.pageSIze = 500; converter.pagrOrientation = "Portrait"; etc;
/ get binary data from the target URL and store in the byte array /
url = "http://www.someWebSite.com"; byte bytes = converter.getDataFrom(URL_to_convert);
/ Setup response buffer / HttpResponse response = HttpContext.Current.Response; response.Clear(); response.AddHeader("Content-Type", "binary/octet-stream"); response.Flush(); response.BinaryWrite(downloadBytes); response.Flush(); response.End();
The comments are self-explanatory. You will be able to set additional properties to control the final appearance of the PDF. All converter classes will require you to setup the HTTP Response object as shown. See the documentation that comes with your download for details on how to setup the call to the converter.
Compile and run your project. Your browser will display your page containing the button.Click the button to execute the event handler code. It will retrieve the binary data from the specified URL and convert the raw HTML into a PDF document.
Tips and warnings
- Normally you will put the conversion code shown above a separate class. You can then let each page create an instance of that class. This will improve the scalability of your ASP.NET website.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for