The converter prints the server’s HTML without running JavaScript.
dotnet add package RecursiveAI.OnePdf.Html.RenderRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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 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:
<!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>
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:
Wrote page.pdfOpen 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:
var pdf = HtmlToPdf.ConvertUrl(url, new() { PageSize = HtmlPageSize.Letter });
Related recipes
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 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 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 with a URL source.
For clickable links, the default link policy 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.