Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,6 @@ MigrationBackup/
.ionide/

# Fody - auto-generated XML schema
FodyWeavers.xsd
FodyWeavers.xsd

.idea/
2 changes: 1 addition & 1 deletion HTMLToQPDF.Example/Assets/testHtml.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<img src="https://images.pexels.com/photos/7961265/pexels-photo-7961265.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" />
</a>
</div>
<p>Lorem</p>
<p class="underline">Lorem</p>
<h1>h1</h1>
<h2><s>h2</s></h2>
<h3>h3</h3>
Expand Down
4 changes: 3 additions & 1 deletion HTMLToQPDF.Example/Utilities/PDFCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static void Create(string html, string path, bool customStyles)
page.MarginVertical(1f, Unit.Centimetre);

page.DefaultTextStyle(TextStyle.Default
.FontFamily("MS Reference Sans Serif")
.Fallback(y => y.FontFamily("MS Reference Sans Serif")
.Fallback(y => y.FontFamily("Segoe UI Emoji")
.Fallback(y => y.FontFamily("Microsoft YaHei")))));
Expand All @@ -28,7 +29,8 @@ public static void Create(string html, string path, bool customStyles)
{
if (customStyles)
{
handler.SetTextStyleForHtmlElement("h1", TextStyle.Default.FontColor(Colors.DeepOrange.Accent4).FontSize(32).Bold());
handler.SetTextStyleForHtmlElement("h1", t => t.FontColor(Colors.DeepOrange.Accent4).FontSize(32).Bold());
handler.SetCssStyleForHtmlElement("underline", t => t.Underline());
handler.SetContainerStyleForHtmlElement("div", c => c.Background(Colors.Teal.Lighten5));
handler.SetContainerStyleForHtmlElement("img", c => c.MaxHeight(7, Unit.Centimetre));
handler.SetContainerStyleForHtmlElement("table", c => c.Background(Colors.Pink.Lighten5));
Expand Down
35 changes: 19 additions & 16 deletions HTMLToQPDF/Components/HTMLComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,26 @@ internal class HTMLComponent : IComponent
{
public GetImgBySrc GetImgBySrc { get; set; } = ImgUtils.GetImgBySrc;

public Dictionary<string, TextStyle> TextStyles { get; } = new Dictionary<string, TextStyle>()
public Dictionary<string, Func<TextStyle, TextStyle>> TextStyles { get; } = new()
{
{ "h1", TextStyle.Default.FontSize(32).Bold() },
{ "h2", TextStyle.Default.FontSize(28).Bold() },
{ "h3", TextStyle.Default.FontSize(24).Bold() },
{ "h4", TextStyle.Default.FontSize(20).Bold() },
{ "h5", TextStyle.Default.FontSize(16).Bold() },
{ "h6", TextStyle.Default.FontSize(12).Bold() },
{ "b", TextStyle.Default.Bold() },
{ "i", TextStyle.Default.Italic() },
{ "small", TextStyle.Default.Light() },
{ "strike", TextStyle.Default.Strikethrough() },
{ "s", TextStyle.Default.Strikethrough() },
{ "u", TextStyle.Default.Underline() },
{ "a", TextStyle.Default.Underline() },
{ "h1", t => t.FontSize(32).Bold() },
{ "h2", t => t.FontSize(28).Bold() },
{ "h3", t => t.FontSize(24).Bold() },
{ "h4", t => t.FontSize(20).Bold() },
{ "h5", t => t.FontSize(16).Bold() },
{ "h6", t => t.FontSize(12).Bold() },
{ "b", t => t.Bold() },
{ "i", t => t.Italic() },
{ "small", t => t.Light() },
{ "strike", t => t.Strikethrough() },
{ "s", t => t.Strikethrough() },
{ "u", t => t.Underline() },
{ "a", t => t.Underline() },
};

public Dictionary<string, Func<IContainer, IContainer>> ContainerStyles { get; } = new Dictionary<string, Func<IContainer, IContainer>>()
public Dictionary<string, Func<TextStyle, TextStyle>> CssStyles { get; } = new();

public Dictionary<string, Func<IContainer, IContainer>> ContainerStyles { get; } = new()
{
{ "p", c => c.PaddingVertical(6) },
{ "ul", c => c.PaddingLeft(30) },
Expand All @@ -52,7 +54,8 @@ public void Compose(IContainer container)

CreateSeparateBranchesForTextNodes(node);

container.Component(node.GetComponent(new HTMLComponentsArgs(TextStyles, ContainerStyles, ListVerticalPadding, GetImgBySrc)));
var args = new HTMLComponentsArgs(TextStyles, CssStyles, ContainerStyles, ListVerticalPadding, GetImgBySrc);
container.Component(node.GetComponent(args));
}

/// <summary>
Expand Down
21 changes: 6 additions & 15 deletions HTMLToQPDF/Components/HTMLComponentsArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,10 @@ namespace HTMLToQPDF.Components
{
public delegate byte[]? GetImgBySrc(string src);

internal class HTMLComponentsArgs
{
public Dictionary<string, TextStyle> TextStyles { get; }
public Dictionary<string, Func<IContainer, IContainer>> ContainerStyles { get; }
public float ListVerticalPadding { get; }
public GetImgBySrc GetImgBySrc { get; }

public HTMLComponentsArgs(Dictionary<string, TextStyle> textStyles, Dictionary<string, Func<IContainer, IContainer>> containerStyles, float listVerticalPadding, GetImgBySrc getImgBySrc)
{
TextStyles = textStyles;
ContainerStyles = containerStyles;
ListVerticalPadding = listVerticalPadding;
GetImgBySrc = getImgBySrc;
}
}
internal record HTMLComponentsArgs(
Dictionary<string, Func<TextStyle, TextStyle>> TextStyles,
Dictionary<string, Func<TextStyle, TextStyle>> CssStyles,
Dictionary<string, Func<IContainer, IContainer>> ContainerStyles,
float ListVerticalPadding,
GetImgBySrc GetImgBySrc);
}
10 changes: 8 additions & 2 deletions HTMLToQPDF/Components/ParagraphComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ namespace HTMLQuestPDF.Components
internal class ParagraphComponent : IComponent
{
private readonly List<HtmlNode> lineNodes;
private readonly Dictionary<string, TextStyle> textStyles;
private readonly Dictionary<string, Func<TextStyle, TextStyle>> textStyles;
private readonly Dictionary<string, Func<TextStyle, TextStyle>> cssStyles;

public ParagraphComponent(List<HtmlNode> lineNodes, HTMLComponentsArgs args)
{
this.lineNodes = lineNodes;
this.textStyles = args.TextStyles;
this.cssStyles = args.CssStyles;
}

private HtmlNode? GetParrentBlock(HtmlNode node)
Expand Down Expand Up @@ -109,7 +111,11 @@ public TextSpanAction GetTextStyles(HtmlNode element)

public TextStyle GetTextStyle(HtmlNode element)
{
return textStyles.TryGetValue(element.Name.ToLower(), out TextStyle? style) ? style : TextStyle.Default;
var style = TextStyle.Default;
foreach (var cssName in element.GetClasses())
if (cssStyles.TryGetValue(cssName, out var cssStyle))
style = cssStyle(style);
return textStyles.TryGetValue(element.Name.ToLower(), out var textStyle) ? textStyle(style) : style;
}
}
}
7 changes: 6 additions & 1 deletion HTMLToQPDF/HTMLDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ public void OverloadImgReceivingFunc(GetImgBySrc getImg)
PDFPage.GetImgBySrc = getImg;
}

public void SetTextStyleForHtmlElement(string tagName, TextStyle style)
public void SetTextStyleForHtmlElement(string tagName, Func<TextStyle, TextStyle> style)
{
PDFPage.TextStyles[tagName.ToLower()] = style;
}

public void SetCssStyleForHtmlElement(string cssName, Func<TextStyle, TextStyle> style)
{
PDFPage.CssStyles[cssName.ToLower()] = style;
}

public void SetContainerStyleForHtmlElement(string tagName, Func<IContainer, IContainer> style)
{
PDFPage.ContainerStyles[tagName.ToLower()] = style;
Expand Down
5 changes: 3 additions & 2 deletions HTMLToQPDF/StyleSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace HTMLQuestPDF
{
internal static class HTMLMapSettings
{
public static readonly string[] LineElements = new string[] {
public static readonly string[] LineElements = {
"a",
"b",
"br",
Expand All @@ -24,7 +24,8 @@ internal static class HTMLMapSettings
"tr",
"u",
"img",
"text"
"text",
"span"
};

public static readonly string[] BlockElements = new string[] {
Expand Down