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
40 changes: 39 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,42 @@ MigrationBackup/
.ionide/

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

# Created by https://www.toptal.com/developers/gitignore/api/macos
# Edit at https://www.toptal.com/developers/gitignore?templates=macos

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### macOS Patch ###
# iCloud generated files
*.icloud

# End of https://www.toptal.com/developers/gitignore/api/macos
7 changes: 6 additions & 1 deletion HTMLToQPDF/Components/HTMLComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ internal class HTMLComponent : IComponent
{ "ol", c => c.PaddingLeft(30) }
};

public Dictionary<string, TextHorizontalAlignment> TextAlignments { get; } = new Dictionary<string, TextHorizontalAlignment>()
{
{ "p", TextHorizontalAlignment.Left }
};

public float ListVerticalPadding { get; set; } = 12;

public string HTML { get; set; } = "";
Expand All @@ -58,7 +63,7 @@ public void Compose(IContainer container)

CreateSeparateBranchesForTextNodes(node);

container.Component(node.GetComponent(new HTMLComponentsArgs(TextStyles, ContainerStyles, ListVerticalPadding, GetImgBySrc)));
container.Component(node.GetComponent(new HTMLComponentsArgs(TextStyles, ContainerStyles, TextAlignments, ListVerticalPadding, GetImgBySrc)));
}

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion HTMLToQPDF/Components/HTMLComponentsArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ internal class HTMLComponentsArgs
{
public Dictionary<string, TextStyle> TextStyles { get; }
public Dictionary<string, Func<IContainer, IContainer>> ContainerStyles { get; }
public Dictionary<string, TextHorizontalAlignment> TextAlignments { 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)
public HTMLComponentsArgs(Dictionary<string, TextStyle> textStyles, Dictionary<string, Func<IContainer, IContainer>> containerStyles, Dictionary<string, TextHorizontalAlignment> textAlignments, float listVerticalPadding, GetImgBySrc getImgBySrc)
{
TextStyles = textStyles;
ContainerStyles = containerStyles;
TextAlignments = textAlignments;
ListVerticalPadding = listVerticalPadding;
GetImgBySrc = getImgBySrc;
}
Expand Down
48 changes: 39 additions & 9 deletions HTMLToQPDF/Components/ParagraphComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ internal class ParagraphComponent : IComponent
{
private readonly List<HtmlNode> lineNodes;
private readonly Dictionary<string, TextStyle> textStyles;
private readonly Dictionary<string, TextHorizontalAlignment> textAlignments;

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

private HtmlNode? GetParrentBlock(HtmlNode node)
private HtmlNode? GetParentBlock(HtmlNode node)
{
if (node == null) return null;
return node.IsBlockNode() ? node : GetParrentBlock(node.ParentNode);
return node.IsBlockNode() ? node : GetParentBlock(node.ParentNode);
}

private HtmlNode? GetListItemNode(HtmlNode node)
Expand All @@ -31,7 +33,7 @@ public ParagraphComponent(List<HtmlNode> lineNodes, HTMLComponentsArgs args)

public void Compose(IContainer container)
{
var listItemNode = GetListItemNode(lineNodes.First()) ?? GetParrentBlock(lineNodes.First());
var listItemNode = GetListItemNode(lineNodes.First()) ?? GetParentBlock(lineNodes.First());
if (listItemNode == null) return;

var numberInList = listItemNode.GetNumberInList();
Expand Down Expand Up @@ -70,12 +72,12 @@ private Action<TextDescriptor> GetAction(HtmlNode node)
if (node.NodeType == HtmlNodeType.Text)
{
var span = text.Span(node.InnerText);
GetTextSpanAction(node).Invoke(span);
GetTextSpanAction(node).Invoke(span, text);
}
else if (node.IsBr())
{
var span = text.Span("\n");
GetTextSpanAction(node).Invoke(span);
GetTextSpanAction(node).Invoke(span, text);
}
else
{
Expand All @@ -88,16 +90,23 @@ private Action<TextDescriptor> GetAction(HtmlNode node)
};
}

private TextSpanAction GetTextSpanAction(HtmlNode node)
private Action<TextSpanDescriptor, TextDescriptor> GetTextSpanAction(HtmlNode node)
{
return spanAction =>
return (spanAction, text) =>
{
var action = GetTextStyles(node);
action(spanAction);

var alignment = GetTextAlignment(node);
alignment(text);

if (node.ParentNode != null)
{
var parrentAction = GetTextSpanAction(node.ParentNode);
parrentAction(spanAction);
var parentAction = GetTextSpanAction(node.ParentNode);
parentAction(spanAction, text);

var parentAlignment = GetTextAlignment(node.ParentNode);
parentAlignment(text);
}
};
}
Expand All @@ -111,5 +120,26 @@ public TextStyle GetTextStyle(HtmlNode element)
{
return textStyles.TryGetValue(element.Name.ToLower(), out TextStyle? style) ? style : TextStyle.Default;
}

public Action<TextDescriptor> GetTextAlignment(HtmlNode element)
{
switch (textAlignments.TryGetValue(element.Name.ToLower(), out TextHorizontalAlignment alignment) ? alignment : TextHorizontalAlignment.Left)
{
case TextHorizontalAlignment.Left:
return block => block.AlignLeft();
case TextHorizontalAlignment.Center:
return block => block.AlignCenter();
case TextHorizontalAlignment.Right:
return block => block.AlignRight();
case TextHorizontalAlignment.Start:
return block => block.AlignStart();
case TextHorizontalAlignment.End:
return block => block.AlignEnd();
case TextHorizontalAlignment.Justify:
return block => block.Justify();
default:
return block => block.AlignLeft();
}
}
}
}
5 changes: 5 additions & 0 deletions HTMLToQPDF/HTMLDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public void SetTextStyleForHtmlElement(string tagName, TextStyle style)
PDFPage.TextStyles[tagName.ToLower()] = style;
}

public void SetTextAlignmentForHtmlElement(string tagName, TextHorizontalAlignment alignment)
{
PDFPage.TextAlignments[tagName.ToLower()] = alignment;
}

public void SetContainerStyleForHtmlElement(string tagName, Func<IContainer, IContainer> style)
{
PDFPage.ContainerStyles[tagName.ToLower()] = style;
Expand Down
2 changes: 1 addition & 1 deletion HTMLToQPDF/HTMLToQPDF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

<ItemGroup>
<PackageReference Include="HtmlAgilityPack" Version="1.11.59" />
<PackageReference Include="QuestPDF" Version="2023.12.5" />
<PackageReference Include="QuestPDF" Version="2024.7.2" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ handler.SetTextStyleForHtmlElement("div", TextStyle.Default.FontColor(Colors.Gre
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));
handler.SetTextAlignmentForHtmlElement("p", TextHorizontalAlignment.Justify);
```

You can set the vertical padding size for lists. This padding will not apply to sub-lists:
Expand Down