asp.net-mvcpdf.jspdfobject

Add PDFObject from already created pdf element


I have a pdf element that I am returning as a string base64 element since it is an MVC Web Application and the files live on a server. I am currently using PDFObject and pdf.js to try and view this PDF in the browser. However, I seem unable to display the PDF, unless I pass a url, which won't work when I put this application in IIS on a server.

So is there a way to have my embedded pdf with the src="{my base 64 string}, and then wrap the PDFObject around that? If not, is there a way, via PDFObject, to use a base64 string instead of a url?

Also, this is in IE 11

UPDATE Here is my controller

public ActionResult GetPDFString(string instrumentType, string booktype, string book, string startpage, string EndPage)
    {
        LRS_Settings settings = ctxLRS.LRS_Settings.FirstOrDefault();
        string root = settings.ImagePathRoot;
        string state = settings.State;
        string county = settings.County;
        g_filePath = @"\\10.20.170.200\Imaging\GA\075\Daily\" + instrumentType + "\\" + book + "\\";
        //g_filePath = @"\\10.15.100.225\sup_court\Imaging\GA\075\Daily\" + instrumentType + "\\" + book + "\\";
        byte[] file = imgConv.ConvertTifToPDF(g_filePath, booktype, book, startpage, EndPage);

        var ms = new MemoryStream(file);
        var fsResult = new FileStreamResult(ms, "application/pdfContent");

        return fsResult;


    //return imgConv.ConvertTifToPDF(g_filePath, booktype, book, startpage, EndPage);
}

Here is my jquery

var options = {
                pdfOpenParams: {
                    navpanes: 1,
                    toolbar: 0,
                    statusbar: 0,
                    pagemode: 'none',
                    pagemode: "none",
                    page: 1,
                    zoom: "page-width",
                    enableHandToolOnLoad: true
                },
                forcePDFJS: true,
                PDFJS_URL: "/PDF.js/web/viewer.html"
            }

            PDFObject.embed("@Url.Action("GetPDFString", "DocumentView", new { instrumentType = ViewBag.instrumentType, BookType = Model.BookType, Book = ViewBag.Book, StartPage = ViewBag.StartPage, EndPage = ViewBag.endPage, style = "height:100%; width100%;" })", "#PDFViewer", options);

The problem is now, instead of showing the PDF inside of #PDFViewer, it is trying to download the file. Could someone please assist me on the final piece to the puzzle. This is driving me crazy.


Solution

  • Have you tried to use just the standard html to do this instead? Controller Action

    public ActionResult GetAttachment(string instrumentType, string booktype, string book, string startpage, string EndPage)
    {
        var fileStream = new FileStream(Server.MapPath("~/Content/files/sample.pdf"), 
                                         FileMode.Open,
                                         FileAccess.Read
                                       );
        var fsResult = new FileStreamResult(fileStream, "application/pdf");
        return fsResult;
    }
    

    In your view

    <div id="PDFViewer">
        <embed src="@Url.Action("GetAttachment", "DocumentView", new { instrumentType = ViewBag.instrumentType, BookType = Model.BookType, Book = ViewBag.Book, StartPage = ViewBag.StartPage, EndPage = ViewBag.endPage })" width="100%" height="100%" type="application/pdf"></embed>
    </div>
    

    Would this suit your requirements rather than using PDFObject?