Skip to content

Commit 70f8dff

Browse files
authored
feat(PdfReader): print pdf function support stream (#841)
* refactor: 重构下载 pdf 功能 * feat(PdfReader): print pdf function support stream * chore: bump version 10.0.21
1 parent e609ce8 commit 70f8dff

2 files changed

Lines changed: 59 additions & 41 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.20</Version>
4+
<Version>10.0.21</Version>
55
</PropertyGroup>
66

77
<PropertyGroup>

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

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ const disposePdf = pdf => {
192192
if (thumbnailsContainer) {
193193
thumbnailsContainer.innerHTML = "";
194194
}
195+
196+
const iframe = el.querySelector(".bb-view-print-iframe");
197+
if (iframe) {
198+
iframe.remove();
199+
}
195200
}
196201
}
197202

@@ -513,7 +518,7 @@ const addToolbarEventHandlers = (el, pdfViewer, invoke, options) => {
513518
rotateView(pdfViewer, 90);
514519
});
515520
EventHandler.on(toolbar, "click", ".bb-view-print", async e => {
516-
printPdf(options.url);
521+
await printPdf(el, options);
517522
await invoke.invokeMethodAsync("Printing");
518523
})
519524
EventHandler.on(toolbar, "click", ".dropdown-item-pages", async e => {
@@ -526,23 +531,15 @@ const addToolbarEventHandlers = (el, pdfViewer, invoke, options) => {
526531
pdfViewer.spreadMode = 0;
527532
}
528533
});
529-
EventHandler.on(toolbar, "click", ".bb-view-download", e => {
534+
EventHandler.on(toolbar, "click", ".bb-view-download", async e => {
530535
let fileName = el.getAttribute('data-bb-download');
531-
if (options.url) {
532-
if (fileName === null) {
533-
const docTitle = el.querySelector('.bb-view-subject');
534-
if (docTitle) {
535-
fileName = docTitle.textContent;
536-
}
536+
if (fileName === null) {
537+
const docTitle = el.querySelector('.bb-view-subject');
538+
if (docTitle) {
539+
fileName = docTitle.textContent;
537540
}
538-
downloadPdf(options.url, fileName);
539-
}
540-
else if (options.data) {
541-
const blob = new Blob([options.data], { type: 'application/pdf' });
542-
const url = window.URL.createObjectURL(blob);
543-
downloadPdf(url, fileName);
544-
window.URL.revokeObjectURL(url);
545541
}
542+
await downloadPdf(options, fileName);
546543
});
547544

548545
EventHandler.on(toolbar, "click", ".dropdown-item-presentation", async e => {
@@ -571,16 +568,23 @@ const addToolbarEventHandlers = (el, pdfViewer, invoke, options) => {
571568
});
572569
}
573570

574-
const downloadPdf = (url, fileName) => {
571+
const downloadPdf = async (options, fileName) => {
575572
if (fileName === null) {
576573
fileName = "download.pdf";
577574
}
578-
const anchorElement = document.createElement('a');
579-
anchorElement.href = url;
580-
anchorElement.download = fileName;
581-
document.body.appendChild(anchorElement);
582-
anchorElement.click();
583-
document.body.removeChild(anchorElement);
575+
576+
await getPdfUrl(options, url => {
577+
const anchorElement = document.createElement('a');
578+
anchorElement.href = url;
579+
anchorElement.download = fileName;
580+
document.body.appendChild(anchorElement);
581+
anchorElement.click();
582+
document.body.removeChild(anchorElement);
583+
584+
return new Promise((resolve, reject) => {
585+
resolve();
586+
});
587+
});
584588
}
585589

586590
const removeToolbarEventHandlers = el => {
@@ -748,25 +752,39 @@ const makeThumb = async page => {
748752
return canvas;
749753
}
750754

751-
const printPdf = url => {
752-
let iframe = document.querySelector(".bb-view-print-iframe");
753-
if (iframe) {
754-
iframe.remove();
755-
}
756-
757-
iframe = document.createElement("iframe");
758-
iframe.classList.add("bb-view-print-iframe");
759-
iframe.style.position = "fixed";
760-
iframe.style.right = "100%";
761-
iframe.style.bottom = "100%";
762-
iframe.src = url;
763-
764-
iframe.onload = () => {
765-
iframe.contentWindow.focus();
766-
iframe.contentWindow.print();
767-
};
755+
const printPdf = async (el, options) => {
756+
let iframe = el.querySelector(".bb-view-print-iframe");
757+
if (iframe === null) {
758+
iframe = document.createElement("iframe");
759+
iframe.classList.add("bb-view-print-iframe");
760+
iframe.style.position = "fixed";
761+
iframe.style.right = "100%";
762+
iframe.style.bottom = "100%";
763+
el.appendChild(iframe);
764+
}
765+
766+
await getPdfUrl(options, url => {
767+
iframe.src = url;
768+
iframe.onload = () => {
769+
iframe.contentWindow.focus();
770+
iframe.contentWindow.print();
771+
};
772+
return new Promise((resolve, reject) => {
773+
resolve();
774+
});
775+
});
776+
}
768777

769-
document.body.appendChild(iframe);
778+
const getPdfUrl = async (options, callback) => {
779+
if (options.url) {
780+
callback(options.url);
781+
}
782+
else if (options.data) {
783+
const blob = new Blob([options.data], { type: 'application/pdf' });
784+
var url = window.URL.createObjectURL(blob);
785+
await callback(url);
786+
window.URL.revokeObjectURL(url);
787+
}
770788
}
771789

772790
export function dispose(id) {

0 commit comments

Comments
 (0)