Skip to content

Commit 2b2316f

Browse files
authored
Merge pull request #14 from jbennie/debug_ul_error
Debug ul error
2 parents ed55547 + f114aec commit 2b2316f

File tree

5 files changed

+58
-9
lines changed

5 files changed

+58
-9
lines changed

HTMLToQPDF.Example/HTMLToQPDF.Example.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</PackageReference>
2525
<PackageReference Include="HandyControls" Version="3.4.2" />
2626
<PackageReference Include="PropertyChanged.Fody" Version="4.0.4" />
27-
<PackageReference Include="QuestPDF" Version="2022.9.0" />
27+
<PackageReference Include="QuestPDF" Version="2023.12.5" />
2828
</ItemGroup>
2929

3030
<ItemGroup>

HTMLToQPDF.Example/Utilities/PDFCreator.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ internal static class PDFCreator
99
{
1010
public static void Create(string html, string path, bool customStyles)
1111
{
12+
13+
QuestPDF.Settings.License = LicenseType.Community;
1214
Document.Create(container =>
1315
{
1416
container.Page(page =>
@@ -17,10 +19,14 @@ public static void Create(string html, string path, bool customStyles)
1719
page.MarginHorizontal(0.5f, Unit.Centimetre);
1820
page.MarginVertical(1f, Unit.Centimetre);
1921

20-
page.DefaultTextStyle(TextStyle.Default
21-
.Fallback(y => y.FontFamily("MS Reference Sans Serif")
22+
page.DefaultTextStyle(TextStyle.Default.FontFamily("Arial").FontSize(8).Fallback(y => y.FontFamily("Segoe UI Emoji")));
23+
24+
/* page.DefaultTextStyle(TextStyle.Default
25+
// .Fallback(y => y.FontFamily("MS Reference Sans Serif")
2226
.Fallback(y => y.FontFamily("Segoe UI Emoji")
23-
.Fallback(y => y.FontFamily("Microsoft YaHei")))));
27+
// .Fallback(y => y.FontFamily("Microsoft YaHei")))
28+
));
29+
*/
2430

2531
page.Content().Column(col =>
2632
{

HTMLToQPDF/Components/Tags/ImgComponent.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ protected override void ComposeSingle(IContainer container)
1919
{
2020
var src = node.GetAttributeValue("src", "");
2121
var img = getImgBySrc(src) ?? Placeholders.Image(200, 100);
22-
container.Image(img, ImageScaling.FitArea);
22+
container.AlignCenter().Image(img).FitArea();
23+
2324
}
2425
}
2526
}

HTMLToQPDF/HTMLToQPDF.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
</ItemGroup>
2626

2727
<ItemGroup>
28-
<PackageReference Include="HtmlAgilityPack" Version="1.11.46" />
29-
<PackageReference Include="QuestPDF" Version="2022.9.0" />
28+
<PackageReference Include="HtmlAgilityPack" Version="1.11.59" />
29+
<PackageReference Include="QuestPDF" Version="2023.12.5" />
3030
</ItemGroup>
3131

3232
<ItemGroup>

HTMLToQPDF/Utils/ImgUtils.cs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,28 @@ namespace HTMLToQPDF.Utils
44
{
55
internal static class ImgUtils
66
{
7+
8+
private static HttpClient _SingletonClient;
9+
10+
static ImgUtils() {
11+
12+
if (_SingletonClient == null)
13+
{
14+
_SingletonClient = new HttpClient();
15+
}
16+
}
17+
18+
/// <summary>
19+
/// Call this manually at the end of the appliction life : Static classes don't auto Dispose.
20+
/// </summary>
21+
public static void Dispose() {
22+
23+
if (_SingletonClient != null)
24+
{
25+
_SingletonClient.Dispose();
26+
}
27+
}
28+
729
public static byte[]? GetImgBySrc(string src)
830
{
931
try
@@ -13,13 +35,33 @@ internal static class ImgUtils
1335
var base64 = src.Substring(src.IndexOf("base64,") + "base64,".Length);
1436
return Convert.FromBase64String(base64);
1537
}
16-
var webClient = new WebClient();
17-
return webClient.DownloadData(src);
38+
39+
else {
40+
return Download(src).Result;
41+
}
42+
43+
1844
}
1945
catch
2046
{
2147
return null;
2248
}
2349
}
50+
51+
private static Task<byte[]> Download(string src)
52+
{
53+
54+
if (_SingletonClient != null)
55+
{
56+
Uri uri = new Uri(src);
57+
return _SingletonClient.GetByteArrayAsync(uri);
58+
}
59+
else
60+
{
61+
// To prevent memory and IO socket Leaks HttpClient should be a singleton for life.
62+
throw new Exception("HttpClient not Available");
63+
}
64+
}
65+
2466
}
2567
}

0 commit comments

Comments
 (0)