-
-
Notifications
You must be signed in to change notification settings - Fork 7
feat(Html2Pdf): add PdfOptions parameter #632
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion
2
src/components/BootstrapBlazor.Html2Pdf/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
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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| // Copyright (c) Argo Zhang (argo@163.com). All rights reserved. | ||
| // Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). 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/ | ||
|
|
||
|
|
@@ -19,18 +19,15 @@ class DefaultPdfService(ILogger<DefaultPdfService> logger) : IHtml2Pdf | |
| /// </summary> | ||
| public IWebProxy? WebProxy { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| public async Task<byte[]> PdfDataAsync(string url) | ||
| public async Task<byte[]> PdfDataAsync(string url, PdfOptions? options = null) | ||
| { | ||
| try | ||
| { | ||
| await using var browser = await LaunchBrowserAsync(); | ||
| await using var page = await browser.NewPageAsync(); | ||
|
|
||
| await page.GoToAsync(url); | ||
| return await page.PdfDataAsync(); | ||
| return await page.PdfDataAsync(GetOptions(options)); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
|
|
@@ -39,18 +36,15 @@ public async Task<byte[]> PdfDataAsync(string url) | |
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| public async Task<Stream> PdfStreamAsync(string url) | ||
| public async Task<Stream> PdfStreamAsync(string url, PdfOptions? options = null) | ||
| { | ||
| try | ||
| { | ||
| await using var browser = await LaunchBrowserAsync(); | ||
| await using var page = await browser.NewPageAsync(); | ||
| await page.GoToAsync(url); | ||
|
|
||
| return await page.PdfStreamAsync(); | ||
| return await page.PdfStreamAsync(GetOptions(options)); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
|
|
@@ -59,13 +53,7 @@ public async Task<Stream> PdfStreamAsync(string url) | |
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Export method | ||
| /// </summary> | ||
| /// <param name="html">html raw string</param> | ||
| /// <param name="links"></param> | ||
| /// <param name="scripts"></param> | ||
| public async Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null) | ||
| public async Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null, PdfOptions? options = null) | ||
| { | ||
| try | ||
| { | ||
|
|
@@ -76,7 +64,7 @@ public async Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? | |
| await AddStyleTagAsync(page, links); | ||
| await AddScriptTagAsync(page, scripts); | ||
|
|
||
| return await page.PdfDataAsync(); | ||
| return await page.PdfDataAsync(GetOptions(options)); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
|
|
@@ -85,13 +73,7 @@ public async Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? | |
| } | ||
| } | ||
|
|
||
| /// <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) | ||
| public async Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null, PdfOptions? options = null) | ||
| { | ||
| try | ||
| { | ||
|
|
@@ -102,7 +84,7 @@ public async Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string | |
| await AddStyleTagAsync(page, links); | ||
| await AddScriptTagAsync(page, scripts); | ||
|
|
||
| return await page.PdfStreamAsync(); | ||
| return await page.PdfStreamAsync(GetOptions(options)); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
|
|
@@ -111,6 +93,14 @@ public async Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string | |
| } | ||
| } | ||
|
|
||
| private static PuppeteerSharp.PdfOptions GetOptions(PdfOptions? options) | ||
| { | ||
| return options == null ? new PuppeteerSharp.PdfOptions() : new PuppeteerSharp.PdfOptions | ||
| { | ||
| Landscape = options.Landscape | ||
| }; | ||
| } | ||
|
|
||
| private static async Task AddStyleTagAsync(IPage page, IEnumerable<string>? links = null) | ||
| { | ||
| var styles = new List<string>(); | ||
|
|
@@ -161,13 +151,16 @@ private async Task<IBrowser> LaunchBrowserAsync() | |
|
|
||
| private void Log(Exception? exception, string? message, params object?[] args) | ||
|
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 removing the redundant log level check and simplifying the GetOptions method to reduce code nesting and verbosity. // ↓ Replace the verbose Log method...
private void Log(Exception? exception, string? message, params object?[] args)
{
- if (logger.IsEnabled(LogLevel.Information))
- {
- if (args.Length != 0)
- {
- logger.LogInformation(exception, "{Message} | Args: {Args}", message, args);
- }
- else
- {
- logger.LogInformation(exception, "{Message}", message);
- }
- }
+ // no need for IsEnabled guard – LogInformation already checks the level
+ if (args.Length > 0)
+ {
+ logger.LogInformation(exception, "{Message} | Args: {Args}", message, args);
+ }
+ else
+ {
+ logger.LogInformation(exception, "{Message}", message);
+ }
}
// …and make GetOptions an expression‐bodied helper
private static PuppeteerSharp.PdfOptions GetOptions(PdfOptions? options)
=> new()
{
Landscape = options?.Landscape ?? default
};These changes
|
||
| { | ||
| if (args.Length != 0) | ||
| { | ||
| logger.LogInformation(exception, "{Message} | Args: {Args}", message, args); | ||
| } | ||
| else | ||
| if (logger.IsEnabled(LogLevel.Information)) | ||
| { | ||
| logger.LogInformation(exception, "{Message}", message); | ||
| if (args.Length != 0) | ||
| { | ||
| logger.LogInformation(exception, "{Message} | Args: {Args}", message, args); | ||
| } | ||
| else | ||
| { | ||
| logger.LogInformation(exception, "{Message}", message); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
[nitpick] The
GetOptionsmethod only maps theLandscapeproperty fromPdfOptionstoPuppeteerSharp.PdfOptions. IfPdfOptionsis intended to support additional properties in the future, this implementation will require updates. Consider documenting this mapping or using a more extensible pattern if additional options are planned.