Skip to content

Repository files navigation

QuestPDF.HTML

QuestPDF.HTML is an extension for QuestPDF that allows you to generate PDF from HTML.

QuestPDF currently does not support inserting HTML into a PDF document. This library fills that gap. While it doesn't support the full functionality of HTML and CSS, it covers most common use cases.

Dependencies

Installation

dotnet add package QuestPDF.HTML

Usage

Basic Example

Document.Create(container =>{container.Page(page =>{page.Content().Column(col =>{col.Item().HTML(handler =>{handler.SetHtml(html);});});});}).GeneratePdf(path);

Custom Image Loading

Recommended: Override the default image loading method for better performance and async support:

col.Item().HTML(handler =>{handler.OverloadImgReceivingFunc(GetImgBySrc);handler.SetHtml(html);});

Custom Styles

You can customize text and container styles for specific HTML tags:

handler.SetTextStyleForHtmlElement("div",TextStyle.Default.FontColor(Colors.Grey.Medium));handler.SetTextStyleForHtmlElement("h1",TextStyle.Default.FontColor(Colors.DeepOrange.Accent4).FontSize(32).Bold());handler.SetContainerStyleForHtmlElement("table", c =>c.Background(Colors.Pink.Lighten5));handler.SetContainerStyleForHtmlElement("ul", c =>c.PaddingVertical(10));

List Padding

Set vertical padding for top-level lists (does not apply to nested lists):

handler.SetListVerticalPadding(40);

Supported HTML Elements

Block Elements

TagDescription
divGeneric container
pParagraph
h1 - h6Headings (with default font sizes)
ul, ol, liLists (unordered, ordered, list items)
table, thead, tbody, tr, th, tdTables (with colspan/rowspan support)
blockquoteBlock quotation with left border
prePreformatted text (preserves whitespace, monospace font)
hrHorizontal rule
section, header, footerSemantic containers

Inline Elements

TagDescription
aHyperlinks
b, strongBold text
i, emItalic text
uUnderlined text
s, strike, delStrikethrough text
sub, supSubscript/superscript
smallSmaller text
brLine break
imgImages (supports URLs and base64)
spanInline container
codeInline code (monospace with background)
kbdKeyboard input
markHighlighted text
abbrAbbreviation
qInline quotation
varVariable
sampSample output

CSS Inline Style Support

The library supports parsing inline style attributes on HTML elements.

Supported Text Properties

  • color - Text color
  • background-color - Text background
  • font-size - Font size (px, pt, em, rem, cm, mm, in)
  • font-weight - Bold, normal, light, or numeric (100-900)
  • font-style - Italic, normal
  • font-family - Font family name
  • text-decoration - Underline, line-through
  • letter-spacing - Letter spacing
  • line-height - Line height

Supported Container Properties

  • padding, padding-top, padding-bottom, padding-left, padding-right
  • margin (converted to padding)
  • background-color
  • border, border-color
  • width, height, min-width, max-width, min-height, max-height
  • text-align - Left, center, right

Supported Color Formats

  • Hex: #RGB, #RRGGBB, #RRGGBBAA
  • RGB: rgb(r, g, b), rgba(r, g, b, a)
  • Named colors: red, blue, green, etc.

Example

<pstyle="color: #e74c3c; font-size: 18px; font-weight: bold;">
Styled text with CSS
</p><divstyle="background-color: #f0f0f0; padding: 10px; border: 1px;">
Container with background and padding
</div>

Example Application

You can use HTMLToQPDF.Example to try out the capabilities of this extension.

Default StylesOptions for changing styles

Changelog

See CHANGELOG.md for version history and release notes.

License

MIT License - see the LICENSE file for details.

Credits

Originally created by Relorer.

About

HTMLToQPDF is an extension for QuestPDF that allows to generate PDF from HTML

Resources

Stars

15 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages