-
-
Notifications
You must be signed in to change notification settings - Fork 7
feat(PdfReader): print pdf function support stream #841
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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -192,6 +192,11 @@ const disposePdf = pdf => { | |||||||||
| if (thumbnailsContainer) { | ||||||||||
| thumbnailsContainer.innerHTML = ""; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const iframe = el.querySelector(".bb-view-print-iframe"); | ||||||||||
| if (iframe) { | ||||||||||
| iframe.remove(); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -513,7 +518,7 @@ const addToolbarEventHandlers = (el, pdfViewer, invoke, options) => { | |||||||||
| rotateView(pdfViewer, 90); | ||||||||||
| }); | ||||||||||
| EventHandler.on(toolbar, "click", ".bb-view-print", async e => { | ||||||||||
| printPdf(options.url); | ||||||||||
| await printPdf(el, options); | ||||||||||
| await invoke.invokeMethodAsync("Printing"); | ||||||||||
| }) | ||||||||||
| EventHandler.on(toolbar, "click", ".dropdown-item-pages", async e => { | ||||||||||
|
|
@@ -526,23 +531,15 @@ const addToolbarEventHandlers = (el, pdfViewer, invoke, options) => { | |||||||||
| pdfViewer.spreadMode = 0; | ||||||||||
| } | ||||||||||
| }); | ||||||||||
| EventHandler.on(toolbar, "click", ".bb-view-download", e => { | ||||||||||
| EventHandler.on(toolbar, "click", ".bb-view-download", async e => { | ||||||||||
| let fileName = el.getAttribute('data-bb-download'); | ||||||||||
| if (options.url) { | ||||||||||
| if (fileName === null) { | ||||||||||
| const docTitle = el.querySelector('.bb-view-subject'); | ||||||||||
| if (docTitle) { | ||||||||||
| fileName = docTitle.textContent; | ||||||||||
| } | ||||||||||
| if (fileName === null) { | ||||||||||
| const docTitle = el.querySelector('.bb-view-subject'); | ||||||||||
| if (docTitle) { | ||||||||||
| fileName = docTitle.textContent; | ||||||||||
| } | ||||||||||
| downloadPdf(options.url, fileName); | ||||||||||
| } | ||||||||||
| else if (options.data) { | ||||||||||
| const blob = new Blob([options.data], { type: 'application/pdf' }); | ||||||||||
| const url = window.URL.createObjectURL(blob); | ||||||||||
| downloadPdf(url, fileName); | ||||||||||
| window.URL.revokeObjectURL(url); | ||||||||||
| } | ||||||||||
| await downloadPdf(options, fileName); | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| EventHandler.on(toolbar, "click", ".dropdown-item-presentation", async e => { | ||||||||||
|
|
@@ -571,16 +568,23 @@ const addToolbarEventHandlers = (el, pdfViewer, invoke, options) => { | |||||||||
| }); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const downloadPdf = (url, fileName) => { | ||||||||||
| const downloadPdf = async (options, fileName) => { | ||||||||||
| if (fileName === null) { | ||||||||||
| fileName = "download.pdf"; | ||||||||||
| } | ||||||||||
| const anchorElement = document.createElement('a'); | ||||||||||
| anchorElement.href = url; | ||||||||||
| anchorElement.download = fileName; | ||||||||||
| document.body.appendChild(anchorElement); | ||||||||||
| anchorElement.click(); | ||||||||||
| document.body.removeChild(anchorElement); | ||||||||||
|
|
||||||||||
| await getPdfUrl(options, url => { | ||||||||||
| const anchorElement = document.createElement('a'); | ||||||||||
| anchorElement.href = url; | ||||||||||
| anchorElement.download = fileName; | ||||||||||
| document.body.appendChild(anchorElement); | ||||||||||
| anchorElement.click(); | ||||||||||
| document.body.removeChild(anchorElement); | ||||||||||
|
|
||||||||||
| return new Promise((resolve, reject) => { | ||||||||||
| resolve(); | ||||||||||
| }); | ||||||||||
|
Comment on lines
+583
to
+586
|
||||||||||
| return new Promise((resolve, reject) => { | |
| resolve(); | |
| }); |
Copilot
AI
Dec 18, 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 Promise created here is unnecessary and doesn't serve any purpose. The promise is immediately resolved synchronously without waiting for any asynchronous operation. The callback should either return a meaningful promise (e.g., one that resolves when iframe.onload completes) or not return anything at all since the caller doesn't appear to need the return value.
| return new Promise((resolve, reject) => { | |
| resolve(); | |
| }); |
Copilot
AI
Dec 18, 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.
When options.url is provided, the callback is invoked but its return value is not awaited. This is inconsistent with line 785 where the callback is awaited when options.data is used. This inconsistency could lead to race conditions or unexpected behavior. Either await the callback in both cases or remove the await in both cases, depending on whether the callback is expected to be asynchronous.
| callback(options.url); | |
| await callback(options.url); |
Copilot
AI
Dec 18, 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.
Use 'const' or 'let' instead of 'var' to declare the url variable. The use of 'var' is inconsistent with modern JavaScript best practices and can lead to unexpected scoping issues.
| var url = window.URL.createObjectURL(blob); | |
| const url = window.URL.createObjectURL(blob); |
Copilot
AI
Dec 18, 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 blob URL is revoked immediately after calling the callback, which may cause issues if the callback uses the URL asynchronously. For example, in printPdf, the URL is assigned to iframe.src, but the actual loading happens asynchronously via the onload event. Revoking the URL before the iframe finishes loading could result in a failed load. Consider revoking the URL only after the asynchronous operation completes, or adding a delay before revocation.
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.
Avoid automated semicolon insertion (95% of all statements in the enclosing function have an explicit semicolon).