Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to save PDFs from assets/www folder via XHR to Filesystem in Cordova?
    primarykey
    data
    text
    <p>I am trying to get a PDF displaying in Cordova iOS (and potentially later Android) using PDFjs. It seems that PDFjs needs the files served from a webserver in order to use its normal load function but it will accept a PDF encoded as a Uint8Array. </p> <p>I have attempted to use Cordova's <code>FileAPI</code> which has methods for loading to Uint8Array to load the PDF which can load from the <code>Documents</code> folder but from what I can tell the API appears not have the ability to access files in <code>www</code>. </p> <p>The PDF's need to be initially bundled with the Application and will need to be updatable through the app if they get out of sync so I would like to be able to have them as part of the source and then copy them to the Documents folder to be manipulable/over-writable. </p> <p>How do I copy Files from the <code>www</code> folder to the <code>Documents</code> folder? I imagine this is a common scenario but have had trouble finding examples of this online. </p> <p>Any help appreciated.</p> <p>Rickard</p> <h3>Update</h3> <p>So far I have been able to use file transfer to download files from a separate webserver:</p> <pre><code>var fileTransfer = new FileTransfer(); fileTransfer.download( 'http://localhost:8080/data/output8.pdf',// This is a webserver I am running _fileSystem.root.fullPath+'/output8.pdf', function(entry) { console.log("download complete: " + entry.fullPath); }, function(error) { console.log("download error source " + error.source); console.log("download error target " + error.target); console.log("upload error code" + error.code); } ); </code></pre> <p>So this is great if I wanted to download all the PDFs from a remote server but I need to download them from the <code>www</code> folder and I cant seem to find the path to do that. I have tried: <code>file:///data/output8.pdf</code> , <code>data/output8.pdf</code> and <code>/data/output8.pdf</code></p> <p>What is the correct way to reference these paths?</p> <h3>Update 2 - Dead End Cannot Save as Binary</h3> <p>So I am still stuck on this hours later. I have gotten PDFjs rendering correctly but only from the files I serve from a webserver then FileTransfer to the local filesystem but not the files I bundle in with the application. I managed to load the file from www to an ArrayBuffer using XHR but cannot save it. </p> <pre><code>var xhr = new XMLHttpRequest(); xhr.open('GET','data/output8.pdf',true); xhr.responseType = 'arraybuffer'; xhr.onload = function(e){ var itWorked=this.status==0&amp;&amp;(this.response instanceof ArrayBuffer); console.log(itWorked?'looks like it worked':'hmm there was an issue: '+this.status); var myArrayBuffer = this.response; theFileSystem.root.getFile('output8.pdf',{create:true},function(entry){ entry.createWriter(function(fileWriter){ fileWriter.onwrite = function(){ console.log('file has been written'); }; // Turns out the following does not work in // Cordova which cannot write binary to a file. var blob = new Uint8Array(myArrayBuffer); fileWriter.write(blob);//Doesnt work },fail); },fail); } xhr.ontimeout=function(e){ console.log('timeout'); } xhr.send(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload