Skip to content

Commit 53a9929

Browse files
authored
feat(Html2Pdf): add PaperFormat parameter (#634)
* feat: 增加 PaperFormat 等配置项 * chore: bump version 9.0.6-beta02
1 parent af1fb78 commit 53a9929

2 files changed

Lines changed: 79 additions & 5 deletions

File tree

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

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

3-
<PropertyGroup>
4-
<Version>9.0.6-beta01</Version>
3+
<PropertyGroup Condition="'$(VisualStudioVersion)' == '17.0'">
4+
<Version>9.0.6-beta02</Version>
5+
</PropertyGroup>
6+
7+
<PropertyGroup Condition="'$(VisualStudioVersion)' == '18.0'">
8+
<Version>10.0.0-rc.2.1.2</Version>
59
</PropertyGroup>
610

711
<PropertyGroup>
812
<PackageTags>Bootstrap Blazor WebAssembly wasm UI Components Pdf</PackageTags>
913
<Description>Bootstrap UI components extensions of Html2Pdf use PuppeteerSharp lib</Description>
1014
</PropertyGroup>
1115

12-
<ItemGroup>
13-
<PackageReference Include="BootstrapBlazor" Version="9.11.5-beta07" />
16+
<ItemGroup Condition="'$(VisualStudioVersion)' == '17.0'">
17+
<PackageReference Include="BootstrapBlazor" Version="9.11.5-beta11" />
18+
<PackageReference Include="PuppeteerSharp" Version="20.2.4" />
19+
</ItemGroup>
20+
21+
<ItemGroup Condition="'$(VisualStudioVersion)' == '18.0'">
22+
<PackageReference Include="BootstrapBlazor" Version="10.0.0-rc.2.1.10" />
1423
<PackageReference Include="PuppeteerSharp" Version="20.2.4" />
1524
</ItemGroup>
1625

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

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,75 @@ private static PuppeteerSharp.PdfOptions GetOptions(PdfOptions? options)
9797
{
9898
return options == null ? new PuppeteerSharp.PdfOptions() : new PuppeteerSharp.PdfOptions
9999
{
100-
Landscape = options.Landscape
100+
Landscape = options.Landscape,
101+
PrintBackground = options.PrintBackground,
102+
Format = GetFormat(options.Format),
103+
MarginOptions = GetMarginOptions(options.MarginOptions),
104+
DisplayHeaderFooter = options.DisplayHeaderFooter,
105+
Scale = options.Scale
101106
};
102107
}
103108

109+
private static PuppeteerSharp.Media.MarginOptions? GetMarginOptions(MarginOptions options) => new PuppeteerSharp.Media.MarginOptions
110+
{
111+
Top = options.Top,
112+
Bottom = options.Bottom,
113+
Left = options.Left,
114+
Right = options.Right
115+
};
116+
117+
private static PuppeteerSharp.Media.PaperFormat GetFormat(PaperFormat format)
118+
{
119+
if (format == PaperFormat.A0)
120+
{
121+
return PuppeteerSharp.Media.PaperFormat.A0;
122+
}
123+
else if (format == PaperFormat.A1)
124+
{
125+
return PuppeteerSharp.Media.PaperFormat.A1;
126+
}
127+
else if (format == PaperFormat.A2)
128+
{
129+
return PuppeteerSharp.Media.PaperFormat.A2;
130+
}
131+
else if (format == PaperFormat.A3)
132+
{
133+
return PuppeteerSharp.Media.PaperFormat.A3;
134+
}
135+
else if (format == PaperFormat.A4)
136+
{
137+
return PuppeteerSharp.Media.PaperFormat.A4;
138+
}
139+
else if (format == PaperFormat.A5)
140+
{
141+
return PuppeteerSharp.Media.PaperFormat.A5;
142+
}
143+
else if (format == PaperFormat.A6)
144+
{
145+
return PuppeteerSharp.Media.PaperFormat.A6;
146+
}
147+
else if (format == PaperFormat.Letter)
148+
{
149+
return PuppeteerSharp.Media.PaperFormat.Letter;
150+
}
151+
else if (format == PaperFormat.Legal)
152+
{
153+
return PuppeteerSharp.Media.PaperFormat.Legal;
154+
}
155+
else if (format == PaperFormat.Tabloid)
156+
{
157+
return PuppeteerSharp.Media.PaperFormat.Tabloid;
158+
}
159+
else if (format == PaperFormat.Ledger)
160+
{
161+
return PuppeteerSharp.Media.PaperFormat.Ledger;
162+
}
163+
else
164+
{
165+
return new PuppeteerSharp.Media.PaperFormat(format.Width, format.Height);
166+
}
167+
}
168+
104169
private static async Task AddStyleTagAsync(IPage page, IEnumerable<string>? links = null)
105170
{
106171
var styles = new List<string>();

0 commit comments

Comments
 (0)