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

Today I’m going to continue the series on converting to PDF with one line of code with our HTML to PDF API. I’ll be trying to accomplish the same we did in PHP last time, but this time in C# or .NET.

Now just to summarize the ‘rules of the game’ again:

  • we’ll only send the HTTP request necessary to do the URL to PDF conversion
  • we’ll refrain from error handling (of course you’d normally do that in your real implementation)
  • we’ll not destroy created objects properly (of course you’d normally do this as well in your own implementation)
  • we’ll not count lines of code that we need to include libraries (mind you: we only use standard libraries, as our API does not require you to install anything you don’t already have)

Now let’s look at the challenge request again.

The challenge HTTP request

Our challenge HTTP request, that we will want to send in C# today is going to be the following:

This will call our API with the API key yourapikey, the URL and turn it into an encrypted PDF with user password htm2pdf.

Coding the conversion in C#

In our API documentation we used code similar to the following example code to get this done.

string apiKey = "yourapikey";
string url = "";
string userpass= "htm2pdf";

using (var client = new WebClient())
client.QueryString.Add("apikey", apiKey);
client.QueryString.Add("url", url);
client.QueryString.Add("userpass", userpass);
client.DownloadFile("", @"c:\temp\mypdf.pdf");

This code breaks up the parameters apikey, url and userpass in separate lines and then feeds them to the WebClient.Downloadfile statement. This is actually the one, which calls and executes the HTTP request (aka calling the API) and stores the result in the file we specify. The using clause is there to make sure we dispose the created object correctly.

Now if we wouldn’t break up the parameters in separate lines, we’d loose 3 lines in the beginning. We would then get:

using (var client = new WebClient())
client.QueryString.Add("apikey", "yourapikey");
client.QueryString.Add("url", "");
client.QueryString.Add("userpass", "htm2pdf");
client.DownloadFile("", @"c:\temp\mypdf.pdf");

Then we can also just specify the parameters in the URL directly and gain another three lines.

using (var client = new WebClient())
client.DownloadFile("", @"c:\temp\mypdf.pdf");

And last we can also remove the using statement and we’d be left with the following two lines only:

var client = new WebClient();
client.DownloadFile("", @"c:\temp\mypdf.pdf");

Now – at this point I’m at a loss here. Can we lose one line more somehow? Or should I even count the 1st line as a line? Cause the ground rules of counting say that we weren’t going to include disposing of newly created objects. But I guess creating an object is something else than disposing of it…


In this post I showed that we can get C# allows to call the API and do the URL to PDF conversion in just two lines of code. I wasn’t able to trim it down to one, like we did in PHP, but I’d say it’s not a whole lot of code to get an awesome high quality PDF from a HTML page!

Now, before I sign off – if you have a way of doing this in one line – then please shout it out at the comments!

In the next articles in this series I’ll try to show you that you can accomplish the same in Java, Ruby, Python, Perl and maybe some other programming languages.

Stay tuned!

For now – if you want to learn more about our HTML to PDF API – have a look at the documentation! If you want to use it – SIGN UP!

Leave a Reply

You must be logged in to post a comment.