-
-
Notifications
You must be signed in to change notification settings - Fork 365
feat(IHtml2Image): add IHtml2Image service #5421
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
Merged
+249
−6
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a04df3e
feat: 增加 Html2Image 服务
ArgoZhang 01bc65a
chore: 增加脚本
ArgoZhang b6285a7
chore: 增加 IHtml2Image 服务注册
ArgoZhang 55925be
doc: 增加 Html2Image 菜单
ArgoZhang 8b06d30
doc: 增加源码映射
ArgoZhang 39bd834
doc: 增加 Html2Image 示例
ArgoZhang 347de00
doc: 更新示例
ArgoZhang be4b523
doc: 更新资源文件
ArgoZhang cceeaa2
doc: 更新标识
ArgoZhang fdfb510
doc: 增加一个友链
ArgoZhang a638f5c
doc: 增加排序
ArgoZhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
src/BootstrapBlazor.Server/Components/Samples/Html2Images.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| @page "/html2image" | ||
|
|
||
| <h3>@Localizer["Html2ImageTitle"]</h3> | ||
|
|
||
| <h4>@Localizer["Html2ImageIntro"]</h4> | ||
|
|
||
| <DemoBlock Title="@Localizer["Html2ImageElementTitle"]" Introduction="@Localizer["Html2ImageElementIntro"]" Name="Normal"> | ||
| <Button OnClickWithoutRender="OnExportAsync" Text="@Localizer["Html2ImageButtonText"]" Icon="fa-solid fa-image"></Button> | ||
| <Table TItem="Foo" Items="@Items.Take(3)" Id="table-9527"> | ||
| <TableColumns> | ||
| <TableColumn @bind-Field="@context.DateTime" Width="180" /> | ||
| <TableColumn @bind-Field="@context.Name" Sortable="true" Filterable="true" /> | ||
| <TableColumn @bind-Field="@context.Address" /> | ||
| </TableColumns> | ||
| </Table> | ||
| @if (!string.IsNullOrEmpty(_imageData)) | ||
| { | ||
| <section ignore> | ||
| <img src="@_imageData" class="w-100" /> | ||
| </section> | ||
| } | ||
| </DemoBlock> |
71 changes: 71 additions & 0 deletions
71
src/BootstrapBlazor.Server/Components/Samples/Html2Images.razor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // 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.Server.Components.Samples; | ||
|
|
||
| /// <summary> | ||
| /// Html2Image 组件 | ||
| /// </summary> | ||
| public partial class Html2Images | ||
| { | ||
| /// <summary> | ||
| /// 获得 IconTheme 实例 | ||
| /// </summary> | ||
| [Inject] | ||
| [NotNull] | ||
| private IIconTheme? IconTheme { get; set; } | ||
|
|
||
| [Inject] | ||
| [NotNull] | ||
| private IStringLocalizer<Foo>? LocalizerFoo { get; set; } | ||
|
|
||
| [Inject] | ||
| [NotNull] | ||
| private IHtml2Image? Html2ImageService { get; set; } | ||
|
|
||
| [Inject] | ||
| [NotNull] | ||
| private IStringLocalizer<Html2Images>? Localizer { get; set; } | ||
|
|
||
| [Inject] | ||
| [NotNull] | ||
| private NavigationManager? NavigationManager { get; set; } | ||
|
|
||
| [NotNull] | ||
| private List<Foo>? Items { get; set; } | ||
|
|
||
| private string? _imageData; | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| protected override void OnInitialized() | ||
| { | ||
| base.OnInitialized(); | ||
|
|
||
| Items = Foo.GenerateFoo(LocalizerFoo); | ||
| } | ||
|
|
||
| private async Task OnExportAsync() | ||
| { | ||
| _imageData = await Html2ImageService.GetDataAsync("#table-9527", new Html2ImageOptions() | ||
| { | ||
| //IncludeStyleProperties = [ | ||
| // $"{NavigationManager.BaseUri}_content/BootstrapBlazor.FontAwesome/css/font-awesome.min.css", | ||
| // $"{NavigationManager.BaseUri}_content/BootstrapBlazor/css/bootstrap.blazor.bundle.min.css", | ||
| // $"{NavigationManager.BaseUri}BootstrapBlazor.Server.styles.css", | ||
| // $"{NavigationManager.BaseUri}css/site.css" | ||
| //] | ||
| }); | ||
| StateHasChanged(); | ||
|
|
||
| //if (stream != null) | ||
| //{ | ||
| // var reader = new StreamReader(stream); | ||
| // var data = await reader.ReadToEndAsync(); | ||
| // reader.Close(); | ||
| //} | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // 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 | ||
|
|
||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
||
| /// <summary> | ||
| /// Html2Image 选项类 | ||
| /// </summary> | ||
| public class Html2ImageOptions | ||
| { | ||
| /// <summary> | ||
| /// 获得/设置 样式集合 默认 null | ||
| /// </summary> | ||
| [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] | ||
| public List<string>? IncludeStyleProperties { get; set; } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // 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 | ||
|
|
||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
||
| /// <summary> | ||
| /// 默认 Html to Image 实现 | ||
| /// <param name="runtime"></param> | ||
| /// <param name="logger"></param> | ||
| /// </summary> | ||
| class DefaultHtml2ImageService(IJSRuntime runtime, ILogger<DefaultHtml2ImageService> logger) : IHtml2Image | ||
| { | ||
| private JSModule? _jsModule; | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| public Task<string?> GetDataAsync(string selector, Html2ImageOptions options) => Execute(selector, "toPng", options); | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| public Task<Stream?> GetStreamAsync(string selector, Html2ImageOptions options) => ToBlob(selector, options); | ||
|
|
||
| private async Task<string?> Execute(string selector, string methodName, Html2ImageOptions options) | ||
| { | ||
| string? data = null; | ||
| try | ||
| { | ||
| _jsModule ??= await runtime.LoadModuleByName("html2image"); | ||
| if (_jsModule != null) | ||
| { | ||
| data = await _jsModule.InvokeAsync<string?>("execute", selector, methodName, options); | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| logger.LogError(ex, "{Execute} throw exception", nameof(Execute)); | ||
| } | ||
| return data; | ||
| } | ||
|
|
||
| private async Task<Stream?> ToBlob(string selector, Html2ImageOptions options) | ||
| { | ||
| Stream? data = null; | ||
| try | ||
| { | ||
| _jsModule ??= await runtime.LoadModuleByName("html2image"); | ||
| if (_jsModule != null) | ||
| { | ||
| var streamRef = await _jsModule.InvokeAsync<IJSStreamReference>("execute", selector, "toBlob", options); | ||
| if (streamRef != null) | ||
| { | ||
| data = await streamRef.OpenReadStreamAsync(streamRef.Length); | ||
| } | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| logger.LogError(ex, "{ToBlob} throw exception", nameof(ToBlob)); | ||
| } | ||
| return data; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // 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> | ||
| /// IHtml2Image 接口 | ||
| /// </summary> | ||
| public interface IHtml2Image | ||
| { | ||
| /// <summary> | ||
| /// Export method | ||
| /// </summary> | ||
| /// <param name="selector">选择器</param> | ||
| /// <param name="options"></param> | ||
| Task<string?> GetDataAsync(string selector, Html2ImageOptions options); | ||
|
|
||
| /// <summary> | ||
| /// Export method | ||
| /// </summary> | ||
| /// <param name="selector">选择器</param> | ||
| /// <param name="options"></param> | ||
| Task<Stream?> GetStreamAsync(string selector, Html2ImageOptions options); | ||
| } |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import '../lib/html2image/html-to-image.js' | ||
|
|
||
| export async function execute(selector, methodName, options) { | ||
| let data = null; | ||
| const el = document.querySelector(selector); | ||
| if (el) { | ||
| const fn = htmlToImage[methodName]; | ||
| data = await fn(el, {}); | ||
ArgoZhang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| return data; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.