Add PDFObject from already created pdf element

时间:2016-08-31 18:43:29

标签: asp.net-mvc pdf.js pdfobject

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.

2 个答案:

答案 0 :(得分:1)

您是否曾尝试仅使用标准html来执行此操作? 控制器动作

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;
}

在你看来

<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>

这是否符合您的要求,而不是使用PDFObject?

答案 1 :(得分:0)

请务必将内容处置标头设置为{2 1 0 5 3 4 6 7} source-based form. {2 1 0 4 5 3 6 7} destination-based equivalent ,否则浏览器会尝试下载文件而不是在视口中呈现文件。

请参阅Content-Disposition:What are the differences between "inline" and "attachment"?

就PDFObject与纯HTML而言,为了排除故障,我总是建议尝试使用静态标记(无JS)来显示相同​​的PDF。如果它在那里工作,问题可能在于PDFObject(在这种情况下,PDFObject&#39;对Base64字符串的处理)。如果通过纯标记未正确呈现PDF,则问题可能在于您的文件/ Base64。

您可以从PDFObject静态标记生成器中获取静态标记的副本:http://pdfobject.com/generator

(我应该补充一点,我不能说PDF.js&#39;处理Base64字符串...)