title: Which Java-Datatype is required when calling JavaScript-Method
body: <p>I'm trying to call a JavaScript-Method from within my JavaFX-WebView via:<br>
  </p>
  
  <pre><code>JSObject win = (JSObject)getEngine().executeScript("window");
  win.call("showPDF", myPDFFile /*typeof java.io.File*/);
  </code></pre>
  
  <p>the result of this call is:</p>
  
  <blockquote>
    <p>Invalid parameter object: need either .data, .range or .url</p>
  </blockquote>
  
  <p>This is the JavaScript-Part (not by me), which throws the error:
  </p>
  
  <pre><code>var source;
  if (typeof src === 'string') {
     source = { url: src };
  } else if (isArrayBuffer(src)) {
    source = { data: src };
  } else if (src instanceof PDFDataRangeTransport) {
    source = { range: src };
  } else {
    if (typeof src !== 'object') {
       error('Invalid parameter in getDocument, need either Uint8Array, ' +
       'string or a parameter object');
    }
    if (!src.url &amp;&amp; !src.data &amp;&amp; !src.range) {
       error('Invalid parameter object: need either .data, .range or .url');
    }
  }
  </code></pre>
  
  <p>Implementation of <code>isArrayBuffer</code>:</p>
  
  <pre><code>function isArrayBuffer(v) {
     return typeof v === 'object' &amp;&amp; v !== null &amp;&amp; v.byteLength !== undefined;
  }
  </code></pre>
  
  <p>So my question is:<br>
  <strong>What type of (java) object could be used so that this call might work?</strong></p>
  
  <pre><code>win.call("showPDF", ???);
  </code></pre>
  
  <p><em><strong>EDIT 1:</strong></em><br>
  <strong>String</strong> cannot be used, because it will be treated as a URL.<br>
  I would like to commit something like a <strong>ByteArray</strong> (my concrete file), but using a <code>byte[]</code> (instead of <code>java.io.File</code>) causes the same error.
  <br><br>
  Here are some comments from the JS function above:
  <a href="https://github.com/mozilla/pdf.js/blob/master/src/display/api.js#L234" rel="nofollow">https://github.com/mozilla/pdf.js/blob/master/src/display/api.js#L234</a></p>
  
  <blockquote>
    <p>This is the main entry point for loading a PDF and interacting with it.<br>
    NOTE: If a URL is used to fetch the PDF data a standard XMLHttpRequest(XHR)<br>
    is used, which means it must follow the same origin rules that any XHR does<br>
    e.g. No cross domain requests without CORS.<br>
    <br>
    @param {string|TypedArray|DocumentInitParameters|PDFDataRangeTransport} src<br>
    Can be a url to where a PDF is located, a typed array (Uint8Array)<br>
    already populated with data or parameter object.</p>
  </blockquote>
  
  <p><strong>What Datatype i have to use (in JAVA), so it will be a (e.g) <em>TypedArray (Uint8Array)</em> in JAVASCRIPT?</strong></p>
  
  <p><em><strong>EDIT 2:</strong></em><br>
  I was trying Aarons suggestion:</p>
  
  <pre><code>String encoded = Base64.getEncoder().encodeToString(Files.readAllBytes(myPDFFile.toPath()));
  engine.executeScript("var src = _base64ToArrayBuffer('"+encoded+"'); showPDF(src);");
  </code></pre>
  
  <p>This causes a new problem:</p>
  
  <blockquote>
    <p>Error: Invalid PDF binary data: either typed array, string or array-like object is expected in the data property.</p>
  </blockquote>
  
  <p>This (new) error is thrown (few lines later) here: <a href="https://github.com/mozilla/pdf.js/blob/master/src/display/api.js#L291" rel="nofollow">https://github.com/mozilla/pdf.js/blob/master/src/display/api.js#L291</a></p>
  
  <p><code>console.log(_base64ToArrayBuffer(encoded))</code> returns: <code>[object ArrayBuffer]</code></p>
  
  <p><strong>Solution:</strong><br>
  I managed to make this work (with help of Aaron):</p>
  
  <p><strong>pdfViewer.html</strong><br></p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;html&gt;
  &lt;head&gt;
    &lt;script src="web/compatibility.js"&gt;&lt;/script&gt;
    &lt;script src="build/pdf.js"&gt;&lt;/script&gt;
    &lt;script&gt;
      function _base64ToBinaryString(base64) {
          var binary_string =  window.atob(base64);
          return binary_string;
      }
      function showPDF(pdfFile) {
          console.log('calling showPDF...');  
  
          'use strict';
  
          PDFJS.disableWorker = true; /* IMPORTANT TO DISABLE! */
          PDFJS.workerSrc = 'build/pdf.worker.js';
  
          console.log('TRYING TO GET DOCUMENT FROM BINARY DATA...');
          PDFJS.getDocument({data: pdfFile}).then(function(pdf) 
          {
              console.log('PDF LOADED.');
              console.log('TRYING TO GET PAGE 1...');
  
              pdf.getPage(1).then(function(page) {
              console.log('PAGE 1 LOADED.');
  
              var scale = 1.0;
              var viewport = page.getViewport(scale);
  
              var canvas = document.getElementById('the-canvas');
              var context = canvas.getContext('2d');
              canvas.height = viewport.height;
              canvas.width = viewport.width;
  
              var renderContext = {
                  canvasContext: context,
                  viewport: viewport
              };
              console.log('RENDERING PAGE 1...');
              page.render(renderContext);
              });
          });
      };
     &lt;/script&gt;
     &lt;/head&gt;
     &lt;body&gt;
        &lt;canvas id="the-canvas" style="border:1px solid black;"/&gt;
     &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>After loading the above HTML-Page into the WebView,
  following Java-Code is used to load a PDF-File into the Page:</p>
  
  <pre><code>String encoded = Base64.getEncoder().encodeToString(Files.readAllBytes(myPdfFile.toPath()));
  webEngine.executeScript("var src = _base64ToBinaryString('"+encoded+"'); showPDF(src);");
  </code></pre>