2014-07-25 111 views
0

我創建使用PhoneGap的android.I都寫在創建PDF java.Code文件中的代碼創建的HTML字符串pdf文件一個示例應用程序,無法從phonegap android調用java方法?

public class PdfGenerator 
{ 
    private WebView mAppView; 
    private DroidGap mGap; 
    public PdfGenerator(DroidGap gap, WebView view) 
    { 
     mAppView = view; 
     mGap = gap; 
    } 

    public void generatePDF() 
    { 
     File root = Environment.getExternalStorageDirectory(); 

     File gpxfile = new File(root, "test.pdf"); 
     System.out.println("Path ::::"+gpxfile); 
     try{ 
     Document document = new Document(PageSize.LETTER); 
      PdfWriter.getInstance(document, new FileOutputStream(gpxfile)); 
      document.open(); 
      document.addAuthor("Real Gagnon"); 
      document.addCreator("Real's HowTo"); 
      document.addSubject("Thanks for your support"); 
      document.addCreationDate(); 
      document.addTitle("Please read this"); 

      HTMLWorker htmlWorker = new HTMLWorker(document); 
      String str = "<html><head></head><body>"+ 
      "<a href='http://www.rgagnon.com/howto.html'><b>Real's HowTo</b></a>" + 
      "<h1>Show your support</h1>" + 
      "<p>It DOES cost a lot to produce this site - in ISP storage and transfer fees, " + 
      "in personal hardware and software costs to set up test environments, and above all," + 
      "the huge amounts of time it takes for one person to design and write the actual content." + 
      "<p>If you feel that effort has been useful to you, perhaps you will consider giving something back?" + 
      "<p>Donate using PayPal® to [email protected]" + 
      "<p>Contributions via PayPal are accepted in any amount " + 
      "<P><br><table border='1'><tr><td>Java HowTo<tr>" + 
      "<td bgcolor='red'>Javascript HowTo<tr><td>Powerbuilder HowTo</table>" + 
      "</body></html>"; 
      htmlWorker.parse(new StringReader(str)); 
      document.close(); 
      System.out.println("Done"); 
      } 
     catch (Exception e) { 
      e.printStackTrace(); 
    } 
    } 
} 

我勉類代碼,

public class MainActivity extends DroidGap 
{ 
    PdfGenerator pdf; 
    public void onCreate(Bundle savedInstanceState) 
    { 

     super.onCreate(savedInstanceState);  
     super.init(); 

     pdf = new PdfGenerator(this, appView);  
     appView.addJavascriptInterface(pdf, "PdfGenerator"); 

     super.loadUrl("file:///android_asset/www/test.html"); 
    } 
} 

Javascript代碼,

<html> 
    <head> 
    <meta name="viewport" content="width=320; user-scalable=no" /> 
    <meta http-equiv="Content-type" content="text/html; charset=utf-8"> 

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script> 

    <script> 
    function pdf() 
    { 
    document.addEventListener("deviceReady", deviceReady, false); 
    } 

    function creatfile() 
    { 
    window.PdfGenerator.generatePDF(); 
    } 



function deviceReady() { 
    window.PdfGenerator.generatePDF(); 
} 

    </script> 

    </head> 

    <body> 
    <input type="submit" onclick="pdf()" value="IMEI" /> 
    </body> 

煥我從JavaScript調用我得到了以下錯誤generatePDF,

Uncaught TypeError: Cannot call method 'generatePDF' of undefined 

請幫幫我。

+0

我建議你在谷歌搜索。此外,我很確定這個問題已經得到解答。 這不是在js代碼中調用java函數的方法。 有一個函數調用cordova.exec。請看看文檔。 – Larta

+0

Ditto Larta。查看插件上的文檔:http://cordova.apache.org/docs/en/3.5.0/guide_hybrid_plugins_index.md.html#Plugin%20Development%20Guide –

+0

我只有谷歌的代碼。它實際上工作很好 –

回答

0

定義DroidGap類中的函數(不)|(不會)|(不應該)|(不應該)將該函數暴露給webview javascript環境,也不會將成員對象作爲DOM對象通過Window公開。

你想要什麼(如Raymond和Larta所示)是寫一個PDF插件。

Here is the documentation for writing a Cordova Plugin

一般情況下,你需要 1.本機實現 這完全取決於你的插件是書面的平臺。在機器人的情況下,你可以找到本地插件

public class PDFWriter extends CordovaPlugin { 

    @Override 
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { 
     if (action.equals("generatePDF")) { 
      // CALL YOUR GENERATE PDF CODE HERE 
      return true; 
     } 
     return false; 
    } 
} 
  1. JS實現 你可能看起來像 PDFWriter.js: cordova.exec(successCB, failCB, 「PDFWriter」, 「generatePDF」, args);

successCB - 是成功回調 failCB - 是失敗的回調 PDFWriter - 是您所呼叫 generatePDF本地插件實現 - 是您呼叫 本地插件功能args - 要傳遞給本機插件功能的參數數組

請注意, s是一個完整的例子,我省略了JAVA中的import語句以及必要的鉤子。使用plugman npm模塊爲您自動生成這些模塊。