我有 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/