从已创建的 pdf 元素添加 PDFObject

Add PDFObject from already created pdf element

我有一个 pdf 元素,我将其作为字符串 base64 元素返回,因为它是一个 MVC Web 应用程序并且文件位于服务器上。我目前正在使用 PDFObject 和 pdf.js 尝试在浏览器中查看此 PDF。但是,我似乎无法显示 PDF,除非我传递 url,当我将此应用程序放在服务器上的 IIS 中时,这将不起作用。

那么有没有办法让我的嵌入式 pdf 带有 src="{my base 64 string},然后将 PDFObject 包裹在它周围?如果没有,有没有办法通过 PDFObject 使用 base64字符串而不是 url?

此外,这是在 IE 11 中

更新 这是我的控制器

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\GA5\Daily\" + instrumentType + "\" + book + "\";
        //g_filePath = @"\10.15.100.225\sup_court\Imaging\GA5\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);
}

这是我的 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);

现在的问题是,它没有在#PDFViewer 中显示 PDF,而是尝试下载文件。有人可以帮助我解决难题的最后一部分。这让我发疯。

您是否尝试过仅使用标准 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 吗?

务必将内容配置 header 设置为 inline,否则浏览器将尝试下载文件而不是在视口中呈现它。

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

就 PDFObject 与普通 HTML 而言,为了进行故障排除,我始终建议尝试使用静态标记(无 JS)来显示相同​​的 PDF。如果它在那里工作,问题可能在于 PDFObject(在这种情况下,PDFObject 对 Base64 字符串的处理)。如果 PDF 没有通过纯标记正确呈现,问题可能出在您的 file/Base64 上。

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

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