2

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.

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.

enter image description here

Can anyone please advise?

//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);
    }
});
LeeTee
  • 6,401
  • 16
  • 79
  • 139
  • is your pdf getting rendered in new window??And add the error message. – vkstack Aug 30 '16 at 11:05
  • sorry, I forgot to add the error! yes, it opens a new window but the PDF doesn't load at all its just blank – LeeTee Aug 30 '16 at 11:17
  • It is happening because `pdfvar` does not contain a valid pdf file – vkstack Aug 30 '16 at 11:39
  • it does contain a valid PDF file. It opens fine when online, but doesn't open when saved locally, so something is happening when being saved. – LeeTee Aug 30 '16 at 11:51
  • Upload your PDF somewhere online, if you can, and paste link here. We need to see the binary content of the file. – Legionar Aug 30 '16 at 13:25

1 Answers1

0
  1. Are you holding entire content of a PDF in a DOM element attribute this.getAttribute('data-pdffile') ? This is where it may get corrupted.

  2. When you call

    window.open(pdfvar, "_blank");
    

the content is not ready to be opened like that. It should be first converted to base64 encoded url.

window.open('data:application/pdf,' + escape(pdfvar)); 

UPDATE:

The actual problem was that the OP was trying to save the file using it's URL in "pdfvar"

fs.writeFile(filename, pdfvar);

but the file created was containing only a URL itself. What was required was downloading the file first using a http library:

Community
  • 1
  • 1
Pawel
  • 16,093
  • 5
  • 70
  • 73
  • 1.Im not holding entire content in the DOM. Just the URL to the PDF file. That must be where Im going wrong. How do I fix this? – LeeTee Aug 30 '16 at 15:16
  • 2. Now I get, "Failed to load PDF document" even when I am online. – LeeTee Aug 30 '16 at 15:18
  • @LeeTee so why do you pass URL to "saveFile" ? Then you will get fs.writeFile('test.pdf', 'http://address.com/file.pdf') The second parameter should be actual data not a URL. Your locally saved file is now a text file with just a URL inside... – Pawel Aug 30 '16 at 15:41
  • What is your URL? Is it an absolute address with "http : //domain.com/files/file.pdf" or is it relative i.e. "files/file.pdf"? It should be absolute – Pawel Aug 30 '16 at 16:24
  • yes its absolute. OK so how do I get the data from the PDF file online to write it locally? I couldnt find the info anywhere, hence why Ive done it wrong. I thought I could simply download the PDF file and save it – LeeTee Aug 30 '16 at 17:03
  • @LeeTee use google, this is the first result for "node download url" http://stackoverflow.com/questions/11944932/how-to-download-a-file-with-node-js-without-using-third-party-libraries – Pawel Aug 30 '16 at 17:50
  • Thanks, thats what I need. I didn't come across that, must have been searching with different keywords. – LeeTee Aug 30 '16 at 18:25