Skip to content

Commit eea9569

Browse files
authored
feat(Html2Pdf): add PaperFormat parameter (#7036)
* refactor: 增加 PdfOptions 配置项 * doc: 更新示例增加 Options 配置 * chore: bump version 9.11.5-beta11 * chore: 更新 Html2Pdf 依赖包 * test: 增加单元测试
1 parent a55507d commit eea9569

File tree

7 files changed

+192
-4
lines changed

7 files changed

+192
-4
lines changed

src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<PackageReference Include="BootstrapBlazor.Gantt" Version="9.0.2" />
4545
<PackageReference Include="BootstrapBlazor.Holiday" Version="9.0.1" />
4646
<PackageReference Include="BootstrapBlazor.Html2Image" Version="9.0.2" />
47-
<PackageReference Include="BootstrapBlazor.Html2Pdf" Version="9.0.6-beta01" />
47+
<PackageReference Include="BootstrapBlazor.Html2Pdf" Version="10.0.0-rc.2.1.2" />
4848
<PackageReference Include="BootstrapBlazor.IconPark" Version="9.0.3" />
4949
<PackageReference Include="BootstrapBlazor.ImageCropper" Version="9.0.3" />
5050
<PackageReference Include="BootstrapBlazor.IP2Region" Version="9.0.4" />

src/BootstrapBlazor.Server/Components/Samples/Html2Pdfs.razor.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ private async Task OnExportAsync()
8686
""";
8787

8888
// 增加网页所需样式表文件
89-
using var stream = await Html2PdfService.PdfStreamFromHtmlAsync(htmlString, [$"{NavigationManager.BaseUri}_content/BootstrapBlazor/css/bootstrap.blazor.bundle.min.css"]);
89+
using var stream = await Html2PdfService.PdfStreamFromHtmlAsync(htmlString, [$"{NavigationManager.BaseUri}_content/BootstrapBlazor/css/bootstrap.blazor.bundle.min.css"], options: new PdfOptions()
90+
{
91+
Format = PaperFormat.A4
92+
});
9093

9194
// 下载 Pdf 文件
9295
await DownloadService.DownloadFromStreamAsync($"table-{DateTime.Now:HHmmss}.pdf", stream);

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-beta10</Version>
4+
<Version>9.11.5-beta11</Version>
55
</PropertyGroup>
66

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

1111
<ItemGroup>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
/// 页面边距选项
10+
/// </summary>
11+
[ExcludeFromCodeCoverage]
12+
public record MarginOptions
13+
{
14+
/// <summary>
15+
/// Top margin, accepts values labeled with units.
16+
/// </summary>
17+
public object? Top { get; set; }
18+
19+
/// <summary>
20+
/// Left margin, accepts values labeled with units.
21+
/// </summary>
22+
public object? Left { get; set; }
23+
24+
/// <summary>
25+
/// Bottom margin, accepts values labeled with units.
26+
/// </summary>
27+
public object? Bottom { get; set; }
28+
29+
/// <summary>
30+
/// Right margin, accepts values labeled with units.
31+
/// </summary>
32+
public object? Right { get; set; }
33+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
/// 纸张规格配置类
10+
/// </summary>
11+
[ExcludeFromCodeCoverage]
12+
public record PaperFormat
13+
{
14+
/// <summary>
15+
///
16+
/// </summary>
17+
public static PaperFormat Letter => new(8.5m, 11m);
18+
19+
/// <summary>
20+
///
21+
/// </summary>
22+
public static PaperFormat Legal => new(8.5m, 14m);
23+
24+
/// <summary>
25+
///
26+
/// </summary>
27+
public static PaperFormat Tabloid => new(11m, 17m);
28+
29+
/// <summary>
30+
///
31+
/// </summary>
32+
public static PaperFormat Ledger => new(17m, 11m);
33+
34+
/// <summary>
35+
///
36+
/// </summary>
37+
public static PaperFormat A0 => new(33.1102m, 46.811m);
38+
39+
/// <summary>
40+
///
41+
/// </summary>
42+
public static PaperFormat A1 => new(23.3858m, 33.1102m);
43+
44+
/// <summary>
45+
///
46+
/// </summary>
47+
public static PaperFormat A2 => new(16.5354m, 23.3858m);
48+
49+
/// <summary>
50+
///
51+
/// </summary>
52+
public static PaperFormat A3 => new(11.6929m, 16.5354m);
53+
54+
/// <summary>
55+
///
56+
/// </summary>
57+
public static PaperFormat A4 => new(8.2677m, 11.6929m);
58+
59+
/// <summary>
60+
///
61+
/// </summary>
62+
public static PaperFormat A5 => new(5.8268m, 8.2677m);
63+
64+
/// <summary>
65+
///
66+
/// </summary>
67+
public static PaperFormat A6 => new(4.1339m, 5.8268m);
68+
69+
/// <summary>
70+
///
71+
/// </summary>
72+
public decimal Width { get; set; }
73+
74+
/// <summary>
75+
///
76+
/// </summary>
77+
public decimal Height { get; set; }
78+
79+
/// <summary>
80+
///
81+
/// </summary>
82+
/// <param name="width"></param>
83+
/// <param name="height"></param>
84+
public PaperFormat(decimal width, decimal height)
85+
{
86+
Width = width;
87+
Height = height;
88+
}
89+
}

src/BootstrapBlazor/Options/PdfOptions.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,29 @@ public class PdfOptions
1414
/// 获得/设置 是否横向打印 默认 false
1515
/// </summary>
1616
public bool Landscape { get; set; }
17+
18+
/// <summary>
19+
/// 获得/设置 是否打印背景色 默认 false
20+
/// </summary>
21+
public bool PrintBackground { get; set; }
22+
23+
/// <summary>
24+
/// 获得/设置 纸张格式 默认 A4
25+
/// </summary>
26+
public PaperFormat Format { get; set; } = PaperFormat.A4;
27+
28+
/// <summary>
29+
/// 获得/设置 是否显示页眉页脚 默认 false
30+
/// </summary>
31+
public bool DisplayHeaderFooter { get; set; }
32+
33+
/// <summary>
34+
/// 获得/设置 放大比例 默认 1 取值 0.1 到 2 之间
35+
/// </summary>
36+
public decimal Scale { get; set; } = 1;
37+
38+
/// <summary>
39+
/// 获得/设置 页面边距 默认 none
40+
/// </summary>
41+
public MarginOptions MarginOptions { get; set; } = new();
1742
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 UnitTest.Options;
7+
8+
public class PdfOptionsTest
9+
{
10+
[Fact]
11+
public void Test_PdfOptions_Ok()
12+
{
13+
var options = new PdfOptions()
14+
{
15+
Scale = 1.0m,
16+
DisplayHeaderFooter = true,
17+
PrintBackground = true,
18+
Landscape = true,
19+
Format = PaperFormat.A1,
20+
MarginOptions = new MarginOptions()
21+
{
22+
Top = "10mm",
23+
Bottom = "10mm",
24+
Left = "10mm",
25+
Right = "10mm"
26+
}
27+
};
28+
Assert.Equal(1.0m, options.Scale);
29+
Assert.True(options.DisplayHeaderFooter);
30+
Assert.True(options.PrintBackground);
31+
Assert.True(options.Landscape);
32+
Assert.Equal(PaperFormat.A1, options.Format);
33+
Assert.Equal("10mm", options.MarginOptions.Top);
34+
Assert.Equal("10mm", options.MarginOptions.Bottom);
35+
Assert.Equal("10mm", options.MarginOptions.Left);
36+
Assert.Equal("10mm", options.MarginOptions.Right);
37+
}
38+
}

0 commit comments

Comments
 (0)