Skip to content

Commit 045fa2e

Browse files
authored
feat(Html2Pdf): add PdfOptions parameter (#7013)
* feat: 增加 PdfOptions 配置项 * chore: bump version 9.11.5-beta07 * test: 更新单元测试 * test: 增加单元测试
1 parent b3f199e commit 045fa2e

File tree

6 files changed

+45
-32
lines changed

6 files changed

+45
-32
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

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

33
<PropertyGroup Condition="'$(VisualStudioVersion)' == '17.0'">
4-
<Version>9.11.5-beta06</Version>
4+
<Version>9.11.5-beta07</Version>
55
</PropertyGroup>
66

77
<PropertyGroup Condition="'$(VisualStudioVersion)' == '18.0'">
8-
<Version>10.0.0-rc.2.1.5</Version>
8+
<Version>10.0.0-rc.2.1.6</Version>
99
</PropertyGroup>
1010

1111
<ItemGroup>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
namespace BootstrapBlazor.Components;
7+
8+
/// <summary>
9+
/// PdfOptions 实例用于设置导出 Pdf 相关选项
10+
/// </summary>
11+
public class PdfOptions
12+
{
13+
/// <summary>
14+
/// 获得/设置 是否横向打印 默认 false
15+
/// </summary>
16+
public bool Landscape { get; set; }
17+
}

src/BootstrapBlazor/Services/DefaultHtml2PdfService.cs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,11 @@ class DefaultHtml2PdfService : IHtml2Pdf
1212
{
1313
private const string ErrorMessage = "请增加依赖包 BootstrapBlazor.Html2Pdf 通过 AddBootstrapBlazorHtml2PdfService 进行服务注入; Please add BootstrapBlazor.Html2Pdf package and use AddBootstrapBlazorHtml2PdfService inject service";
1414

15-
/// <summary>
16-
/// <inheritdoc/>
17-
/// </summary>
18-
public Task<byte[]> PdfDataAsync(string url) => throw new NotImplementedException(ErrorMessage);
15+
public Task<byte[]> PdfDataAsync(string url, PdfOptions? options = null) => throw new NotImplementedException(ErrorMessage);
1916

20-
/// <summary>
21-
/// <inheritdoc/>
22-
/// </summary>
23-
public Task<Stream> PdfStreamAsync(string url) => throw new NotImplementedException(ErrorMessage);
17+
public Task<Stream> PdfStreamAsync(string url, PdfOptions? options = null) => throw new NotImplementedException(ErrorMessage);
2418

25-
/// <summary>
26-
/// Export method
27-
/// </summary>
28-
/// <param name="html">html raw string</param>
29-
/// <param name="links"></param>
30-
/// <param name="scripts"></param>
31-
public Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null) => throw new NotImplementedException(ErrorMessage);
19+
public Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null, PdfOptions? options = null) => throw new NotImplementedException(ErrorMessage);
3220

33-
/// <summary>
34-
/// Export method
35-
/// </summary>
36-
/// <param name="html">html raw string</param>
37-
/// <param name="links"></param>
38-
/// <param name="scripts"></param>
39-
public Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null) => throw new NotImplementedException(ErrorMessage);
21+
public Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null, PdfOptions? options = null) => throw new NotImplementedException(ErrorMessage);
4022
}

src/BootstrapBlazor/Services/IHtml2Pdf.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,31 @@ public interface IHtml2Pdf
2222
/// Export method
2323
/// </summary>
2424
/// <param name="url">url</param>
25-
Task<byte[]> PdfDataAsync(string url);
25+
/// <param name="options"></param>
26+
Task<byte[]> PdfDataAsync(string url, PdfOptions? options = null);
2627

2728
/// <summary>
2829
/// Export method
2930
/// </summary>
3031
/// <param name="url">url</param>
31-
Task<Stream> PdfStreamAsync(string url);
32+
/// <param name="options"></param>
33+
Task<Stream> PdfStreamAsync(string url, PdfOptions? options = null);
3234

3335
/// <summary>
3436
/// Export method
3537
/// </summary>
3638
/// <param name="html">html raw string</param>
3739
/// <param name="links"></param>
3840
/// <param name="scripts"></param>
39-
Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null);
41+
/// <param name="options"></param>
42+
Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null, PdfOptions? options = null);
4043

4144
/// <summary>
4245
/// Export method
4346
/// </summary>
4447
/// <param name="html">html raw string</param>
4548
/// <param name="links"></param>
4649
/// <param name="scripts"></param>
47-
Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null);
50+
/// <param name="options"></param>
51+
Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null, PdfOptions? options = null);
4852
}

test/UnitTest/Components/ExportPdfButtonTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,21 +157,21 @@ public void ExportPdfButtonSettings_Ok()
157157

158158
class MockHtml2PdfService : IHtml2Pdf
159159
{
160-
public Task<byte[]> PdfDataAsync(string url)
160+
public Task<byte[]> PdfDataAsync(string url, PdfOptions? options = null)
161161
{
162162
throw new NotImplementedException();
163163
}
164164

165-
public Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null)
165+
public Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null, PdfOptions? options = null)
166166
{
167167
throw new NotImplementedException();
168168
}
169169

170-
public Task<Stream> PdfStreamAsync(string url)
170+
public Task<Stream> PdfStreamAsync(string url, PdfOptions? options = null)
171171
{
172172
throw new NotImplementedException();
173173
}
174174

175-
public Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null) => Task.FromResult<Stream>(new MemoryStream(Encoding.UTF8.GetBytes("Hello World")));
175+
public Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null, PdfOptions? options = null) => Task.FromResult<Stream>(new MemoryStream(Encoding.UTF8.GetBytes("Hello World")));
176176
}
177177
}

test/UnitTest/Services/Html2PdfServiceTest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,14 @@ public async Task ExportPdf_Error()
2121
await Assert.ThrowsAsync<NotImplementedException>(() => pdfService.PdfDataFromHtmlAsync("<h2>Test</h2>"));
2222
await Assert.ThrowsAsync<NotImplementedException>(() => pdfService.PdfStreamFromHtmlAsync("<h2>Test</h2>"));
2323
}
24+
25+
[Fact]
26+
public void PdfOptions_Ok()
27+
{
28+
var options = new PdfOptions
29+
{
30+
Landscape = true
31+
};
32+
Assert.True(options.Landscape);
33+
}
2434
}

0 commit comments

Comments
 (0)