Electron Save Dialog Specify File Type
Asked Answered
F

1

8

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);

        });
      });
    }
  });
}
Fouquet answered 31/3, 2017 at 8:59 Comment(0)
F
15

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

dialog.showSaveDialog({
    filters: [{
      name: 'Adobe PDF',
      extensions: ['pdf']
    }]
  },
Fouquet answered 31/3, 2017 at 9:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.