How to Convert a URL to PDF in C# .NET Core Framework

Cloudmersive
2 min readApr 23, 2024

Using just a few lines of C# code, we can quickly render website URLs, screenshot their contents, and store those screenshots in PDF format.

To take care of this conversion, we’ll leverage a free API, and we’ll structure our API call with ready-to-run code examples provided below.

First things first, let’s look at the below input request example. We can specify extra loading time for the URL (good for websites with more media to load), and we can also specify the scale factor and whether background graphics from the website should be included in our resulting document.

{
"Url": "string",
"ExtraLoadingWait": 0,
"IncludeBackgroundGraphics": true,
"ScaleFactor": 0
}

Next, let’s turn our attention to API call authorization. We’ll need a free Cloudmersive API key to authorize our requests; this will allow us to make up to 800 API calls per month with no additional commitments.

Now we can install the client SDK using the below command:

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.4.2

And we can call the function using the below code examples:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Model;

namespace Example
{
public class ConvertWebUrlToPdfExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

var apiInstance = new ConvertWebApi();
var input = new UrlToPdfRequest(); // UrlToPdfRequest | URL to PDF request parameters

try
{
// Convert a URL to PDF
byte[] result = apiInstance.ConvertWebUrlToPdf(input);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertWebApi.ConvertWebUrlToPdf: " + e.Message );
}
}
}
}

Now we just need to write the resulting PDF file encoding to a new document, and we’re all done!

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.