0

I'm trying to use PDFNet / PDFTron WebViewer within an Electron based application. The user should be able to select a file from the local filesystem to open. But when passing a file:// URL for initialDoc I always get the error Error retrieving file: file:///Users/mat/Desktop/document-generator/xod/Anchoring/Anchoring.xod?_=-22,. Received return status 0..

Has anyone an Idea how this could be solved? Copying the file in a folder close to index.html and using a relative URL is not an option, since the PDF documents are quite big.

Mato
  • 830
  • 2
  • 9
  • 21

1 Answers1

0

The initialDoc property does not seem to support the file:// URI scheme. However, you can open PDFs directly in the browser by using the HTML5's File API, and then using the (hidden?) loadLocalFile function:

<input type="file" id="files" name="files[]" multiple />
<output id="list"></output>

<script>
$(function() 
{
   var viewerElement = document.getElementById("viewer");
   window.myWebViewer = new PDFTron.WebViewer({
       documentId: "pdf",
       documentType: "pdf",
       path: "lib",
       type: "html5",
       initialDoc: "GettingStarted.pdf"
   }, viewerElement);
});

document.getElementById('files').addEventListener('change', handleFileSelect, false);
function handleFileSelect(e) 
{
  var files = e.target.files; // FileList object
  console.log("Loading file " + files[0].name);
  window.myWebViewer.getInstance().loadLocalFile(files[0], {});
}

</script>

Such a functionality is also demonstrated in the WebViewer online demo. Note that documentId and documentType must be set to "pdf" for this to work.

Tomer
  • 3,149
  • 23
  • 26
  • Thank you for the tip. But this only seems to work with the html5 viewer. When using the html5mobile viewer there is no method loadLocalFile and getInstance returns undefined. Is there maybe a way to also open XOD from the local file system and, more important, also in the html5mobile mode? – Mato Jun 14 '16 at 14:58