
I am reviewing the .net core package for web app development and I am interested in the PDF viewer. I create PDF documents programmatically (C# / iText on the server) and was wondering if I could use a MemoryStream as a datasource for the PDF viewer without having to write the document to file first. Rendering from RAM would be much the preferred option.
Kind regards,
Paul S
4 Answers, 1 is accepted
The simplest configuration of the PDF viewer needs a URL that will return the PDF file: https://demos.telerik.com/aspnet-core/pdfviewer/index. Thus, you can point it to an action that will get the stream and write it out as a file in its response. This can be a good starting point for that task: https://stackoverflow.com/questions/52127108/returning-a-pdf-from-an-asp-net-core-2-controller.
Regards,
Marin Bratanov
Progress Telerik

Thanks Marin,
That's helpful but some further questions, if I may.
1) The first example uses a file as an argument to the URL but the second is more like the kind of thing I am trying to do, Would it work if I supplied a reference to a web API that returns a PDF on demand in the response as "application/pdf" something like:
@{
var reportUrl = "https://www.myReportServer.com/api/Report/123";
...
.PdfjsProcessing(pdf => pdf.File(Url.Content(reportUrl )))
}
2) The example is driven client-side. Can I control the viewer using server-side code (C#)? I am generating the MemoryStream version of my PDFs on the server.
3) To help with 2) above, presumably the client side control can interact with MVC C# code blocks in .cshtml files? (i.e. I can supply @parameters to the control via embedded C# code that refers to data models and so on).
Kind regards,
Paul
The .File() config takes a string that is the URL the widget will request from the browser. What the application has behind that URL is up to the app - it can be a static file, it can be a parametrized action (including WebAPI Get), what's important is that it sends back a PDF file.
The second example is tied to our own Document Processing libraries and uses their specific features. The fact that it uses streams does not mean anything additional, they are just a way of obtaining data. You can use a stream in any controller and return the PDF file from it.
Thus, you can use any logic in the view to generate that string - you could send it from the controller in the ViewData, you can have it as a part of a model the view uses, or you can build it in the razor markup based on some other logic.
Regards,
Marin Bratanov
Progress Telerik

Thanks Marin,
That all looks good.
I'm downloading the trial as I type...
:-)