javascript - 将外部 PDF/xod 文件加载到 PDFTron

标签 javascript html pdftron

我有 PDFTron 的演示项目,在我本地的相同项目位置也有 Booking.pdf。

我编写了以下代码将 PDF 加载到 PDFTron 中

<html>
<head>
  <script src="jquery-1.7.2.min.js"></script>
  <script src="lib/WebViewer.js"></script>

    <style>
        #viewer {
            width: 1024px;
            height: 600px;
        }
    </style>
 </head>
<body class="page-reader">
  <div id="viewer"></div>
</body>
<script>

    $(function() {
        var viewerElement = document.getElementById("viewer");
        var myWebViewer = new PDFTron.WebViewer({
            path: "lib",
            type: "html5",
            documentType: "pdf",
            initialDoc: "Booking.pdf"
        }, viewerElement);
    });

</script>
</html>

现在的问题是,当我尝试从本地目录加载 pdf/xod 文件时,它工作正常, 但假设我想从其他服务器获取 PDF 文件,例如:http://serverURL/Booking.pdf那时我将服务器路径包含在 initialDoc 中:“http://serverURL/Booking.pdf ” 然后它给我错误。

错误是网络错误或未找到。

如何将外部 pdf/xod 加载到 pdftron?

我引用了以下链接来解决问题,但我无法解决错误。

http://www.pdftron.com/webviewer/demo/documentation.html

http://www.pdftron.com/webviewer/demo/doc/WebViewer_Developer_Guide.pdf

请帮我解决这个问题。

最佳答案

InitialDoc 不必是绝对路径,它可以是一些标记,即 session 中的某些内容等。您可以读取并获取文件二进制文件。在我的例子中,asp.net Web 表单 ASMX 服务,我使用 GET 创建了一个 web 方法,所有 PDFViewere 所做的就是使用指定的 intialDoc 值生成 HTTP GET。你只需要这样捕捉:

[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = true)]
public string GetFile(string token)
{
    byte[] fileBinary = null;// Read file here from DB or Service.

    if (fileBinary != null)
    {
        var response = HttpContext.Current.Response;
        response.Clear();
        response.ContentType = "application/pdf";
        response.AddHeader("Content-Length", fileBinary.Length.ToString());
        response.OutputStream.Write(fileBinary, 0, fileBinary.Length);
        response.Flush();
        response.End();
    } 
    //We need to return this for no apparent reason (PDFViewer get action need that).
    return token;
}


 $(function () {
    var customData = { serviceUrl: 'services/PDFWebService.asmx', token: '<%=initialDoc.Value %>', isReadonly: '<%=IsReadonly?"yes":"no" %>' };
    var myWebViewer = new PDFTron.WebViewer({
        path: "Resources/js/PDFTron",
        mobileRedirect: false,
        stream: true,
        config: 'Resources/js/PDFViewerConfig.js',
        documentType: "pdf",
        custom: JSON.stringify(customData),
        l: '<%=LicenseKey%>',
        initialDoc: customData.serviceUrl + '/GetFile?token=' + customData.token
    }, document.getElementById('viewer'));
});

关于javascript - 将外部 PDF/xod 文件加载到 PDFTron,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34853745/

相关文章:

javascript - 是否可以更改 'annotationStyleEditButton'和 ' annotationDeleteButton'的webviewer按钮图标

javascript - pdftron webviewer - 如何使用 php 将整个编辑后的 ​​pdf 保存到服务器

jquery - Bootstrap : Proper way of linking JavaScript files in HTML layout

javascript - Javascript 中的作用域是什么?

javascript - 如何添加无限数量的文本框

javascript - input 的 event.target 在 this.setState [React.js] 中为 null

javascript - 尝试从表单输入 JS 生成变量时未捕获 RangeError 超出最大调用堆栈

php - YouTube嵌入:自动以1080p播放吗?

Angular:将方法作为 Angular 中的参数传递

javascript - 我的 JavaScript 格式有问题吗?