-
-
Notifications
You must be signed in to change notification settings - Fork 5
feat(PdfReader): support download #773
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -109,8 +109,6 @@ const loadPdf = async (el, invoke, options) => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const loadMetadata = (el, pdfViewer, metadata) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log(metadata); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const filename = el.querySelector('.bb-view-pdf-dialog-filename'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const docTitle = el.querySelector('.bb-view-subject'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filename.textContent = docTitle.textContent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -439,6 +437,17 @@ const addToolbarEventHandlers = (el, pdfViewer, invoke, options) => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pdfViewer.spreadMode = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| EventHandler.on(toolbar, "click", ".bb-view-download", e => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (options.url) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const docTitle = el.querySelector('.bb-view-subject'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const anchorElement = document.createElement('a'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| anchorElement.href = options.url; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| anchorElement.download = docTitle.textContent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| anchorElement.download = docTitle.textContent; | |
| anchorElement.download = (docTitle && docTitle.textContent) ? docTitle.textContent : ''; |
ArgoZhang marked this conversation as resolved.
Show resolved
Hide resolved
Copilot
AI
Dec 3, 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 download implementation using anchor element with download attribute will not work for cross-origin URLs due to CORS restrictions. When the PDF URL is from a different origin, the browser will navigate to the URL instead of downloading it. This is different from the print functionality which uses an iframe. Consider:
- Documenting this limitation in the component documentation, or
- Implementing a server-side proxy download mechanism similar to how the previous
OnDownloadAsynccallback allowed, or - Using a fetch + Blob approach for same-origin URLs:
if (options.url) {
const docTitle = el.querySelector('.bb-view-subject');
try {
const response = await fetch(options.url);
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const anchorElement = document.createElement('a');
anchorElement.href = url;
anchorElement.download = docTitle?.textContent || 'document.pdf';
anchorElement.click();
window.URL.revokeObjectURL(url);
anchorElement.remove();
} catch (error) {
// Fallback to direct link for cross-origin URLs
const anchorElement = document.createElement('a');
anchorElement.href = options.url;
anchorElement.download = docTitle?.textContent || 'document.pdf';
anchorElement.click();
anchorElement.remove();
}
}| EventHandler.on(toolbar, "click", ".bb-view-download", e => { | |
| if (options.url) { | |
| const docTitle = el.querySelector('.bb-view-subject'); | |
| const anchorElement = document.createElement('a'); | |
| anchorElement.href = options.url; | |
| anchorElement.download = docTitle.textContent; | |
| anchorElement.click(); | |
| anchorElement.remove(); | |
| EventHandler.on(toolbar, "click", ".bb-view-download", async e => { | |
| if (options.url) { | |
| const docTitle = el.querySelector('.bb-view-subject'); | |
| try { | |
| const response = await fetch(options.url); | |
| if (!response.ok) throw new Error("Network response was not ok"); | |
| const blob = await response.blob(); | |
| const url = window.URL.createObjectURL(blob); | |
| const anchorElement = document.createElement('a'); | |
| anchorElement.href = url; | |
| anchorElement.download = docTitle?.textContent || 'document.pdf'; | |
| document.body.appendChild(anchorElement); | |
| anchorElement.click(); | |
| window.URL.revokeObjectURL(url); | |
| anchorElement.remove(); | |
| } catch (error) { | |
| // Fallback to direct link for cross-origin URLs | |
| const anchorElement = document.createElement('a'); | |
| anchorElement.href = options.url; | |
| anchorElement.download = docTitle?.textContent || 'document.pdf'; | |
| document.body.appendChild(anchorElement); | |
| anchorElement.click(); | |
| anchorElement.remove(); | |
| } |
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.
Removing the
OnDownloadAsyncparameter is a breaking API change that will affect existing consumers of this component. Users who were relying on this callback to implement custom download logic (e.g., logging, authentication checks, custom file sources) will no longer be able to do so.Consider:
[Obsolete]in this versionIf this breaking change is intentional, ensure it's clearly documented in the changelog and migration guide.