【问题标题】:Add PDFObject from already created pdf element从已创建的 pdf 元素中添加 PDFObject
【发布时间】:2017-01-08 11:18:53
【问题描述】:

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

那么有没有办法让我的嵌入式 pdf 带有 src="{my base 64 string},然后将 PDFObject 包裹起来?如果没有,有没有办法通过 PDFObject 来使用 base64 字符串一个网址?

另外,这是在 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\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);
}

这是我的 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,而是尝试下载文件。有人可以帮助我完成拼图的最后一块。这让我发疯了。

【问题讨论】:

  • 您没有指定有问题的浏览器。根据您使用的堆栈,它可能是 IE,并且它对 base64 编码的 URL 大小有限制——大多数 PDF 都不符合这个限制。
  • 我可以显示 PDF。问题是,我真的需要 pdf.js 的功能,所以我试图让它加载到 pdf.js 中
  • 不确定这是否有帮助...stackoverflow.com/questions/6439634/… 是否需要将“ActionResult”指定为“FileStreamResult”返回类型?
  • 有趣的是,PDFObject 提供的唯一示例与在 .embed 函数的 scr 中指定的实际 .pdf 文件有关...不是 FileStreamResult...实际上是否可以使用 PDFObject 来指定src 中的 FileStream?

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


【解决方案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?

【讨论】:

  • 这不仅是因为我也必须使用 PDF.js,否则我会很遗憾:\
【解决方案2】:

确保将内容处置标头设置为inline,否则浏览器将尝试下载文件而不是在视口中呈现它。

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 2013-12-12
    • 2017-04-07
    • 2015-01-24
    • 2018-08-24
    相关资源
    最近更新 更多