Posted by & filed under HTML to PDF API, HTML to PDF conversion.

So you want to convert HTML to PDF in C#? Well – you’ve come to the right place. We’ve made our HTML to PDF API so easy that you’ll be converting any URL to PDF in C# in a matter of minutes.

Our API works by sending a HTTP request with the parameters for the conversion. There’s no library or anything for you to install. You’ll only need a valid API key, which you can get by signing up for our service. In this article we assume your API key is “yourapikey” and we’ll use this in the sample code.

The sole purpose of the code is to send a HTTP request that looks like this:

https://api.htm2pdf.co.uk/urltopdf?apikey=yourapikey&url=http://www.google.com

As you can see you send us a request with just the API key and the URL and in return we’ll send you the PDF. We’ll use the settings in your members area for the conversion. If you want to adjust these you can add parameters to the URL to indicate how you want to do the conversion. We’ll get to that later.

Now suppose you want to do the above URL to PDF conversion in C#. The code you’ll be using will be the following:

string apiKey = "yourapikey";
string url = "http://www.google.com";

using (var client = new WebClient())
{
client.QueryString.Add("apikey", apiKey);
client.QueryString.Add("url", url);
client.DownloadFile("http://api.htm2pdf.co.uk/urltopdf", @"c:\temp\mypdf.pdf");
}

This will get the PDF and store it in the file ‘mypdf.pdf’ in your temp directory.

Adding PDF conversion options in C#

If you want to change the options for the conversion in C#, you can just add extra parameters to the query string and the API will obey your orders. You can find an overview of all the different parameters you can pass in the description of the HTML to PDF API.

For now let’s assume you want to watermark the PDF with our logo and rotate it 45 degrees. You’d then add the correct watermarking parameters to the query string and that’s all there is to it!

Your code would look something like this:

string apiKey = "yourapikey";
string url = "http://www.google.com";
string wm = "http://www.htm2pdf.co.uk/img/logo.png";
string wm_angle = "45";
using (var client = new WebClient())
{
client.QueryString.Add("apikey", apiKey);
client.QueryString.Add("url", url);
client.QueryString.Add("wm", wm);
client.QueryString.Add("wm_angle", wm_angle);
client.DownloadFile("http://api.htm2pdf.co.uk/urltopdf", @"c:\temp\mypdf.pdf");
}

We think this should get you converting HTML to PDF in C# over the next 10 minutes. All you need now is a valid API key, which you can get here. If you want to more about the HTML to PDF API and what all the different conversion options are, then go to the extensive documentation.

One Response to “HTML to PDF in C# made easy”

  1. agoradirectory.com

    It’s really a nice and helpful piece of information. I’m happy that you just shared this helpful info with us. Please keep us informed like this. Thanks for sharing.

Leave a Reply

You must be logged in to post a comment.