Context
I'm trying to implement a button that generates, and then downloads, a PDF. The method I'm calling to do so looks something like this:
private async void ExportToPdf(){ string fileName = $"{_entity.Name}.pdf"; string filePath = Path.Join(AppDomain.Current.BaseDirectory, "PdfBuffer", fileName); await _pdfBuilder.GeneratePdf(_entity, printPath); await _fileDownloadService.DownloadAsync(fileName, filePath); File.Delete(filePath);}Some key points:
- _pdfBuilder is a custom PDF builder. While not strictly relevant to the question, I'm sure someone will point this out: I am not in control of this code. I have to save to a file because the public API does not allow me to just get the bytes.
- _fileDownloadService is a scoped service (requires an
IJSRuntime), adapted from this blogpost.
Problem
The problem lies in the fact that _pdfBuilder.GeneratePdf(...)must run on the server. The client is incapable of the work required, so awaiting the method just hangs indefinitely and never completes. The file download service, then, must be able to stream the PDF file's bytes down to the client. As blazor somewhat blurs the line between client and server, I'm not sure how to go about making this happen.
The question, therefore, is: How can I explicitly tell _pdfBuilder.GeneratePdf(...) to run on the server?