# HtmlConverter class

> Converts HTML and CSS to PDF without a browser: the document is parsed as browsers do, styled with the cascade, laid out in CSS boxes and split into...

Namespace: [PdfNetStack.Html](https://www.jadev-corp.com/api/pdfnetstack-html/)  
Assembly: PdfNetStack.Html.dll  
Package: `PdfNetStack.Html`

Converts HTML and CSS to PDF without a browser: the document is parsed as browsers do, styled
with the cascade, laid out in CSS boxes and split into pages per the paged media rules.

```csharp
public static class HtmlConverter
```

**Inheritance**

[object](https://learn.microsoft.com/dotnet/api/system.object) ← 
[HtmlConverter](https://www.jadev-corp.com/api/pdfnetstack-html/htmlconverter/)

## Examples

<pre><code class="lang-csharp">byte[] pdf = HtmlConverter.ConvertToPdf("&lt;h1&gt;Invoice&lt;/h1&gt;&lt;p&gt;Total: 42 EUR&lt;/p&gt;");
PdfDocument doc = HtmlConverter.ConvertFileToDocument("report.html");
doc.Save("report.pdf");</code></pre>

## Methods

### <a id="PdfNetStack_Html_HtmlConverter_ConvertFileToDocument_System_String_PdfNetStack_Html_HtmlConverterOptions_"></a> ConvertFileToDocument\(string, HtmlConverterOptions?\)

Converts an HTML file. Relative URLs resolve against the file's folder, and files in that
folder may load, unless <code class="paramref">options</code> sets its own base or loader.

```csharp
public static PdfDocument ConvertFileToDocument(string path, HtmlConverterOptions? options = null)
```

#### Parameters

`path` [string](https://learn.microsoft.com/dotnet/api/system.string)

The HTML file, read as UTF-8 unless it starts with a byte order mark.

`options` [HtmlConverterOptions](https://www.jadev-corp.com/api/pdfnetstack-html/htmlconverteroptions/)?

Page size, fonts and how resources load; defaults when null.

#### Returns

 [PdfDocument](https://www.jadev-corp.com/api/pdfnetstack/pdfdocument/)

#### Exceptions

 [HtmlConversionException](https://www.jadev-corp.com/api/pdfnetstack-html/htmlconversionexception/)

The document needs more pages than [MaxPages](https://www.jadev-corp.com/api/pdfnetstack-html/htmlconverteroptions/#PdfNetStack_Html_HtmlConverterOptions_MaxPages).

### <a id="PdfNetStack_Html_HtmlConverter_ConvertToDocument_System_String_PdfNetStack_Html_HtmlConverterOptions_"></a> ConvertToDocument\(string, HtmlConverterOptions?\)

Converts an HTML string to a PDF document.

```csharp
public static PdfDocument ConvertToDocument(string html, HtmlConverterOptions? options = null)
```

#### Parameters

`html` [string](https://learn.microsoft.com/dotnet/api/system.string)

The HTML. Malformed markup is repaired as browsers repair it.

`options` [HtmlConverterOptions](https://www.jadev-corp.com/api/pdfnetstack-html/htmlconverteroptions/)?

Page size, fonts and how resources load; defaults when null.

#### Returns

 [PdfDocument](https://www.jadev-corp.com/api/pdfnetstack/pdfdocument/)

#### Exceptions

 [HtmlConversionException](https://www.jadev-corp.com/api/pdfnetstack-html/htmlconversionexception/)

The document needs more pages than [MaxPages](https://www.jadev-corp.com/api/pdfnetstack-html/htmlconverteroptions/#PdfNetStack_Html_HtmlConverterOptions_MaxPages).

### <a id="PdfNetStack_Html_HtmlConverter_ConvertToPdf_System_String_PdfNetStack_Html_HtmlConverterOptions_"></a> ConvertToPdf\(string, HtmlConverterOptions?\)

Converts an HTML string to PDF bytes.

```csharp
public static byte[] ConvertToPdf(string html, HtmlConverterOptions? options = null)
```

#### Parameters

`html` [string](https://learn.microsoft.com/dotnet/api/system.string)

The HTML.

`options` [HtmlConverterOptions](https://www.jadev-corp.com/api/pdfnetstack-html/htmlconverteroptions/)?

Page size, fonts and how resources load; defaults when null.

#### Returns

 [byte](https://learn.microsoft.com/dotnet/api/system.byte)\[\]

#### Exceptions

 [HtmlConversionException](https://www.jadev-corp.com/api/pdfnetstack-html/htmlconversionexception/)

The document needs more pages than [MaxPages](https://www.jadev-corp.com/api/pdfnetstack-html/htmlconverteroptions/#PdfNetStack_Html_HtmlConverterOptions_MaxPages).

### <a id="PdfNetStack_Html_HtmlConverter_ConvertToPdf_System_String_System_IO_Stream_PdfNetStack_Html_HtmlConverterOptions_"></a> ConvertToPdf\(string, Stream, HtmlConverterOptions?\)

Converts an HTML string and writes the PDF to a stream.

```csharp
public static void ConvertToPdf(string html, Stream output, HtmlConverterOptions? options = null)
```

#### Parameters

`html` [string](https://learn.microsoft.com/dotnet/api/system.string)

The HTML.

`output` [Stream](https://learn.microsoft.com/dotnet/api/system.io.stream)

Where the PDF is written.

`options` [HtmlConverterOptions](https://www.jadev-corp.com/api/pdfnetstack-html/htmlconverteroptions/)?

Page size, fonts and how resources load; defaults when null.

#### Exceptions

 [HtmlConversionException](https://www.jadev-corp.com/api/pdfnetstack-html/htmlconversionexception/)

The document needs more pages than [MaxPages](https://www.jadev-corp.com/api/pdfnetstack-html/htmlconverteroptions/#PdfNetStack_Html_HtmlConverterOptions_MaxPages).

### <a id="PdfNetStack_Html_HtmlConverter_ConvertToPdfAsync_System_String_System_IO_Stream_PdfNetStack_Html_HtmlConverterOptions_System_Threading_CancellationToken_"></a> ConvertToPdfAsync\(string, Stream, HtmlConverterOptions?, CancellationToken\)

Converts an HTML string and writes the PDF to a stream, asynchronously: no thread waits
while the document is laid out, and <code class="paramref">output</code> is only written with
asynchronous calls, as response bodies in ASP.NET Core and Azure Functions require.

```csharp
public static Task ConvertToPdfAsync(string html, Stream output, HtmlConverterOptions? options = null, CancellationToken cancellationToken = default)
```

#### Parameters

`html` [string](https://learn.microsoft.com/dotnet/api/system.string)

The HTML.

`output` [Stream](https://learn.microsoft.com/dotnet/api/system.io.stream)

Where the PDF is written, from its current position. It is flushed, not closed.

`options` [HtmlConverterOptions](https://www.jadev-corp.com/api/pdfnetstack-html/htmlconverteroptions/)?

Page size, fonts and how resources load; defaults when null.

`cancellationToken` [CancellationToken](https://learn.microsoft.com/dotnet/api/system.threading.cancellationtoken)

Observed before layout starts and while the PDF is saved; layout itself, bounded by
[MaxPages](https://www.jadev-corp.com/api/pdfnetstack-html/htmlconverteroptions/#PdfNetStack_Html_HtmlConverterOptions_MaxPages), runs to its end. When it fires, the call ends
with `OperationCanceledException` and what reached the stream is not a complete file.

#### Returns

 [Task](https://learn.microsoft.com/dotnet/api/system.threading.tasks.task)

#### Remarks

Layout runs on a thread of its own, as [ConvertToDocument](https://www.jadev-corp.com/api/pdfnetstack-html/htmlconverter/#PdfNetStack_Html_HtmlConverter_ConvertToDocument_System_String_PdfNetStack_Html_HtmlConverterOptions_) does, and the PDF is
then saved with [SaveAsync](https://www.jadev-corp.com/api/pdfnetstack/pdfdocument/#PdfNetStack_PdfDocument_SaveAsync_System_IO_Stream_PdfNetStack_PdfSaveOptions_System_Threading_CancellationToken_),
with [SaveOptions](https://www.jadev-corp.com/api/pdfnetstack-html/htmlconverteroptions/#PdfNetStack_Html_HtmlConverterOptions_SaveOptions).

#### Exceptions

 [HtmlConversionException](https://www.jadev-corp.com/api/pdfnetstack-html/htmlconversionexception/)

The document needs more pages than [MaxPages](https://www.jadev-corp.com/api/pdfnetstack-html/htmlconverteroptions/#PdfNetStack_Html_HtmlConverterOptions_MaxPages).

## Used in

- [Run PdfNetStack on AWS Lambda](https://www.jadev-corp.com/guides/aws-lambda/)
- [Run PdfNetStack on Azure Functions](https://www.jadev-corp.com/guides/azure-functions/)
- [Fonts and text in PDF with C# and .NET](https://www.jadev-corp.com/guides/fonts-and-text/)
- [HTML to PDF in C# without a browser](https://www.jadev-corp.com/guides/html-to-pdf/)
- [CSS support in PdfNetStack.Html](https://www.jadev-corp.com/guides/html-to-pdf/css-support/)
- [Fonts, images and resources in HTML to PDF](https://www.jadev-corp.com/guides/html-to-pdf/fonts-and-assets/)
- [Page headers, footers and numbers in HTML](https://www.jadev-corp.com/guides/html-to-pdf/paged-media/)
- [Work with large PDF files in C#](https://www.jadev-corp.com/guides/large-files/)
- [Create PDF/A files in C# and .NET](https://www.jadev-corp.com/guides/pdf-a/)
- [Convert an HTML invoice to PDF in C#](https://www.jadev-corp.com/recipes/html-invoice-to-pdf/)

Source: https://www.jadev-corp.com/pdfnetstack/api/pdfnetstack-html/htmlconverter/
