Skip to content

added print capability #355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions ElectronNET.API/Entities/PrintOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
namespace ElectronNET.API.Entities
{
/// <summary>
/// Print dpi
/// </summary>
public class PrintDpi
{
/// <summary>
/// The horizontal dpi
/// </summary>
public float Horizontal { get; set; }

/// <summary>
/// The vertical dpi
/// </summary>
public float Vertical { get; set; }
}

/// <summary>
/// The page range to print
/// </summary>
public class PrintPageRange
{
/// <summary>
/// From
/// </summary>
public int From { get; set; }

/// <summary>
/// To
/// </summary>
public int To { get; set; }
}

/// <summary>
/// Print options
/// </summary>
public class PrintOptions
{
/// <summary>
/// Don't ask user for print settings
/// </summary>
public bool Silent { get; set; }

/// <summary>
/// Prints the background color and image of the web page
/// </summary>
public bool PrintBackground { get; set; }

/// <summary>
/// Set the printer device name to use
/// </summary>
public string DeviceName { get; set; }

/// <summary>
/// Set whether the printed web page will be in color or grayscale
/// </summary>
public bool Color { get; set; }

/// <summary>
/// Specifies the type of margins to use. Uses 0 for default margin, 1 for no
/// margin, and 2 for minimum margin.
/// </summary>
public int MarginsType { get; set; }

/// <summary>
/// true for landscape, false for portrait.
/// </summary>
public bool Landscape { get; set; }

/// <summary>
/// The scale factor of the web page
/// </summary>
public float ScaleFactor { get; set; }

/// <summary>
/// The number of pages to print per page sheet
/// </summary>
public int PagesPerSheet { get; set; }

/// <summary>
/// The number of copies of the web page to print
/// </summary>
public bool Copies { get; set; }

/// <summary>
/// Whether the web page should be collated
/// </summary>
public bool Collate { get; set; }

/// <summary>
/// The page range to print
/// </summary>
public PrintPageRange PageRanges { get; set; }

/// <summary>
/// Set the duplex mode of the printed web page. Can be simplex, shortEdge, or longEdge.
/// </summary>
public string DuplexMode { get; set; }

/// <summary>
/// Dpi
/// </summary>
public PrintDpi Dpi { get; set; }

}
}
29 changes: 29 additions & 0 deletions ElectronNET.API/Entities/PrinterInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace ElectronNET.API.Entities
{
/// <summary>
/// Printer info
/// </summary>
public class PrinterInfo
{
/// <summary>
/// Name
/// </summary>
public string Name { get; set; }

/// <summary>
/// Name
/// </summary>
public string Description { get; set; }

/// <summary>
/// Status
/// </summary>
public int Status { get; set; }

/// <summary>
/// Is default
/// </summary>
public bool IsDefault { get; set; }

}
}
47 changes: 47 additions & 0 deletions ElectronNET.API/WebContents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,53 @@ public void OpenDevTools(OpenDevToolsOptions openDevToolsOptions)
BridgeConnector.Socket.Emit("webContentsOpenDevTools", Id, JObject.FromObject(openDevToolsOptions, _jsonSerializer));
}

/// <summary>
/// Get system printers.
/// </summary>
/// <returns>printers</returns>
public Task<PrinterInfo[]> GetPrintersAsync()
{
var taskCompletionSource = new TaskCompletionSource<PrinterInfo[]>();

BridgeConnector.Socket.On("webContents-getPrinters-completed", (printers) =>
{
BridgeConnector.Socket.Off("webContents-getPrinters-completed");

taskCompletionSource.SetResult(((Newtonsoft.Json.Linq.JArray)printers).ToObject<PrinterInfo[]>());
});

BridgeConnector.Socket.Emit("webContents-getPrinters", Id);

return taskCompletionSource.Task;
}

/// <summary>
/// Prints window's web page.
/// </summary>
/// <param name="options"></param>
/// <returns>success</returns>
public Task<bool> PrintAsync(PrintOptions options = null)
{
var taskCompletionSource = new TaskCompletionSource<bool>();

BridgeConnector.Socket.On("webContents-print-completed", (success) =>
{
BridgeConnector.Socket.Off("webContents-print-completed");
taskCompletionSource.SetResult((bool)success);
});

if(options == null)
{
BridgeConnector.Socket.Emit("webContents-print", Id, "");
}
else
{
BridgeConnector.Socket.Emit("webContents-print", Id, JObject.FromObject(options, _jsonSerializer));
}

return taskCompletionSource.Task;
}

/// <summary>
/// Prints window's web page as PDF with Chromium's preview printing custom
/// settings.The landscape will be ignored if @page CSS at-rule is used in the web page.
Expand Down
31 changes: 19 additions & 12 deletions ElectronNET.Host/api/webContents.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading