Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron Save Dialog Specify File Type

I have a save dialog in an electron app. At the moment, when a user clicks save it will save with the default name and file extension foo.pdf.

When the name is changed, the file extension isn't added.

Is there a way to make sure that the .pdf file extension is added to all file names?

document.getElementById("pdf-btn").onclick = function() {
  var webv = document.getElementById('appview');
  dialog.showSaveDialog({
    defaultPath: '~/foo.pdf'
  }, function(file_path) {
    if (file_path) {
      webv.printToPDF({}, function(err, data) {
        if (err) {
          dialog.showErrorBox('Error', err);
          return;
        }
        fs.writeFile(file_path, data, function(err) {
          if (err) {
            dialog.showErrorBox('Error', err);
            return;
          }

          // addext = file_path + ".pdf";
          //
          // save_pdf_path = addext;

          save_pdf_path = file_path;

          var message = "<p> Write PDF file: " + save_pdf_path + " successfully!</p>";

          console.log(message);

        });
      });
    }
  });
}
like image 209
Jason Avatar asked Mar 31 '17 08:03

Jason


1 Answers

By adding the following code I was able to specify the file extension.

dialog.showSaveDialog({
    filters: [{
      name: 'Adobe PDF',
      extensions: ['pdf']
    }]
  },
like image 76
Jason Avatar answered Oct 05 '22 07:10

Jason