Skip to content

feat(Html2Pdf): add support to ARM browsers#492

Merged
ArgoZhang merged 2 commits intomasterfrom
feat-pdf
Jul 18, 2025
Merged

feat(Html2Pdf): add support to ARM browsers#492
ArgoZhang merged 2 commits intomasterfrom
feat-pdf

Conversation

@ArgoZhang
Copy link
Copy Markdown
Member

@ArgoZhang ArgoZhang commented Jul 18, 2025

Link issues

fixes #491

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Enable ARM browser support and improve observability for HTML-to-PDF service by injecting logging, proxy configuration, and robust error handling.

Enhancements:

  • Inject ILogger into DefaultPdfService and expose a WebProxy property to configure network proxy for PuppeteerSharp’s BrowserFetcher.
  • Wrap all PDF generation methods in try-catch blocks with error logging for clearer diagnostics.
  • Refactor LaunchBrowserAsync to use the instance-level WebProxy, log download and launch progress, and unify logging through a private helper method.

@bb-auto bb-auto Bot added the enhancement New feature or request label Jul 18, 2025
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Jul 18, 2025

Reviewer's Guide

The PR extends DefaultPdfService to support ARM browsers by introducing network proxy configuration, structured logging, and enhanced error handling around browser download, launch, and PDF creation.

Sequence diagram for PDF generation with error handling and logging

sequenceDiagram
    participant Client
    participant DefaultPdfService
    participant BrowserFetcher
    participant Puppeteer
    participant ILogger
    Client->>DefaultPdfService: PdfDataAsync(url)
    DefaultPdfService->>BrowserFetcher: DownloadAsync() [with WebProxy]
    BrowserFetcher-->>DefaultPdfService: Browser downloaded
    DefaultPdfService->>ILogger: Log (Ready to start downloading browser)
    DefaultPdfService->>Puppeteer: LaunchAsync(options)
    Puppeteer-->>DefaultPdfService: IBrowser
    DefaultPdfService->>ILogger: Log (Start your browser)
    DefaultPdfService->>IBrowser: NewPageAsync()
    IBrowser-->>DefaultPdfService: IPage
    DefaultPdfService->>IPage: GoToAsync(url)
    DefaultPdfService->>IPage: PdfDataAsync()
    IPage-->>DefaultPdfService: PDF bytes
    DefaultPdfService-->>Client: PDF bytes
    alt Exception occurs
        DefaultPdfService->>ILogger: Log (Error generating PDF)
        DefaultPdfService-->>Client: Exception thrown
    end
Loading

Class diagram for updated DefaultPdfService with ARM and proxy support

classDiagram
    class DefaultPdfService {
        +IWebProxy? WebProxy
        +Task<byte[]> PdfDataAsync(string url)
        +Task<Stream> PdfStreamAsync(string url)
        +Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null)
        +Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null)
        -Task<IBrowser> LaunchBrowserAsync()
        -void Log(Exception? exception, string? message, params object?[] args)
    }
    DefaultPdfService --|> IHtml2Pdf
    class IHtml2Pdf {
        <<interface>>
    }
    class ILogger~DefaultPdfService~ {
        <<interface>>
    }
    DefaultPdfService o-- ILogger~DefaultPdfService~
    class IWebProxy {
        <<interface>>
    }
    DefaultPdfService o-- IWebProxy
Loading

Class diagram for BrowserFetcher proxy integration

classDiagram
    class BrowserFetcher {
        +IWebProxy? WebProxy
        +Task DownloadAsync()
    }
    BrowserFetcher o-- IWebProxy
    DefaultPdfService --> BrowserFetcher
Loading

File-Level Changes

Change Details Files
Inject ILogger and centralize logging with exception handling
  • Add ILogger to constructor signature
  • Implement private Log method for info-level logging
  • Wrap all PDF generation methods in try/catch and log exceptions before rethrowing
src/components/BootstrapBlazor.Html2Pdf/Services/DefaultPdfService.cs
Introduce IWebProxy support for network requests
  • Add public IWebProxy? WebProxy property
  • Assign WebProxy to BrowserFetcher.WebProxy before download
src/components/BootstrapBlazor.Html2Pdf/Services/DefaultPdfService.cs
Enhance browser launch sequence with logging and options
  • Convert LaunchBrowserAsync to instance method
  • Log download start, success (with BuildId), and launch args
  • Use CreateOptions with sandbox and web-security flags
src/components/BootstrapBlazor.Html2Pdf/Services/DefaultPdfService.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#491 Add support for ARM browsers in the Html2Pdf service.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@bb-auto bb-auto Bot added this to the v9.2.0 milestone Jul 18, 2025
@ArgoZhang ArgoZhang merged commit 5bc9e55 into master Jul 18, 2025
1 check passed
@ArgoZhang ArgoZhang deleted the feat-pdf branch July 18, 2025 01:45
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey @ArgoZhang - I've reviewed your changes - here's some feedback:

  • The inline constructor parameter on the class declaration isn’t valid C#—you need a private readonly ILogger field and a proper constructor to accept and assign it.
  • BrowserFetcher.DownloadAsync returns a revision string rather than a browser instance, so assigning its result to a browser variable won’t compile—you should call DownloadAsync for the revision and then use Puppeteer.LaunchAsync.
  • You’re logging exceptions at Information level; use LogError or LogWarning for exception paths so failures are more visible in the logs.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The inline constructor parameter on the class declaration isn’t valid C#—you need a private readonly ILogger field and a proper constructor to accept and assign it.
