Files
Electron.NET/ElectronNET.WebApp/Controllers/PdfController.cs

43 lines
1.4 KiB
C#
Raw Permalink Normal View History

using System.Linq;
using Microsoft.AspNetCore.Mvc;
using ElectronNET.API;
using ElectronNET.API.Entities;
namespace ElectronNET.WebApp.Controllers
{
public class PdfController : Controller
{
public IActionResult Index()
{
2017-10-23 21:24:05 +02:00
if (HybridSupport.IsElectronActive)
{
Electron.IpcMain.On("print-pdf", async (args) =>
{
2017-10-23 21:24:05 +02:00
BrowserWindow mainWindow = Electron.WindowManager.BrowserWindows.First();
var saveOptions = new SaveDialogOptions
{
2017-10-23 21:24:05 +02:00
Title = "Save an PDF-File",
DefaultPath = await Electron.App.GetPathAsync(PathName.Documents),
2017-10-23 21:24:05 +02:00
Filters = new FileFilter[]
{
new FileFilter { Name = "PDF", Extensions = new string[] { "pdf" } }
2017-10-23 21:24:05 +02:00
}
};
var path = await Electron.Dialog.ShowSaveDialogAsync(mainWindow, saveOptions);
2017-10-23 21:24:05 +02:00
if (await mainWindow.WebContents.PrintToPDFAsync(path))
{
await Electron.Shell.OpenExternalAsync("file://" + path);
}
else
{
Electron.Dialog.ShowErrorBox("Error", "Failed to create pdf file.");
}
});
}
return View();
}
}
}