Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>9.0.4</Version>
<Version>9.0.6-beta01</Version>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BootstrapBlazor" Version="$(BBVersion)" />
<PackageReference Include="BootstrapBlazor" Version="9.11.5-beta07" />
<PackageReference Include="PuppeteerSharp" Version="20.2.4" />
</ItemGroup>

Expand Down
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/

Expand Down
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/

Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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
{
Expand All @@ -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)
{
Expand All @@ -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
{
Expand All @@ -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)
{
Expand All @@ -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
Comment on lines 95 to +100
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The GetOptions method only maps the Landscape property from PdfOptions to PuppeteerSharp.PdfOptions. If PdfOptions is 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.

Suggested change
private static PuppeteerSharp.PdfOptions GetOptions(PdfOptions? options)
{
return options == null ? new PuppeteerSharp.PdfOptions() : new PuppeteerSharp.PdfOptions
{
Landscape = options.Landscape
/// <summary>
/// Maps <see cref="PdfOptions"/> to <see cref="PuppeteerSharp.PdfOptions"/>.
///
/// NOTE: If new properties are added to <see cref="PdfOptions"/>, they must be mapped here as well.
/// </summary>
private static PuppeteerSharp.PdfOptions GetOptions(PdfOptions? options)
{
return options == null ? new PuppeteerSharp.PdfOptions() : new PuppeteerSharp.PdfOptions
{
Landscape = options.Landscape
// TODO: Map additional properties from PdfOptions here as needed.

Copilot uses AI. Check for mistakes.
};
}

private static async Task AddStyleTagAsync(IPage page, IEnumerable<string>? links = null)
{
var styles = new List<string>();
Expand Down Expand Up @@ -161,13 +151,16 @@ private async Task<IBrowser> LaunchBrowserAsync()

private void Log(Exception? exception, string? message, params object?[] args)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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

  • remove the redundant IsEnabled check and flatten the nesting,
  • shorten GetOptions to a single expression while preserving the mapping.

{
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);
}
}
}
}