**In html file add script tag**
<head>
<script type="text/javascript" src="https://raw.github.com/mozilla/pdf.js/gh-pages/build/pdf.js"></script>
</head>
<body>
<button id="prev" onclick="goPrevious()">Previous</button>
<button id="next" onclick="goNext()">Next</button>
<span>Page: <span id="page_num"></span>
<span id="page_count"></span></span>
</div>
<div>
<canvas id="the-canvas" width="700" style="border:1px solid black"></canvas>
</div>
</body>
并在客户端(pdf.java)使用JSNI调用java脚本函数,在公用文件夹中粘贴pdf.js文件并调用.gwt.xml
**I am just follow this link http://jsbin.com/pdfjs-prevnext-v2/1/edit**
public void onModuleLoad() {
alert1();
}
private native void alert1 ()
/*-{
var url = 'http://cdn.mozilla.net/pdfjs/tracemonkey.pdf';
PDFJS.disableWorker = true;
var pdfDoc = null,
pageNum = 1,
scale = 0.8,
canvas = document.getElementById('the-canvas'),
ctx = canvas.getContext('2d');
function renderPage(num) {
// Using promise to fetch the page
pdfDoc.getPage(num).then(function(page) {
var viewport = page.getViewport(canvas.width / page.getViewport(1.0).width);
canvas.height = viewport.height;
// Render PDF page into canvas context
var renderContext = {
canvasContext: ctx,
viewport: viewport
};
page.render(renderContext);
});
document.getElementById('page_num').textContent = pageNum;
document.getElementById('page_count').textContent = pdfDoc.numPages;
}
function goPrevious() {
if (pageNum <= 1) return;
pageNum--;
renderPage(pageNum);
}
function goNext() {
if (pageNum >= pdfDoc.numPages) return;
pageNum++;
renderPage(pageNum);
}
PDFJS.getDocument(url).then(function getPdfHelloWorld(_pdfDoc) {
pdfDoc = _pdfDoc;
renderPage(pageNum);
});
}-*/;
11:27:17.679 [错误] [pdf]无法加载模块入口点类com.ruchi.client.Pdf(有关详细信息,请参阅相关异常) com.google.gwt.core.client.JavaScriptException :( ReferenceError):未定义PDFJS 在com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:248) 在com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136) 在com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561) 在com.google.gwt.dev.shell.ModuleSpace.invokeNativeVoid(ModuleSpace.java:289) 在com.google.gwt.dev.shell.JavaScriptHost.invokeNativeVoid(JavaScriptHost.java:107) 在com.ruchi.client.Pdf.alert1(Pdf.java) at com.ruchi.client.Pdf.onModuleLoad(Pdf.java:27) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) 在com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:396) 在com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:200) 在com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:525) 在com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:363) 在java.lang.Thread.run(未知来源)
plz suggest me what i do....
答案 0 :(得分:3)
你只是盲目地将javascript代码粘贴到JSNI块中。
示例是否在您的html js块中有
function test() {
alert('hello');
}
在您的JSNI中
private static native int test() /*-{
var v = $wnd.test();
return v;
}-*/;
您是否注意到使用 $ wnd
1)HTML应该有
<span>Page: <span id="page_num"></span> of <span id="page_count"></span></span>
<div>
<canvas id="the-canvas" width="700" style="border: 1px solid black"></canvas>
</div>
2)将pdf.js移动到本地文件夹(通过互联网请求的html文件中的aovid脚本标记)
3)而是在module.gwt.xml文件中添加<script src="PDF.js" />
。将https://raw.github.com/mozilla/pdf.js/gh-pages/build/pdf.js复制到GWT的公开文件夹中。
4)修改您的JSNI代码
/*-{
var url = 'http://cdn.mozilla.net/pdfjs/tracemonkey.pdf';
$wnd.PDFJS.disableWorker = true;
var pdfDoc = null,
pageNum = 1,
scale = 0.8,
canvas = $wnd.document.getElementById('the-canvas'),
ctx = canvas.getContext('2d');
function renderPage(num) {
// Using promise to fetch the page
pdfDoc.getPage(num).then(function(page) {
var viewport = page.getViewport(canvas.width / page.getViewport(1.0).width);
canvas.height = viewport.height;
// Render PDF page into canvas context
var renderContext = {
canvasContext: ctx,
viewport: viewport
};
page.render(renderContext);
});
$wnd.document.getElementById('page_num').textContent = pageNum;
$wnd.document.getElementById('page_count').textContent = pdfDoc.numPages;
}
function goPrevious() {
if (pageNum <= 1) return;
pageNum--;
renderPage(pageNum);
}
function goNext() {
if (pageNum >= pdfDoc.numPages) return;
pageNum++;
renderPage(pageNum);
}
$wnd.PDFJS.getDocument(url).then(function getPdfHelloWorld(_pdfDoc) {
pdfDoc = _pdfDoc;
renderPage(pageNum);
});
}-*/;
5)你应该能够类似地解决Next / Prev。避免再提出一个问题来解决这个问题。你应该尝试做一些“HOMEWork”
答案 1 :(得分:1)
JSNI 101:您必须使用$wnd.PDFJS
代替PDFJS
。