11

I'm using iTextSharp for generating a pdf. I can save the PDF file from the PDF byte[].

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

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

I want to show the PDF inside a div in my page. Not the PDF as a full response.

I've seen answers for MVC, but I'm using ASP.NET Web Application.

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

Sen Jacob
  • 3,384
  • 3
  • 35
  • 61

4 Answers4

15

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.

Sen Jacob
  • 3,384
  • 3
  • 35
  • 61
  • 3
    This works beautifully, but you do not need to wrap the `` in an ``. I just put `` and it worked perfectly. Thanks. – EternalWulf Jul 15 '14 at 07:45
  • Did you ever have any luck appending Open PDF Parameters to a base64 source? – nulltron Jun 06 '17 at 23:53
  • I did not try to send any other parameters, but the PDF data itself as base64. But I think in the web browser, we can use the arraybuffer response of PDF stream and convert it to Uint8Array or blob and then use window.URL to create a browser link to display the PDF more easily in modern browsers. – Sen Jacob Jun 07 '17 at 17:25
  • I tried this and it worked both as an object and as an embed. My C# code was something like: byte[] bPDF = File.ReadAllBytes("filename.pdf"); string base64PDF = System.Convert.ToBase64String(bPDF, 0, bPDF.Length); wrapped in string str = ""; My Razor code was @Html.Raw(PDFModel.strReturnPDF()) . It displayed the full height PDF, 51 pages. – Miguelito Aug 11 '20 at 11:30
6

Try this

Response.ContentType = "application/pdf";
Response.AddHeader("content-length", outputPDF.Length.ToString());
Response.BinaryWrite(outputPDF);
Karthik
  • 2,391
  • 6
  • 34
  • 65
2

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
    {
        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;
    }
}
Weihui Guo
  • 3,669
  • 5
  • 34
  • 56
  • That is indeed a good option in server part. Since the question is about how to display PDF on client page, could you please add how did you display it in client page? – Sen Jacob Aug 16 '19 at 09:51
  • 1
    @SenJacob Simply call the api and you'll see the pdf on page. I just figured out that it's the same to display on a razor page. I'll update my answer. – Weihui Guo Aug 16 '19 at 12:06
0

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.

ovaltein
  • 1,185
  • 2
  • 12
  • 34
  • Thank you, but as I asked in the question, I don't have a saved `PDF` file. I need to know whether I can embed the `PDF bytes` directly to the `object`. As we do with images like `` – Sen Jacob Jul 10 '13 at 04:34