Skip to content

Commit 39bd834

Browse files
committed
doc: 增加 Html2Image 示例
1 parent 8b06d30 commit 39bd834

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@page "/html2image"
2+
3+
<h3>@Localizer["Html2ImageTitle"]</h3>
4+
5+
<h4>@Localizer["Html2ImageDescription"]</h4>
6+
7+
<Tips>
8+
<div>@((MarkupString)Localizer["Html2ImageNote"].Value)</div>
9+
</Tips>
10+
11+
<DemoBlock Title="@Localizer["Html2ImageElementTitle"]" Introduction="@Localizer["Html2ImageElementIntro"]" Name="Normal">
12+
<section ignore>
13+
<p>@((MarkupString)Localizer["Html2ImageElementDesc"].Value)</p>
14+
<Button OnClickWithoutRender="OnExportAsync" Text="@Localizer["ExportButtonText"]" Icon="@_exportIcon"></Button>
15+
</section>
16+
<Table TItem="Foo" Items="@Items.Take(3)" Id="table-9527">
17+
<TableColumns>
18+
<TableColumn @bind-Field="@context.DateTime" Width="180" />
19+
<TableColumn @bind-Field="@context.Name" Sortable="true" Filterable="true" />
20+
<TableColumn @bind-Field="@context.Address" />
21+
</TableColumns>
22+
</Table>
23+
</DemoBlock>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.Server.Components.Samples;
7+
8+
/// <summary>
9+
/// Html2Image 组件
10+
/// </summary>
11+
public partial class Html2Images
12+
{
13+
/// <summary>
14+
/// 获得 IconTheme 实例
15+
/// </summary>
16+
[Inject]
17+
[NotNull]
18+
private IIconTheme? IconTheme { get; set; }
19+
20+
[Inject]
21+
[NotNull]
22+
private IStringLocalizer<Foo>? LocalizerFoo { get; set; }
23+
24+
[Inject]
25+
[NotNull]
26+
private IHtml2Image? Html2ImageService { get; set; }
27+
28+
[Inject]
29+
[NotNull]
30+
private IStringLocalizer<Html2Pdfs>? Localizer { get; set; }
31+
32+
[Inject]
33+
[NotNull]
34+
private NavigationManager? NavigationManager { get; set; }
35+
36+
[NotNull]
37+
private List<Foo>? Items { get; set; }
38+
39+
private string? _exportIcon = "";
40+
41+
/// <summary>
42+
/// <inheritdoc/>
43+
/// </summary>
44+
protected override void OnInitialized()
45+
{
46+
base.OnInitialized();
47+
48+
Items = Foo.GenerateFoo(LocalizerFoo);
49+
50+
_exportIcon = IconTheme.GetIconByKey(ComponentIcons.TableExportPdfIcon);
51+
}
52+
private async Task OnExportAsync()
53+
{
54+
var stream = await Html2ImageService.GetStreamAsync("#table-9527", new Html2ImageOptions()
55+
{
56+
//IncludeStyleProperties = [
57+
// $"{NavigationManager.BaseUri}_content/BootstrapBlazor.FontAwesome/css/font-awesome.min.css",
58+
// $"{NavigationManager.BaseUri}_content/BootstrapBlazor/css/bootstrap.blazor.bundle.min.css",
59+
// $"{NavigationManager.BaseUri}BootstrapBlazor.Server.styles.css",
60+
// $"{NavigationManager.BaseUri}css/site.css"
61+
//]
62+
});
63+
if (stream != null)
64+
{
65+
var reader = new StreamReader(stream);
66+
var data = await reader.ReadToEndAsync();
67+
reader.Close();
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)