Using pdf.js exception occur JavaScriptException: (ReferenceError): PDFJS is not defined

匿名 (未验证) 提交于 2019-12-03 09:05:37

问题:

    **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>                 &nbsp; &nbsp;                 <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> 

and in client side (pdf.java) call a java script funcion using JSNI alse paste pdf.js file in public folder and call .gwt.xml like

11:27:17.679 [ERROR] [pdf] Unable to load module entry point class com.ruchi.client.Pdf (see associated exception for details) com.google.gwt.core.client.JavaScriptException: (ReferenceError): PDFJS is not defined at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:248) at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136) at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561) at com.google.gwt.dev.shell.ModuleSpace.invokeNativeVoid(ModuleSpace.java:289) at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeVoid(JavaScriptHost.java:107) at 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) at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:396) at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:200) at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:525) at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:363) at java.lang.Thread.run(Unknown Source)

    plz suggest me what i do.... 

回答1:

You are just blindly copy pasting javascript code into JSNI block.

Example if in your html js block you have

function test() {   alert('hello');   } 

In your JSNI

private static native int test() /*-{     var v = $wnd.test();     return v;   }-*/; 

Do you notice the use of $wnd

1) HTML should have

    <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) Move pdf.js to local folder ( aovid script tag in html file requesting over internet )

3) Instead add <script src="PDF.js" /> in your module.gwt.xml file. Copy https://raw.github.com/mozilla/pdf.js/gh-pages/build/pdf.js into your GWT's public folder.

4) Modify your JSNI code

5) You should be able to solve Next/Prev similarly. Avoid raising one more question to solve that. You should try doing some "HOMEWork"



回答2:

JSNI 101: you have to use $wnd.PDFJS instead of PDFJS.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!