1

Hey Related to your answer on Can I write files with HTML5/JS?

How would I modify this for say Excel or PDF. I tried but was unable to get it to work. I get a corrupt file downloaded when I change MIME to application/pdf

I am trying to link it to data stored in localstorage which I have all in a variable.

My current code is:

function setSaveFile(contents, file_name, mime_type) {

  var a = document.getElementById('save');
  mime_type = mime_type || 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; // text/html, image/png, et c
  if (file_name) a.setAttribute('Log.xls', file_name);
  a.href = 'data:'+ mime_type +';base64,'+ btoa(contents || 'Description' + '   ' + 'Notes' + ' ' + 'Date\n\n' + pdftimeLog);
}

I want to be able to export it as a CSV with each of those headings: DATE, description, notes in different cells. I want it mobile friendly.Thanks

Community
  • 1
  • 1
AK7
  • 33
  • 2
  • 8

3 Answers3

0

For a file to be a valid pdf or excelfile their contents need to conform to the standard that defines the respective format, just changing mimetypes won't do much good.

For PDF there's a library jspdf that might work for you.

I haven't seen a solution for Excel yet, it will be a whole lot harder as the (xls) file format is quite complicated, for the time being it's probably preferable to generate the file on the server.

fvu
  • 32,488
  • 6
  • 61
  • 79
  • 1
    If you only need to open it as a spread sheet, you could also produce a *.csv file, a comma seperated value file. – Sandro Meier May 09 '13 at 02:15
  • If I opt for CSV how would it work. Currently I am storing data in localstorage. I want to make it so that this data can be exported and emailed by users and I suppose csv will do. The software is intended for mobile use thus, downloadify is not an option. Thanks – AK7 May 09 '13 at 17:44
0

As an alternative to CSV, it's also possible to write an Excel file by writing HTML to a file with a .xls extension. Excel opens it up based on the extension. Haven't yet explored how deeply it can paginate, format.

prototype
  • 7,249
  • 15
  • 60
  • 94