Skip to content

added functionality to download pdf #1101

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion blazorbootstrap/Components/PdfViewer/PdfViewer.razor
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
<li><span class="dropdown-item d-flex align-items-center" role="button" @onclick="SwitchOrientationAsync"><Icon Class="mx-2" Name="IconName.Compass" Size="IconSize.x5" /> Switch Orientation</span></li>
<li><span class="dropdown-item d-flex align-items-center" role="button" @onclick="FirstPageAsync"><Icon Class="mx-2" Name="IconName.ChevronBarUp" Size="IconSize.x5" /> Go to First Page</span></li>
<li><span class="dropdown-item d-flex align-items-center" role="button" @onclick="LastPageAsync"><Icon Class="mx-2" Name="IconName.ChevronBarDown" Size="IconSize.x5" /> Go to Last Page</span></li>
<li><span class="dropdown-item d-flex align-items-center" role="button" @onclick="ResetZoomAsync"><Icon Class="mx-2" Name="IconName.PlusSlashMinus" Size="IconSize.x5" /> Reset Zoom</span></li>
<li><span class="dropdown-item d-flex align-items-center" role="button" @onclick="ResetZoomAsync"><Icon Class="mx-2" Name="IconName.PlusSlashMinus" Size="IconSize.x5" /> Reset Zoom</span></li>
<li><span class="dropdown-item d-flex align-items-center" role="button" @onclick="DownloadAsync"><Icon Class="mx-2" Name="IconName.PlusSlashMinus" Size="IconSize.x5" /> Download</span></li>
</ul>
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions blazorbootstrap/Components/PdfViewer/PdfViewer.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ private async Task PageNumberChangedAsync(int value)

private async Task PrintAsync() => await PdfViewerJsInterop.PrintAsync(objRef!, Id!, Url!);

private async Task DownloadAsync() => await PdfViewerJsInterop.DownloadAsync(objRef!, Id!, Url!);

private async Task ResetZoomAsync()
{
zoomLevel = defaultZoomLevel;
Expand Down
6 changes: 6 additions & 0 deletions blazorbootstrap/Components/PdfViewer/PdfViewerJsInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,11 @@ public async Task ZoomInOutAsync(object objRef, string elementId, double scale)
await module.InvokeVoidAsync("zoomInOut", objRef, elementId, scale);
}

public async Task DownloadAsync(object objRef, string elementId, string url)
{
var module = await moduleTask.Value;
await module.InvokeVoidAsync("downloadDocument", objRef, elementId, "document.pdf");
}

#endregion
}
42 changes: 41 additions & 1 deletion blazorbootstrap/wwwroot/blazor.bootstrap.pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Pdf {
this.pageNumPending = null;
this.scale = 1;
this.rotation = 0;
this.url = '';

pdfInstances[this.id] = this;
}
Expand Down Expand Up @@ -198,7 +199,7 @@ export function initialize(dotNetHelper, elementId, scale, rotation, url) {
const pdf = new Pdf(elementId);
pdf.scale = scale;
pdf.rotation = rotation;

pdf.url = url;
pdfJS.getDocument(url).promise.then(function (doc) {
pdf.pdfDoc = doc;
pdf.pagesCount = doc.numPages;
Expand Down Expand Up @@ -265,3 +266,42 @@ function setPdfViewerMetaData(dotNetHelper, pdf) {

dotNetHelper.invokeMethodAsync('SetPdfViewerMetaData', { pagesCount: pdf.pagesCount, pageNumber: pdf.pageNum });
}

export function downloadDocument(dotNetHelper, elementId, filename) {
const pdf = Pdf.getPdf(elementId);
if (pdf.url) {
if (pdf.url.indexOf('data:') === 0) {
const split = pdf.url.split(',');
const base64Data = split.length > 1 ? split[1] : '';
try {
saveAsFile(filename, base64Data)
} catch (e) {
console.error('Failed to decode base64 PDF:', e);
}
}
else {
saveAsFileFromUrl(pdf.url)
}
}
else {
console.error('Pdf Url empty');
}
}

function saveAsFile(filename, byteBase64) {
var link = document.createElement('a');
link.download = filename;
link.href = "data:application/octet-stream;base64," + byteBase64;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}

function saveAsFileFromUrl(url) {
var link = document.createElement('a');
link.href = url;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}