In this article, we will display the PDF on the browser as a response to the .Net5 Web API endpoint.
So that's all about implementing .Net Web API endpoints to display the PDF in browser.
Create A .Net5 Web API Sample Application:
Now let's create a .Net5 Web API sample application to begin our coding.
API Endpoint To Load PDF From Physical Path:
Now we are going to create a web API endpoint that will read the PDF file from the server physical folder. So first let's add a sample pdf file in our API server, so create 'wwwroot' folder(framework understandable folder to store the static files). Now add the 'pdf' folder inside of the 'wwwroot' folder and then add our sample pdf file as below.
Create a new controller like 'PdfController.cs' inside of the 'Controllers' folder.
Controllers/PdfController.cs:
using System.IO; using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; namespace Dot5.PDF.OnBrowser.Controllers { [ApiController] [Route("[controller]")] public class PdfController : ControllerBase { [HttpGet] [Route("load-from-server-physical-location")] public IActionResult PhysicalLocation() { string physicalPath = "wwwroot/pdf/mvc.pdf"; byte[] pdfBytes = System.IO.File.ReadAllBytes(physicalPath); MemoryStream ms = new MemoryStream(pdfBytes); return new FileStreamResult(ms, "application/pdf"); } } }
- (Line: 14) Configured route for our action method.
- (Line: 17) Pdf file physical folder location.
- (Line: 18) The 'System.IO.File.ReadAllBytes()' method reads the file as an array of bytes.
- (Line: 19) Storing the pdf bytes array data into the memory of the application using 'System.IO.MemoryStream'.
- (Line: 20) The 'FileStreamResult' is the default method provided by API, that outputs the pdf memory bytes. Here we also need to define the content-type(eg: "application/pdf") so that browser can understand the type of stream that to be rendered on the browser.
API Endpoint To Load PDF From External Domain:
Another way of reading PDF doc is from an external domain through the HTTP call from our API. For this demo, I'm going to use 'Named Type' HttpClient technique. The 'Named Type' means external domain will be registered with a specific name in 'HttpClientFactory', so we have to create a 'HttpClient' instance from the 'HttpClientFactory' by using the registered name.
For this demo the external domain sample PDF I'm going to use is 'http://www.africau.edu/images/default/sample.pdf'.
In 'Startup.cs' let's register our external domain with a specific name.
Startup.cs:
services.AddHttpClient("PdfDomain", client => { client.BaseAddress = new Uri("http://www.africau.edu/"); });
- Here our 'HttpClient' name is 'PdfDomain'.
Controllers/PdfController.cs:
using System.IO; using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; namespace Dot5.PDF.OnBrowser.Controllers { [ApiController] [Route("[controller]")] public class PdfController : ControllerBase { private readonly IHttpClientFactory _httpClientFactory; public PdfController(IHttpClientFactory httpClientFactory) { _httpClientFactory = httpClientFactory; } [HttpGet] [Route("external-domain-pdf")] public async Task<IActionResult> ExternlDomainPdf() { string url = "/images/default/sample.pdf"; var httpClient = _httpClientFactory.CreateClient("PdfDomain"); var response = await httpClient.GetAsync(url); MemoryStream ms = new MemoryStream(await response.Content.ReadAsByteArrayAsync()); return new FileStreamResult(ms, "application/pdf"); } } }
- (Line: 14) Injected the 'IHttpClientFactory'.
- (Line: 22) Since the domain is registered in 'Startup.cs' we simply define the URL path.
- (Line: 23) Creating the 'HttpClient' instance from the 'IHttpClientFactory' using the registered name like 'PdfDomain'.
- (Line: 24) Invoking the 'HttpGet' call.
- (Line: 25) Copying the response stream byte array into the memory stream.
- (Line: 26) Using the 'FileStreamResult' method returning our PDF stream data to the client browser.
So that's all about implementing .Net Web API endpoints to display the PDF in browser.
Video Session:
Support Me!
Buy Me A Coffee
PayPal Me
Wrapping Up:
Hopefully, I think this article delivered some useful information on .Net5 Web API display PDF in the browser. I love to have your feedback, suggestions, and better techniques in the comment section below.
Comments
Post a Comment