Display ASP.NET generated PDF file [] on a web page without saving the file

I am using iTextSharp to create pdf. I can save a PDF file from PDF bytes [].

byte[] outputPDF = cnt.CreateBreakPDF(); File.WriteAllBytes(pdfOutPutPath, outputPDF); 

What is the best way to display byte[] output on a web page?

I want to show the pdf inside a div on my page. Not a PDF as a complete answer.

I saw the answers for MVC, but I am using an ASP.NET web application.

Is there a better way than using HTTP handlers? I do not want to send all the details for creating a PDF as a query string.

+9
source share
4 answers

I tried this in jsFiddle and it works well in Chrome and FF, you need to check other browsers as well.

Convert byte[] to Base64 using

 string base64PDF = System.Convert.ToBase64String(outputPDF, 0, outputPDF.Length); 

All I had to do was to specify the MIME type as data:application/pdf;base64, in the source and provide Base64 a PDF version.

 <object data="data:application/pdf;base64, JVBERi0xLjQKJeLjz9MKMyA..." type="application/pdf" width="160px"> <embed src="data:application/pdf;base64, JVBERi0xLjQKJeLjz9MKMyA..." type="application/pdf" /> </object> 

I could not hide the top toolbar that appears in FF by adding #toolbar=0&navpanes=0&statusbar=0 .

IE8 needs a saved pdf file to be displayed.

+11
source

try it

 Response.ContentType = "application/pdf"; Response.AddHeader("content-length", outputPDF.Length.ToString()); Response.BinaryWrite(outputPDF); 
+6
source

I use Convert.ToBase64String(content) for some projects without any problems, until today with a file of 18 pages about 1 MB in size. Error from the Chrome console - Failed to load resource: net::ERR_INVALID_URL . I think this is due to row size ?!

I ended up using a web API and just returning it as a FileStreamResult instead of a Base64 string.

 var stream = new MemoryStream(); await stream.WriteAsync(content, 0, content.Length); stream.Position = 0; return new FileStreamResult(stream, "application/pdf"); 

Update: This is basically the same as displaying it on a razor page. I just copied my code to receive fax contents using RingCentral here. Better yet, just use FileContentResult as you already have byte[] .

 public async Task<IActionResult> OnGet(string messageId) { try { using (var rc = new RingCentral.RestClient(setting.ClientId, setting.ClientSecret, setting.Production, "Fax")) { await rc.Authorize(setting.UserName, setting.Extension, setting.Password); var extension = rc.Restapi().Account().Extension(); var outputPDF = await extension.MessageStore(messageId).Content(messageId).Get(); return new FileContentResult(outputPDF, "application/pdf"); } return Page(); } catch (Exception ex) { _logger.Error(ex.Message); throw; } } 
+2
source

Something like this work?

 <div> <object data="myPDF.pdf" type="application/pdf" width="200" height="500"> alt : <a href="myPDF.pdf">myPDF.pdf</a> </object> </div> 

You just need to transfer your pdf file to the object data source.

0
source

Source: https://habr.com/ru/post/947294/


All Articles