-
-
Notifications
You must be signed in to change notification settings - Fork 362
feat(Html2Pdf): add PaperFormat parameter #7036
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
||
| /// <summary> | ||
| /// 页面边距选项 | ||
| /// </summary> | ||
| [ExcludeFromCodeCoverage] | ||
| public record MarginOptions | ||
| { | ||
| /// <summary> | ||
| /// Top margin, accepts values labeled with units. | ||
| /// </summary> | ||
| public object? Top { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Left margin, accepts values labeled with units. | ||
| /// </summary> | ||
| public object? Left { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Bottom margin, accepts values labeled with units. | ||
| /// </summary> | ||
| public object? Bottom { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Right margin, accepts values labeled with units. | ||
| /// </summary> | ||
| public object? Right { get; set; } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
||
| /// <summary> | ||
| /// 纸张规格配置类 | ||
| /// </summary> | ||
| [ExcludeFromCodeCoverage] | ||
| public record PaperFormat | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Consider making PaperFormat immutable for thread safety and consistency. Width and Height should be init-only or readonly to prevent changes after creation, ensuring immutability for this value object. |
||
| { | ||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| public static PaperFormat Letter => new(8.5m, 11m); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (complexity): Consider using a centralized mapping for paper formats to avoid repetitive instantiation in each static property. Consider centralizing the size-to-dimension mapping (once) instead of repeating public enum StandardPaperFormat
{
Letter, Legal, Tabloid, Ledger,
A0, A1, A2, A3, A4, A5, A6
}
public record PaperFormat(decimal Width, decimal Height)
{
private static readonly IReadOnlyDictionary<StandardPaperFormat, PaperFormat> _map
= new Dictionary<StandardPaperFormat, PaperFormat>
{
[StandardPaperFormat.Letter] = new(8.5m, 11m),
[StandardPaperFormat.Legal] = new(8.5m, 14m),
[StandardPaperFormat.Tabloid] = new(11m, 17m),
[StandardPaperFormat.Ledger] = new(17m, 11m),
[StandardPaperFormat.A0] = new(33.1102m, 46.811m),
[StandardPaperFormat.A1] = new(23.3858m, 33.1102m),
// …
};
public static PaperFormat Create(StandardPaperFormat format)
=> _map.TryGetValue(format, out var pf)
? pf
: throw new ArgumentOutOfRangeException(nameof(format), format, null);
}If you still want named properties for discoverability, point them at the dictionary: public static PaperFormat Letter => Create(StandardPaperFormat.Letter);
public static PaperFormat Legal => Create(StandardPaperFormat.Legal);
// … |
||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| public static PaperFormat Legal => new(8.5m, 14m); | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| public static PaperFormat Tabloid => new(11m, 17m); | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| public static PaperFormat Ledger => new(17m, 11m); | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| public static PaperFormat A0 => new(33.1102m, 46.811m); | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| public static PaperFormat A1 => new(23.3858m, 33.1102m); | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| public static PaperFormat A2 => new(16.5354m, 23.3858m); | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| public static PaperFormat A3 => new(11.6929m, 16.5354m); | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| public static PaperFormat A4 => new(8.2677m, 11.6929m); | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| public static PaperFormat A5 => new(5.8268m, 8.2677m); | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| public static PaperFormat A6 => new(4.1339m, 5.8268m); | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| public decimal Width { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| public decimal Height { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| /// <param name="width"></param> | ||
| /// <param name="height"></param> | ||
| public PaperFormat(decimal width, decimal height) | ||
| { | ||
| Width = width; | ||
| Height = height; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,4 +14,29 @@ public class PdfOptions | |||||||||||||||||||||||||||||||||||
| /// 获得/设置 是否横向打印 默认 false | ||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||
| public bool Landscape { get; set; } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||
| /// 获得/设置 是否打印背景色 默认 false | ||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||
| public bool PrintBackground { get; set; } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||
| /// 获得/设置 纸张格式 默认 A4 | ||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||
| public PaperFormat Format { get; set; } = PaperFormat.A4; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||
| /// 获得/设置 是否显示页眉页脚 默认 false | ||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||
| public bool DisplayHeaderFooter { get; set; } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||
| /// 获得/设置 放大比例 默认 1 取值 0.1 到 2 之间 | ||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||
| public decimal Scale { get; set; } = 1; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
| public decimal Scale { get; set; } = 1; | |
| private decimal _scale = 1; | |
| /// <summary> | |
| /// 获得/设置 放大比例 默认 1 取值 0.1 到 2 之间 | |
| /// </summary> | |
| public decimal Scale | |
| { | |
| get => _scale; | |
| set | |
| { | |
| if (value < 0.1m || value > 2m) | |
| { | |
| throw new ArgumentOutOfRangeException(nameof(Scale), "Scale must be between 0.1 and 2."); | |
| } | |
| _scale = value; | |
| } | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| namespace UnitTest.Options; | ||
|
|
||
| public class PdfOptionsTest | ||
| { | ||
| [Fact] | ||
| public void Test_PdfOptions_Ok() | ||
| { | ||
| var options = new PdfOptions() | ||
| { | ||
| Scale = 1.0m, | ||
| DisplayHeaderFooter = true, | ||
| PrintBackground = true, | ||
| Landscape = true, | ||
| Format = PaperFormat.A1, | ||
| MarginOptions = new MarginOptions() | ||
| { | ||
| Top = "10mm", | ||
| Bottom = "10mm", | ||
| Left = "10mm", | ||
| Right = "10mm" | ||
| } | ||
| }; | ||
| Assert.Equal(1.0m, options.Scale); | ||
| Assert.True(options.DisplayHeaderFooter); | ||
| Assert.True(options.PrintBackground); | ||
| Assert.True(options.Landscape); | ||
| Assert.Equal(PaperFormat.A1, options.Format); | ||
| Assert.Equal("10mm", options.MarginOptions.Top); | ||
| Assert.Equal("10mm", options.MarginOptions.Bottom); | ||
| Assert.Equal("10mm", options.MarginOptions.Left); | ||
| Assert.Equal("10mm", options.MarginOptions.Right); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Using object? for margin properties may reduce type safety.
Consider specifying a more precise type for margin properties to prevent runtime issues and make the code clearer.