-
-
Notifications
You must be signed in to change notification settings - Fork 7
feat(Playwright): add BootstrapBlazor.Html2Pdf.Playwright project #435
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
Merged
Changes from all commits
Commits
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
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
17 changes: 17 additions & 0 deletions
17
...components/BootstrapBlazor.Html2Pdf.Playwright/BootstrapBlazor.Html2Pdf.Playwright.csproj
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,17 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Razor"> | ||
|
|
||
| <PropertyGroup> | ||
| <Version>9.0.0</Version> | ||
| </PropertyGroup> | ||
|
|
||
| <PropertyGroup> | ||
| <PackageTags>Bootstrap Blazor WebAssembly wasm UI Components Pdf</PackageTags> | ||
| <Description>Bootstrap UI components extensions of Html2Pdf use Playwright lib</Description> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="BootstrapBlazor" Version="$(BBVersion)" /> | ||
| <PackageReference Include="Microsoft.Playwright" Version="1.52.0" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
26 changes: 26 additions & 0 deletions
26
src/components/BootstrapBlazor.Html2Pdf.Playwright/Extensions/ServiceCollectionExtensions.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,26 @@ | ||
| // Copyright (c) Argo Zhang (argo@163.com). All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
| // Website: https://www.blazor.zone or https://argozhang.github.io/ | ||
|
|
||
| using BootstrapBlazor.Components; | ||
|
|
||
| namespace Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| /// <summary> | ||
| /// BootstrapBlazor 服务扩展类 | ||
| /// </summary> | ||
| public static class BootstrapBlazorHtml2PdfServiceExtensions | ||
| { | ||
| /// <summary> | ||
| /// 添加 AzureOpenAIService 服务 | ||
| /// </summary> | ||
| /// <param name="services"></param> | ||
| public static IServiceCollection AddBootstrapBlazorHtml2PdfUsePlaywrightService(this IServiceCollection services) | ||
| { | ||
| services.AddSingleton<IHtml2Pdf, DefaultPdfService>(); | ||
| #if NET8_0_OR_GREATER | ||
| services.AddKeyedSingleton<IHtml2Pdf, DefaultPdfService>("BootstrapBlazor.Html2Pdf.Playwright"); | ||
| #endif | ||
| return services; | ||
| } | ||
| } |
131 changes: 131 additions & 0 deletions
131
src/components/BootstrapBlazor.Html2Pdf.Playwright/Services/DefaultPdfService.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,131 @@ | ||
| // Copyright (c) Argo Zhang (argo@163.com). All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
| // Website: https://www.blazor.zone or https://argozhang.github.io/ | ||
|
|
||
| using Microsoft.Playwright; | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
||
| /// <summary> | ||
| /// 默认 Html to Pdf 实现 | ||
| /// </summary> | ||
| class DefaultPdfService : IHtml2Pdf | ||
| { | ||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| public Task<byte[]> PdfDataAsync(string url) | ||
| { | ||
| return GeneratePdfFromUrlAsync(url); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| public async Task<Stream> PdfStreamAsync(string url) | ||
| { | ||
| var data = await GeneratePdfFromUrlAsync(url); | ||
| return new MemoryStream(data); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Export method | ||
| /// </summary> | ||
| /// <param name="html">html raw string</param> | ||
| /// <param name="links"></param> | ||
| /// <param name="scripts"></param> | ||
| public Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null) | ||
| { | ||
| return GeneratePdfFromHtmlAsync(html, links, scripts); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Export method | ||
| /// </summary> | ||
| /// <param name="html">html raw string</param> | ||
| /// <param name="links"></param> | ||
| /// <param name="scripts"></param> | ||
| public async Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null) | ||
| { | ||
| var data = await PdfDataFromHtmlAsync(html, links, scripts); | ||
| return new MemoryStream(data); | ||
| } | ||
|
|
||
| private static async Task<byte[]> GeneratePdfFromUrlAsync(string url) | ||
| { | ||
| using var playwright = await Playwright.CreateAsync(); | ||
| await using var browser = await playwright.Chromium.LaunchAsync(CreateOptions()); | ||
|
|
||
| await using var context = await browser.NewContextAsync(); | ||
| var page = await context.NewPageAsync(); | ||
|
|
||
| await page.GotoAsync(url, new PageGotoOptions { WaitUntil = WaitUntilState.NetworkIdle }); | ||
| return await page.PdfAsync(new PagePdfOptions | ||
| { | ||
| Format = "A4", | ||
| Landscape = false, | ||
| }); | ||
| } | ||
|
|
||
| private static async Task<byte[]> GeneratePdfFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null) | ||
| { | ||
| using var playwright = await Playwright.CreateAsync(); | ||
| await using var browser = await playwright.Chromium.LaunchAsync(CreateOptions()); | ||
|
|
||
| await using var context = await browser.NewContextAsync(); | ||
| var page = await context.NewPageAsync(); | ||
|
|
||
| await page.SetContentAsync(html); | ||
| await AddStyleTagAsync(page, links); | ||
| await AddScriptTagAsync(page, scripts); | ||
|
|
||
| return await page.PdfAsync(new PagePdfOptions | ||
| { | ||
| Format = "A4", | ||
| Landscape = false, | ||
| }); | ||
| } | ||
|
|
||
| private static BrowserTypeLaunchOptions CreateOptions() => new() | ||
| { | ||
| Headless = true, | ||
| Args = ["--no-sandbox", "--disable-setuid-sandbox", "--disable-web-security"] | ||
| }; | ||
|
|
||
| private static async Task AddStyleTagAsync(IPage page, IEnumerable<string>? links = null) | ||
| { | ||
| var styles = new List<string>(); | ||
|
|
||
| if (links != null) | ||
| { | ||
| styles.AddRange(links); | ||
| } | ||
|
|
||
| foreach (var link in styles) | ||
| { | ||
| await page.AddStyleTagAsync(new PageAddStyleTagOptions() | ||
| { | ||
| Url = link, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| private static async Task AddScriptTagAsync(IPage page, IEnumerable<string>? scripts = null) | ||
| { | ||
| var tags = new List<string>(); | ||
|
|
||
| if (scripts != null) | ||
| { | ||
| tags.AddRange(scripts); | ||
| } | ||
|
|
||
| foreach (var script in tags) | ||
| { | ||
| await page.AddScriptTagAsync(new PageAddScriptTagOptions() | ||
| { | ||
| Url = script, | ||
| Type = "script" | ||
| }); | ||
| } | ||
| } | ||
| } | ||
1 change: 1 addition & 0 deletions
1
src/components/BootstrapBlazor.Html2Pdf.Playwright/_Imports.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 @@ | ||
| @using Microsoft.AspNetCore.Components.Web |
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.
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.
issue (complexity): Consider iterating directly over the input enumerable in
AddStyleTagAsyncandAddScriptTagAsyncto avoid creating intermediate lists.Consider simplifying the tag helpers to avoid creating intermediate lists. For example, you can iterate directly over the supplied enumerable. This reduces unnecessary nesting and improves readability without changing functionality. Here's a suggestion:
These concise changes remove unnecessary list instantiation while keeping functionality intact.