1

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.

Godrules500
  • 467
  • 6
  • 25
  • You did not specify the browser that has issue. Based on stack you are using, it's probabl IE and it has a limit on base64 encoded URLs size -- most of PDFs will not fit this limit. – async5 Aug 31 '16 at 18:57
  • I can get the PDF to display. The problem is, I really need the functionality of pdf.js, so I am trying to get it to load inside of pdf.js – Godrules500 Aug 31 '16 at 18:59
  • Not sure if this will help... http://stackoverflow.com/questions/6439634/view-pdf-as-part-of-the-page Does the 'ActionResult' need to be specified as a 'FileStreamResult' return type? – Gwasshoppa Aug 31 '16 at 21:27
  • Interestingly enough the only samples provided by PDFObject relate to an actual .pdf file being specified in the scr of the .embed function... not a FileStreamResult... Is it actually possible using PDFObject to specify a FileStream in the src? – Gwasshoppa Aug 31 '16 at 21:45

2 Answers2

1

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?

Gwasshoppa
  • 884
  • 12
  • 18
0

Be sure to set the content disposition header to inline or the browser will try to download the file rather than render it in the viewport.

See Content-Disposition:What are the differences between "inline" and "attachment"?

As far as PDFObject versus plain HTML, for troubleshooting I always recommend trying static markup (no JS) to display the same PDF. If it works there, the problem may lie with PDFObject (in this case, PDFObject's handling of Base64 strings). If the PDF is not properly rendered via plain markup, the issue probably lies with your file/Base64.

You can grab a copy of static markup from the PDFObject static markup generator: http://pdfobject.com/generator

(I should add that I can't speak to PDF.js' handling of Base64 strings...)

Community
  • 1
  • 1
pipwerks
  • 4,440
  • 1
  • 20
  • 24