---
title: "Convert a URL to PDF in C#"
url: https://recursiveai.org/onepdf-html/cookbook/convert-url-to-pdf/
description: "Save a web page as a PDF from C# using its HTTP or HTTPS URL. This .NET example fetches HTML and print resources without running JavaScript."
version: "1.0.0"
---

# Convert a URL to PDF in C#

**OnePdf.Html** is a free, open-source .NET library from Recursive AI that converts PDF to HTML and HTML to PDF. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePdf.Html.Render) · [GitHub](https://github.com/recursive-ai-oss/onepdf-html-csharp) · [Docs](https://recursiveai.org/onepdf-html/docs/) · [Download example (.zip)](https://recursiveai.org/onepdf-html/cookbook/convert-url-to-pdf/convert-url-to-pdf.zip)

The converter prints the server’s HTML without running JavaScript.

```shell
dotnet add package RecursiveAI.OnePdf.Html.Render
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OnePdf.Html.Render;

var url = "https://example.org";
var pdf = HtmlToPdf.ConvertUrl(url);
File.WriteAllBytes("page.pdf", pdf);
Console.WriteLine("Wrote page.pdf");
```

[The example](https://recursiveai.org/onepdf-html/cookbook/convert-url-to-pdf/convert-url-to-pdf.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

The snippet writes `page.pdf` in the working directory. Replace `https://example.org` with the HTTP or HTTPS URL you want to convert.

## The full program

The `TcpListener` serves the bundled `page.html` only so the example runs offline; your own code needs just the `HtmlToPdf.ConvertUrl` call to convert a URL.

The sample page contains this HTML:

```html
<!doctype html>
<html lang="en">
<head><title>Garden hours</title></head>
<body>
  <h1>Garden hours</h1>
  <p>Open daily from 8 AM to 6 PM.</p>
</body>
</html>
```

```csharp
using System.Net;
using System.Net.Sockets;
using System.Text;
using OnePdf.Html.Render;

if (args.Length > 0)
{
    SavePdf(args[0]);
    return;
}

// Serve the included page without needing an external site.
using var server = new TcpListener(IPAddress.Loopback, 0);
server.Start();
var address = (IPEndPoint)server.LocalEndpoint;
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(15));
Task response = ServeSampleAsync(server, timeout.Token);
SavePdf($"http://{address}/page.html");
await response;

static void SavePdf(string url)
{
    byte[] pdf = HtmlToPdf.ConvertUrl(url);
    File.WriteAllBytes("page.pdf", pdf);
    Console.WriteLine("Wrote page.pdf");
}

static async Task ServeSampleAsync(TcpListener server, CancellationToken token)
{
    byte[] html = await File.ReadAllBytesAsync("page.html", token);
    using var client = await server.AcceptTcpClientAsync(token);
    using var stream = client.GetStream();
    using var reader = new StreamReader(stream, leaveOpen: true);
    while (await reader.ReadLineAsync(token) is { Length: > 0 }) { }

    byte[] header = Encoding.ASCII.GetBytes(
        "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n" +
        $"Content-Length: {html.Length}\r\nConnection: close\r\n\r\n");
    await stream.WriteAsync(header, token);
    await stream.WriteAsync(html, token);
}
```

`dotnet run` prints:

```text
Wrote page.pdf
```

Open `page.pdf` and look for the “Garden hours” heading and the opening hours. To convert another page with the downloaded program, run `dotnet run -- https://example.org`, replacing the URL as needed. If the input page can’t be fetched, `ConvertUrl` throws `HtmlToPdfException` with `Failure` set to `Network`, before the example writes the new PDF.

## Variations

**Print to US Letter.** Replace the `ConvertUrl` line in the snippet at the top. This overrides the HTML’s page size:

```csharp
var pdf = HtmlToPdf.ConvertUrl(url, new()
{
    PageSize = HtmlPageSize.Letter
});
```

## Related recipes

- [Add headers, footers, and page numbers](https://recursiveai.org/onepdf-html/cookbook/add-page-numbers-header-footer-html-to-pdf/)
- [Convert HTML to PDF/UA](https://recursiveai.org/onepdf-html/cookbook/convert-html-to-accessible-pdf-ua/)

---

## The short answer

Use `HtmlToPdf.ConvertUrl` with an absolute HTTP or HTTPS address, then save the returned bytes as a `.pdf`. This is a print conversion of the server’s HTML, not a screenshot of a browser tab. For example, the sample saves a copy of an hours page with its heading and paragraph still available as text in the PDF.

## Can I save a web page as a PDF if it uses JavaScript?

It depends on when the content is created. A page can use JavaScript for menus and still send a complete article in the initial response. The converter can lay out the article, but it won’t open a menu or run a script that fills in an initially empty report. Check the server’s original response rather than the browser’s live element inspector: if the text you want isn’t in the HTML response, waiting won’t make it appear.

If you control the website, give your app a print URL that sends the complete report as HTML. If your C# app already builds the whole HTML document, use the [HTML string or file input](https://recursiveai.org/onepdf-html/docs/html-to-pdf/#inputs) directly. You don’t need to serve it over HTTP just to create a PDF.

## Why are links or images missing after converting a URL to PDF?

Check whether you’re missing an image or its clickable link. They are handled separately. For a URL input, the [resource loader](https://recursiveai.org/onepdf-html/docs/html-to-pdf/#resources-files-and-the-network) can fetch referenced stylesheets, images, and fonts. If an extra resource can’t be fetched or exceeds its size limit, the converter can skip it with a diagnostic and still return a PDF. `ConvertUrl` returns bytes only; to inspect diagnostics use the [result-returning stream API](https://recursiveai.org/onepdf-html/docs/html-to-pdf/#results-diagnostics-and-errors) with a URL source.

For clickable links, the default [link policy](https://recursiveai.org/onepdf-html/docs/html-to-pdf/#links-outline-metadata-and-tags) keeps web links with HTTP or HTTPS addresses as well as `mailto`, `tel`, and links to anchors inside the document. It drops relative link targets. If a link should take the PDF reader back to a real web page, use a full HTTPS address in the source HTML.

## How do I save a web page as a PDF without cutting it off?

The default is portrait A4 with half-inch margins, unless the HTML defines its own size or margins in CSS `@page`. Choose a size in the HTML if you own the print stylesheet. Set `PageSize` in C# if the caller picks the paper, for example to print the same page on Letter or A4. Explicit conversion options override the document’s `@page` settings for the properties you set; choosing Letter alone doesn’t erase the document’s margins. If a fixed-width section doesn’t fit, adjust its width in the print stylesheet or give it more room with a wider page.
