Node.js - download and save PDF file locally for offline - file damaged
<p>I currently have a desktop App built with Node Webkit. When the user is online, the App will open up PDF files from the server. I then save these files locally so they are available when offline.</p>

<p>I am using the Node.js File System's fs.writeFile(), to save the PDF locally but when trying to open it via the App the PDF is blank. When I try to open it direct from the folder I get the below error.</p>

<p><a href="https://i.stack.imgur.com/fOA3U.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fOA3U.png" alt="enter image description here"></a></p>

<p>Can anyone please advise?</p>

<pre><code>//save PDF file for offline
function saveFile(pdfvar) {
    var filename = 'test.pdf';
    fs.writeFile(filename, pdfvar);
}

//open PDF in new window
$('#stage').on('click', '#pdflink', function(event){
    var pdfvar = (this.getAttribute('data-pdffile'));
    window.open(pdfvar, "_blank");
    if(online===true){
        saveFile(pdfvar);
    }
});
</code></pre>