October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
.NET

How to Convert HTML to PDF in C# with NReco

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To convert an HTML string to PDF in C#, install NReco.PdfGenerator and call HtmlToPdfConverter.GeneratePdf. For a URL or file, the setup depends on where the application runs: the standard package is listed for Windows, while NReco.PdfGenerator.LT supports Windows, Linux, macOS, and Docker but requires you to supply and configure wkhtmltopdf.

Choose the NReco package that matches your deployment

NReco.PdfGenerator is a .NET wrapper around wkhtmltopdf. It can convert HTML content or a web-page URL and supports options such as headers, footers, page numbering, custom fonts, and JavaScript execution. The package descriptions on NuGet characterize it as generating a PDF from an HTML template or web-page URL.

Package Operating systems wkhtmltopdf executable License and deployment notes
NReco.PdfGenerator Windows, according to its NuGet listing. Windows binaries are embedded and extracted on first use. The listing describes free use for non-SaaS projects with one single-server production deployment. Confirm current terms for your project before shipping.
NReco.PdfGenerator.LT Windows, Linux, macOS, and Docker. Not bundled. You must supply wkhtmltopdf and configure WkHtmlToPdfExeName and PdfToolPath. The listing documents License.SetLicenseKey and says the package is not available to free users. Check current licensing terms before deployment.

The publisher profile lists version 1.2.1 and compatibility with .NET Standard 2.0 and .NET Framework 4.5 for these packages. Package versions and terms can change; verify the package listing and compatibility for your target framework when installing.

Install the package

In Visual Studio, open the project, choose Manage NuGet Packages, search for the package matching your deployment, and install it. Alternatively, use the .NET CLI from the project directory:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
dotnet add package NReco.PdfGenerator

For a cross-platform deployment, install the LT package instead:

dotnet add package NReco.PdfGenerator.LT

Do not install the Windows-bundled package just because development happens on Windows if the deployed application will run on Linux or in a Linux container. Select the package for the runtime environment, then validate the deployed executable path and license configuration there.

Convert an HTML string to PDF

For a string that already contains the HTML you want rendered, create the converter and call GeneratePdf. The returned value is a byte array; write it to disk or return it from an ASP.NET endpoint.

using NReco.PdfGenerator;

var htmlContent = "<html><body><h1>Invoice</h1><p>Ready to print.</p></body></html>";
var htmlToPdf = new HtmlToPdfConverter();
var pdfBytes = htmlToPdf.GeneratePdf(htmlContent);

File.WriteAllBytes("output.pdf", pdfBytes);

This minimal example assumes a modern .NET project with System.IO available. In a file where it is not imported implicitly, add using System.IO;. The result is a complete PDF byte sequence, so an ASP.NET action can return it as a file response rather than saving it first.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Return the bytes from an ASP.NET endpoint

For example, in a controller action, pass the generated bytes and a PDF content type to the framework’s file-response helper:

using Microsoft.AspNetCore.Mvc;
using NReco.PdfGenerator;

public class DocumentsController : Controller
{
    [HttpGet("invoice.pdf")]
    public IActionResult Invoice()
    {
        var html = "<html><body><h1>Invoice</h1></body></html>";
        var converter = new HtmlToPdfConverter();
        var pdfBytes = converter.GeneratePdf(html);
        return File(pdfBytes, "application/pdf", "invoice.pdf");
    }
}

Use a suitable HTML template in place of the short example. If the content contains user-supplied values, HTML-encode those values before inserting them into markup; otherwise text may be interpreted as HTML. Avoid putting secrets into HTML that is rendered or returned to a client.

Convert a URL or local HTML file with NReco.PdfGenerator.LT

For LT, set the license key and tell the converter where the wkhtmltopdf executable is installed. The executable name and folder are deployment-specific: use the actual name and path present in the environment, including wkhtmltopdf.exe on Windows. The package API shown by NReco is:

using NReco.PdfGenerator;

var htmlToPdfConv = new HtmlToPdfConverter();
htmlToPdfConv.License.SetLicenseKey("your_id", "your_license_key");
htmlToPdfConv.WkHtmlToPdfExeName = "wkhtmltopdf"; // use wkhtmltopdf.exe on Windows
htmlToPdfConv.PdfToolPath = "<path-to-wkhtmltopdf-folder>";
htmlToPdfConv.GeneratePdfFromFile("https://example.com/", null, "output.pdf");

