Skip to content

Commit af1fb78

Browse files
authored
feat(Html2Pdf): add PdfOptions parameter (#632)
* refactor: 消除警告信息 * feat: 增加 PdfOptions 支持 * chore: bump version 9.0.6-beta01
1 parent 8a68e03 commit af1fb78

3 files changed

Lines changed: 29 additions & 36 deletions

File tree

src/components/BootstrapBlazor.Html2Pdf/BootstrapBlazor.Html2Pdf.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.0.4</Version>
4+
<Version>9.0.6-beta01</Version>
55
</PropertyGroup>
66

77
<PropertyGroup>
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="BootstrapBlazor" Version="$(BBVersion)" />
13+
<PackageReference Include="BootstrapBlazor" Version="9.11.5-beta07" />
1414
<PackageReference Include="PuppeteerSharp" Version="20.2.4" />
1515
</ItemGroup>
1616

src/components/BootstrapBlazor.Html2Pdf/Extensions/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
1+
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
// Website: https://www.blazor.zone or https://argozhang.github.io/
44

src/components/BootstrapBlazor.Html2Pdf/Services/DefaultPdfService.cs

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
1+
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
// Website: https://www.blazor.zone or https://argozhang.github.io/
44

@@ -19,18 +19,15 @@ class DefaultPdfService(ILogger<DefaultPdfService> logger) : IHtml2Pdf
1919
/// </summary>
2020
public IWebProxy? WebProxy { get; set; }
2121

22-
/// <summary>
23-
/// <inheritdoc/>
24-
/// </summary>
25-
public async Task<byte[]> PdfDataAsync(string url)
22+
public async Task<byte[]> PdfDataAsync(string url, PdfOptions? options = null)
2623
{
2724
try
2825
{
2926
await using var browser = await LaunchBrowserAsync();
3027
await using var page = await browser.NewPageAsync();
3128

3229
await page.GoToAsync(url);
33-
return await page.PdfDataAsync();
30+
return await page.PdfDataAsync(GetOptions(options));
3431
}
3532
catch (Exception ex)
3633
{
@@ -39,18 +36,15 @@ public async Task<byte[]> PdfDataAsync(string url)
3936
}
4037
}
4138

42-
/// <summary>
43-
/// <inheritdoc/>
44-
/// </summary>
45-
public async Task<Stream> PdfStreamAsync(string url)
39+
public async Task<Stream> PdfStreamAsync(string url, PdfOptions? options = null)
4640
{
4741
try
4842
{
4943
await using var browser = await LaunchBrowserAsync();
5044
await using var page = await browser.NewPageAsync();
5145
await page.GoToAsync(url);
5246

53-
return await page.PdfStreamAsync();
47+
return await page.PdfStreamAsync(GetOptions(options));
5448
}
5549
catch (Exception ex)
5650
{
@@ -59,13 +53,7 @@ public async Task<Stream> PdfStreamAsync(string url)
5953
}
6054
}
6155

62-
/// <summary>
63-
/// Export method
64-
/// </summary>
65-
/// <param name="html">html raw string</param>
66-
/// <param name="links"></param>
67-
/// <param name="scripts"></param>
68-
public async Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null)
56+
public async Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null, PdfOptions? options = null)
6957
{
7058
try
7159
{
@@ -76,7 +64,7 @@ public async Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>?
7664
await AddStyleTagAsync(page, links);
7765
await AddScriptTagAsync(page, scripts);
7866

79-
return await page.PdfDataAsync();
67+
return await page.PdfDataAsync(GetOptions(options));
8068
}
8169
catch (Exception ex)
8270
{
@@ -85,13 +73,7 @@ public async Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>?
8573
}
8674
}
8775

88-
/// <summary>
89-
/// Export method
90-
/// </summary>
91-
/// <param name="html">html raw string</param>
92-
/// <param name="links"></param>
93-
/// <param name="scripts"></param>
94-
public async Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null)
76+
public async Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null, PdfOptions? options = null)
9577
{
9678
try
9779
{
@@ -102,7 +84,7 @@ public async Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string
10284
await AddStyleTagAsync(page, links);
10385
await AddScriptTagAsync(page, scripts);
10486

105-
return await page.PdfStreamAsync();
87+
return await page.PdfStreamAsync(GetOptions(options));
10688
}
10789
catch (Exception ex)
10890
{
@@ -111,6 +93,14 @@ public async Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string
11193
}
11294
}
11395

96+
private static PuppeteerSharp.PdfOptions GetOptions(PdfOptions? options)
97+
{
98+
return options == null ? new PuppeteerSharp.PdfOptions() : new PuppeteerSharp.PdfOptions
99+
{
100+
Landscape = options.Landscape
101+
};
102+
}
103+
114104
private static async Task AddStyleTagAsync(IPage page, IEnumerable<string>? links = null)
115105
{
116106
var styles = new List<string>();
@@ -161,13 +151,16 @@ private async Task<IBrowser> LaunchBrowserAsync()
161151

162152
private void Log(Exception? exception, string? message, params object?[] args)
163153
{
164-
if (args.Length != 0)
165-
{
166-
logger.LogInformation(exception, "{Message} | Args: {Args}", message, args);
167-
}
168-
else
154+
if (logger.IsEnabled(LogLevel.Information))
169155
{
170-
logger.LogInformation(exception, "{Message}", message);
156+
if (args.Length != 0)
157+
{
158+
logger.LogInformation(exception, "{Message} | Args: {Args}", message, args);
159+
}
160+
else
161+
{
162+
logger.LogInformation(exception, "{Message}", message);
163+
}
171164
}
172165
}
173166
}

0 commit comments

Comments
 (0)