- BrowserFetcher.DownloadAsync returns a revision string rather than a browser instance, so assigning its result to a browser variable won’t compile—you should call DownloadAsync for the revision and then use Puppeteer.LaunchAsync.
- You’re logging exceptions at Information level; use LogError or LogWarning for exception paths so failures are more visible in the logs.

## Individual Comments

### Comment 1
<location> `src/components/BootstrapBlazor.Html2Pdf/Services/DefaultPdfService.cs:27` </location>
<code_context>
-        await using var browser = await LaunchBrowserAsync();
-        await using var page = await browser.NewPageAsync();
-        await page.GoToAsync(url);
+        try
+        {
+            await using var browser = await LaunchBrowserAsync();
</code_context>

<issue_to_address>
Consider extracting a single helper method to handle try/catch/log logic for all PDF generation methods.

You can remove almost all of the duplicated try/catch/log boiler-plate by extracting a single “execute and log” helper and then passing in only the unique bits (navigate/set-up + PdfData vs PdfStream). For example:

```csharp
// add this helper to DefaultPdfService
private async Task<T> ExecuteAsync<T>(Func<IPage, Task<T>> work, string message, params object?[] args)
{
  try
  {
    await using var browser = await LaunchBrowserAsync();
    await using var page    = await browser.NewPageAsync();
    return await work(page);
  }
  catch (Exception ex)
  {
    _logger.LogError(ex, message, args);
    throw;
  }
}
```

Then each public method collapses down to a one-liner:

```csharp
public Task<byte[]> PdfDataAsync(string url) =>
  ExecuteAsync(
    async page => { await page.GoToAsync(url); return await page.PdfDataAsync(); },
    "Error generating PDF from URL: {Url}", url);

public Task<Stream> PdfStreamAsync(string url) =>
  ExecuteAsync(
    async page => { await page.GoToAsync(url); return await page.PdfStreamAsync(); },
    "Error generating PDF from URL: {Url}", url);

public Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null) =>
  ExecuteAsync(
    async page =>
    {
      await page.SetContentAsync(html);
      await AddStyleTagAsync(page, links);
      await AddScriptTagAsync(page, scripts);
      return await page.PdfDataAsync();
    },
    "Error generating PDF from HTML content");

public Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null) =>
  ExecuteAsync(
    async page =>
    {
      await page.SetContentAsync(html);
      await AddStyleTagAsync(page, links);
      await AddScriptTagAsync(page, scripts);
      return await page.PdfStreamAsync();
    },
    "Error generating PDF from HTML content");
```

This preserves all behavior, removes four-fold duplication, and keeps your logger usage consistent.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

await using var browser = await LaunchBrowserAsync();
await using var page = await browser.NewPageAsync();
await page.GoToAsync(url);
try
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 extracting a single helper method to handle try/catch/log logic for all PDF generation methods.

You can remove almost all of the duplicated try/catch/log boiler-plate by extracting a single “execute and log” helper and then passing in only the unique bits (navigate/set-up + PdfData vs PdfStream). For example:

// add this helper to DefaultPdfService
private async Task<T> ExecuteAsync<T>(Func<IPage, Task<T>> work, string message, params object?[] args)
{
  try
  {
    await using var browser = await LaunchBrowserAsync();
    await using var page    = await browser.NewPageAsync();
    return await work(page);
  }
  catch (Exception ex)
  {
    _logger.LogError(ex, message, args);
    throw;
  }
}

Then each public method collapses down to a one-liner:

public Task<byte[]> PdfDataAsync(string url) =>
  ExecuteAsync(
    async page => { await page.GoToAsync(url); return await page.PdfDataAsync(); },
    "Error generating PDF from URL: {Url}", url);

public Task<Stream> PdfStreamAsync(string url) =>
  ExecuteAsync(
    async page => { await page.GoToAsync(url); return await page.PdfStreamAsync(); },
    "Error generating PDF from URL: {Url}", url);

public Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null) =>
  ExecuteAsync(
    async page =>
    {
      await page.SetContentAsync(html);
      await AddStyleTagAsync(page, links);
      await AddScriptTagAsync(page, scripts);
      return await page.PdfDataAsync();
    },
    "Error generating PDF from HTML content");

public Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null) =>
  ExecuteAsync(
    async page =>
    {
      await page.SetContentAsync(html);
      await AddStyleTagAsync(page, links);
      await AddScriptTagAsync(page, scripts);
      return await page.PdfStreamAsync();
    },
    "Error generating PDF from HTML content");

This preserves all behavior, removes four-fold duplication, and keeps your logger usage consistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(Html2Pdf): add support to ARM browsers

1 participant