Skip to content

Commit dcafa17

Browse files
committed
feat(PdfReader): print pdf function support stream
1 parent 3f032cc commit dcafa17

1 file changed

Lines changed: 36 additions & 17 deletions

File tree

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

Lines changed: 36 additions & 17 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 => {
@@ -747,25 +752,39 @@ const makeThumb = async page => {
747752
return canvas;
748753
}
749754

750-
const printPdf = url => {
751-
let iframe = document.querySelector(".bb-view-print-iframe");
752-
if (iframe) {
753-
iframe.remove();
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);
754764
}
755765

756-
iframe = document.createElement("iframe");
757-
iframe.classList.add("bb-view-print-iframe");
758-
iframe.style.position = "fixed";
759-
iframe.style.right = "100%";
760-
iframe.style.bottom = "100%";
761-
iframe.src = url;
762-
763-
iframe.onload = () => {
764-
iframe.contentWindow.focus();
765-
iframe.contentWindow.print();
766-
};
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+
}
767777

768-
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+
}
769788
}
770789

771790
export function dispose(id) {

0 commit comments

Comments
 (0)