Display ASP.NET generated pdf byte[] to web page without saving the file

…衆ロ難τιáo~ 提交于 2019-11-30 09:21:22

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

Convert the byte[] to Base64 using,

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

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

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

I couldn't be able to hide the top toolbar which appears in FF by appending #toolbar=0&navpanes=0&statusbar=0.

IE8 needs a saved pdf file to be displayed.

Try this

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

I have being using Convert.ToBase64String(content) for some projects without any issue, until today with a 18 page file at about 1 MB. The error from Chrome's console is Failed to load resource: net::ERR_INVALID_URL. I guess it's because of the string size?!

I ended up using web api and just return it as FileStreamResult instead of Base64 string.

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

Update: It's basically the same to display it on a razor page. I just copied my code for retrieving fax content using RingCentral here. And better yet, just use FileContentResult as you already have the byte[].

public async Task<IActionResult> OnGet(string messageId)
{
    try
    {
        if (!string.IsNullOrEmpty(messageId))
        {
            var ringCentral = _configuration.GetSection("RingCentral");
            var clientId = ringCentral.GetValue<string>("ClientId");
            var clientSecret = ringCentral.GetValue<string>("ClientSecret");
            var userName = ringCentral.GetValue<string>("Username");
            var password = ringCentral.GetValue<string>("Password");
            var ext = ringCentral.GetValue<string>("Extension");
            bool production = ringCentral.GetValue<bool>("Production");

            using (var rc = new RingCentral.RestClient(clientId, clientSecret, production, "FaxSystem"))
            {
                await rc.Authorize(userName, ext, 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;
    }
}

Would 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 would just need to pass your pdf into the object data source.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!