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>10.0.11</Version>
<Version>10.0.12</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
49 changes: 37 additions & 12 deletions src/components/BootstrapBlazor.PdfReader/PdfReader.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ public partial class PdfReader
[Parameter]
public Func<Task>? OnPrintingAsync { get; set; }

/// <summary>
/// 通过流加载 PDF 文档回调方法 默认 null
/// </summary>
/// <remarks>优先使用 <see cref="Url"/> 未提供 <see cref="Url"/> 时会尝试调用此回调获得流进行渲染</remarks>
[Parameter]
public Func<Task<Stream>>? OnGetStreamAsync { get; set; }

[Inject, NotNull]
private IStringLocalizer<PdfReader>? Localizer { get; set; }

Expand Down Expand Up @@ -228,19 +235,24 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
/// <inheritdoc/>
/// </summary>
/// <returns></returns>
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new
protected override async Task InvokeInitAsync()
{
Url,
FitMode,
EnableThumbnails,
CurrentPage,
TriggerPagesInit = OnPagesInitAsync != null,
TriggerPagesLoaded = OnPagesLoadedAsync != null,
TriggerPageChanged = OnPageChangedAsync != null,
TriggerTowPagesOnViewChanged = OnTwoPagesOneViewAsync != null,
TriggerScaleChanged = OnScaleChangedAsync != null,
TriggerRotationChanged = OnRotationChanged != null,
});
var _data = await GetPdfStreamDataAsync();
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

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

The GetPdfStreamDataAsync method is called unconditionally and will execute even when Url is provided. According to the documentation comment on OnGetStreamAsync (line 133), the callback should only be invoked when Url is not provided. This wastes resources and doesn't align with the documented behavior. The method should only be called when Url is null or empty.

Suggested change
var _data = await GetPdfStreamDataAsync();
object? _data = null;
if (string.IsNullOrEmpty(Url))
{
_data = await GetPdfStreamDataAsync();
}

Copilot uses AI. Check for mistakes.
await InvokeVoidAsync("init", Id, Interop, new
{
Url,
Data = _data,
FitMode,
EnableThumbnails,
CurrentPage,
TriggerPagesInit = OnPagesInitAsync != null,
TriggerPagesLoaded = OnPagesLoadedAsync != null,
TriggerPageChanged = OnPageChangedAsync != null,
TriggerTowPagesOnViewChanged = OnTwoPagesOneViewAsync != null,
TriggerScaleChanged = OnScaleChangedAsync != null,
TriggerRotationChanged = OnRotationChanged != null,
});
}
Comment on lines +238 to +255
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

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

The JavaScript code (PdfReader.razor.js line 19-21) checks if options.url is null and returns early, which means when only Data is provided without a Url, the PDF will not load. The JavaScript code needs to be updated to handle the case where options.data is provided instead of options.url. Currently, this feature will not work as intended.

Copilot uses AI. Check for mistakes.

/// <summary>
/// 跳转到指定页码方法
Expand All @@ -266,6 +278,19 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
/// <returns></returns>
public Task RotateRight() => InvokeVoidAsync("rotate", Id, 90);

private async Task<byte[]?> GetPdfStreamDataAsync()
{
byte[]? pdfBytes = null;
if (OnGetStreamAsync != null)
{
using var memoryStream = new MemoryStream();
var stream = await OnGetStreamAsync();
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

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

The stream returned by OnGetStreamAsync is not null-checked before use. If the callback returns null, this will cause a NullReferenceException when calling CopyToAsync. Add a null check after awaiting OnGetStreamAsync to handle this case gracefully.

Suggested change
var stream = await OnGetStreamAsync();
var stream = await OnGetStreamAsync();
if (stream == null)
{
return null;
}

Copilot uses AI. Check for mistakes.
await stream.CopyToAsync(memoryStream);
pdfBytes = memoryStream.ToArray();
}
return pdfBytes;
}
Comment on lines +281 to +292
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

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

The stream returned by OnGetStreamAsync is not being disposed. When a callback returns a Stream, the caller is typically responsible for disposing it. The stream should be wrapped in a using statement to ensure proper resource cleanup and prevent memory leaks.

Copilot uses AI. Check for mistakes.

/// <summary>
/// 页面开始初始化时回调方法
/// </summary>
Expand Down
Loading