Skip to content

Commit b63be39

Browse files
authored
feat(PdfReader): add OnPrintingAsync parameter (#731)
* feat: 增加打印回调方法 * feat(PdfReader): add OnPrintingAsync parameter
1 parent bb3597b commit b63be39

3 files changed

Lines changed: 61 additions & 3 deletions

File tree

src/components/BootstrapBlazor.PdfReader/BootstrapBlazor.PdfReader.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>10.0.1-beta07</Version>
4+
<Version>10.0.1-beta08</Version>
55
</PropertyGroup>
66

77
<PropertyGroup>

src/components/BootstrapBlazor.PdfReader/PdfReader.razor.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,17 @@ public partial class PdfReader
110110
public string? MoreButtonIcon { get; set; }
111111

112112
/// <summary>
113-
/// 点击下载按钮回调方法 默认 null 使用组件内置下载功能
113+
/// 点击下载按钮回调方法 默认 null
114114
/// </summary>
115115
[Parameter]
116116
public Func<Task>? OnDownloadAsync { get; set; }
117117

118+
/// <summary>
119+
/// 正在打印回调方法 默认 null
120+
/// </summary>
121+
[Parameter]
122+
public Func<Task>? OnPrintingAsync { get; set; }
123+
118124
private string? ClassString => CssBuilder.Default("bb-pdf-reader")
119125
.AddClassFromAttributes(AdditionalAttributes)
120126
.Build();
@@ -347,4 +353,17 @@ public async Task PageChanged(uint pageIndex)
347353
await OnPageChangedAsync(pageIndex);
348354
}
349355
}
356+
357+
/// <summary>
358+
/// 正在打印回调方法
359+
/// </summary>
360+
/// <returns></returns>
361+
[JSInvokable]
362+
public async Task Printing()
363+
{
364+
if (OnPrintingAsync != null)
365+
{
366+
await OnPrintingAsync();
367+
}
368+
}
350369
}

src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ const addEventListener = (el, pdfViewer, eventBus, invoke, options) => {
127127
}
128128

129129
if (options.triggerPagesInit === true) {
130-
await invoke.invokeMethodAsync("pagesInit", numPages);
130+
await invoke.invokeMethodAsync("PagesInit", numPages);
131131
}
132132
});
133133

@@ -139,6 +139,12 @@ const addEventListener = (el, pdfViewer, eventBus, invoke, options) => {
139139
if (options.triggerPagesLoaded === true) {
140140
await invoke.invokeMethodAsync("PagesLoaded", e.pagesCount);
141141
}
142+
143+
const controls = el.querySelector(".bb-view-controls");
144+
EventHandler.on(controls, "click", ".bb-view-print", e => {
145+
printPdf(options.url);
146+
await invoke.invokeMethodAsync("Printing");
147+
});
142148
})
143149

144150
eventBus.on("pagechanging", async evt => {
@@ -276,6 +282,31 @@ const makeThumb = async page => {
276282
return canvas;
277283
}
278284

285+
const printPdf = url => {
286+
let iframe = document.querySelector(".bb-view-print-iframe");
287+
if (iframe) {
288+
iframe.remove();
289+
}
290+
291+
iframe = document.createElement("iframe");
292+
iframe.classList = "bb-view-print-iframe";
293+
iframe.style.position = "fixed";
294+
iframe.style.right = "100%";
295+
iframe.style.bottom = "100%";
296+
iframe.src = url;
297+
298+
iframe.onload = () => {
299+
iframe.contentWindow.addEventListener('afterprint', function () {
300+
document.body.removeChild(iframe);
301+
});
302+
303+
iframe.contentWindow.focus();
304+
iframe.contentWindow.print();
305+
};
306+
307+
document.body.appendChild(iframe);
308+
}
309+
279310
export function dispose(id) {
280311
Data.remove(id);
281312

@@ -304,5 +335,13 @@ export function dispose(id) {
304335
if (thumbnailsContainer) {
305336
EventHandler.off(thumbnailsContainer, "click");
306337
}
338+
339+
const controls = el.querySelector(".bb-view-controls");
340+
EventHandler.off(controls, "click");
341+
342+
const iframe = document.querySelector('.bb-view-print-iframe');
343+
if (iframe) {
344+
iframe.remove();
345+
}
307346
}
308347
}

0 commit comments

Comments
 (0)