How to render PDF byte array in an HTML Object tag

Joe picture Joe · Jun 6, 2015 · Viewed 7.4k times · Source

I have a PDF that was generated by ASPose as a byte array, and I want to render that PDF inside of a component on a web page.

        using (MemoryStream docStream = new MemoryStream())
        {
            doc.Save(docStream, Aspose.Words.SaveFormat.Pdf);
            documentStream = docStream.ToArray();
        }

I thought it would just be a simple variation on assigning the byte array to the data attribute in the code behind. Below is the setup, and some of the variations that I tried. What can I do to render that array of bytes as a visible sub-component of my web page?

       HtmlGenericControl pTag = new HtmlGenericControl("p");
        pTag.InnerText = "No Letter was Generated.";
        pTag.ID = "errorMsg";
        HtmlGenericControl objTag = new HtmlGenericControl("object");
        objTag.Attributes["id"] = "pdf_content";
        objTag.Attributes["height"] = "400";
        objTag.Attributes["width"] = "500";
        String base64EncodedPdf = System.Convert.ToBase64String(pdfBytes);

        //1-- Brings up the "No Letter was Generated" message
         objTag.Attributes["data"] = "data:application/pdf;base64," + base64EncodedPdf.ToString(); 

        //2-- Brings up the gray PDF background and NO initialization bar.
        objTag.Attributes["type"] = "application/pdf";
        objTag.Attributes["data"] = "data:application/pdf;base64," + base64EncodedPdf.ToString();

        //3-- Brings up the gray PDF background and the initialization bar, then stops.
        objTag.Attributes["type"] = "application/pdf";
        objTag.Attributes["data"] = pdfBytes.ToString();  

        //4-- Brings up a white square of the correct size, containing a circle with a slash in the top left corner.
        objTag.Attributes["data"] = "application/pdf" + pdfBytes.ToString();


        objTag.Controls.Add(pTag);
        pdf.Controls.Add(objTag);

Answer

John Wu picture John Wu · Jun 6, 2015

The data attribute of the object tag should contain a URL which points to an endpoint which will provide the PDF byte stream. It should not contain the byte stream itself inline.

To make this page work, you will need to add an additional handler that provides the byte stream, e.g. GetPdf.ashx. The ProcessRequest method of the handler would prepare the PDF bytestream and return it inline in the response, preceded by the appropriate headers indicating it is a PDF object.

protected void ProcessRequest(HttpContext context)
{
    byte[] pdfBytes = GetPdfBytes(); //This is where you should be calling the appropriate APIs to get the PDF as a stream of bytes
    var response = context.Response;
    response.ClearContent();
    response.ContentType = "application/pdf";
    response.AddHeader("Content-Disposition", "inline");
    response.AddHeader("Content-Length", pdfBytes.Length.ToString());
    response.BinaryWrite(pdfBytes); 
    response.End();
}

Meanwhile your main page would populate the data attibute with a URL pointing at the the handler, e.g.

objTag.Attributes["data"] = "GetPdf.ashx";