-
-
Notifications
You must be signed in to change notification settings - Fork 7
feat(PdfReader): add OnGetStreamAsync parameter #813
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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; } | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -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(); | ||||||||||||||
| 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
|
||||||||||||||
|
|
||||||||||||||
| /// <summary> | ||||||||||||||
| /// 跳转到指定页码方法 | ||||||||||||||
|
|
@@ -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(); | ||||||||||||||
|
||||||||||||||
| var stream = await OnGetStreamAsync(); | |
| var stream = await OnGetStreamAsync(); | |
| if (stream == null) | |
| { | |
| return null; | |
| } |
Copilot
AI
Dec 12, 2025
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.
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.
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.
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.