Replace the sample license values and path with your licensed deployment values. GeneratePdfFromFile writes the PDF to the requested output path; unlike GeneratePdf, it is not the byte-array-returning method. The file overload can also be used with an HTML file path, as appropriate for the package API and the input available to the application.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Deployment checklist for LT

  • Install the LT NuGet package rather than the Windows-bundled package for a cross-platform target.
  • Provide the wkhtmltopdf executable in each runtime environment; the LT package does not embed it.
  • Set WkHtmlToPdfExeName to the executable name used on that operating system.
  • Set PdfToolPath to the directory that contains the executable, and ensure the application process can access it.
  • Configure the license key using the licensed values appropriate to the deployment.
  • Write to a path the application can create or return the resulting PDF from a location managed by the application.

Headers, footers, fonts, and JavaScript

NReco’s package description identifies headers, footers, page numbering, custom fonts, and JavaScript execution among the supported capabilities. The exact configuration should be taken from the API documentation for the package version installed; the core string and file examples above avoid relying on undocumented option names.

  • Headers and footers: use them when the document needs repeated labels or pagination information across pages.
  • Page numbers: useful for multipage reports and print-oriented documents.
  • Custom fonts: check that required fonts are available in the runtime environment, especially when moving from a developer workstation to a server or container.
  • JavaScript: allow for page scripts to run as part of rendering. If the final content depends on client-side rendering, verify that the generated PDF contains the expected content in the actual deployment environment.

The package listings do not establish a modern-browser CSS compatibility matrix or a comparative browser-fidelity result. If a particular layout matters, validate the actual PDF output rather than assuming a specific browser engine behavior.

Common problems and practical fixes

The converter cannot find wkhtmltopdf

This is a common deployment configuration issue with LT because its package does not include the executable. Confirm that the executable exists on the target host, that WkHtmlToPdfExeName matches its filename, and that PdfToolPath points to its containing folder. Check the values in the deployed process, not only on the development machine.

It works on Windows but not in Linux or Docker

The standard NReco.PdfGenerator listing describes a Windows-only package with embedded Windows binaries. For Linux, macOS, or Docker, use the LT package and provide a wkhtmltopdf executable appropriate to that environment. Container images must include the executable and make its directory available to the application.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The PDF is missing content rendered by JavaScript

NReco identifies JavaScript execution as supported, but the package descriptions do not give a universal wait-time or timing guarantee for every page. Check whether the source page finishes rendering its data before conversion and test the resulting PDF in the deployment environment. For a controlled HTML string, consider supplying the content after the data needed for rendering is ready.

The output file is not created

For GeneratePdfFromFile, check that the output path is valid and that the process can write there. For GeneratePdf, the method returns bytes; explicitly save them with File.WriteAllBytes or return them in an HTTP response.

Licensing is unclear for the application

The standard package listing describes free use for a non-SaaS project with one single-server production deployment. The LT listing says it is not available to free users and provides license-key configuration. SaaS use, multiple servers, or another distribution model should be checked against NReco’s current package terms rather than inferred from the examples.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Performance, reliability, and cost considerations

The package listings identify the underlying tool and features but do not publish an independent speed benchmark, a modern-browser compatibility matrix, or a security audit. There is therefore no supported basis here for promising a conversion rate, throughput, or fidelity level. Measure representative documents in your target environment, including the largest HTML inputs and pages that depend on JavaScript.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For reliability, validate the generated PDF and handle conversion failures in the surrounding application. Keep executable paths and license configuration environment-specific, and test after changing the runtime image or host. The standard package’s embedded Windows binary reduces the need to provide that executable separately on Windows; LT offers broader operating-system coverage at the cost of explicit executable deployment and configuration.

Or skip the browser setup

If your input is a public webpage and a page capture meets the need, ScreenshotNeo provides a one-call screenshot API. It can also return PDFs, but this example requests an image; use the service’s documented PDF options when a PDF is specifically required. See the ScreenshotNeo API documentation for request options.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

ScreenshotNeo accepts cookie or consent banners before capture and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each of those steps can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers report the page verdict and billing status. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for Claude, Cursor, and other MCP clients. The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 shots. These features do not make it a drop-in replacement when your application needs to render an arbitrary in-memory HTML string using the NReco workflow.

Sign up for ScreenshotNeo free: 1,000 screenshots a month, no card required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Frequently Asked Questions

Does NReco convert an HTML string as well as a webpage URL?

Yes. The package supports HTML content and webpage URLs; use GeneratePdf for HTML content and the file-generation method for a URL or file.

Which NReco package should I use for a Linux container?

Use NReco.PdfGenerator.LT; it supports Linux and Docker, but you must supply and configure wkhtmltopdf.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Read next

Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.