narsil/Tanshu.Accounts.Print/PrinterLinux.cs

48 lines
2.4 KiB
C#
Raw Normal View History

using System;
using System.Runtime.InteropServices;
namespace Tanshu.Accounts.Print
{
internal class PrinterLinux : PlatformPrinter
{
#region API Declarations
// ReSharper disable InconsistentNaming
[DllImport("libcups", CharSet = CharSet.Ansi)]
static extern int cupsPrintFile(string printer, string filename, string title, int num_options, IntPtr options);
// int cupsPrintFile (const char *name, const char *filename, const char *title, int num_options, cups_option_t *options);
[DllImport("libcups", CharSet = CharSet.Ansi)]
static extern int cupsCreateJob(string http, string name, string title, int num_options, IntPtr options);
// int cupsCreateJob (http_t *http, const char *name, const char *title, int num_options, cups_option_t *options);
[DllImport("libcups", CharSet = CharSet.Ansi)]
static extern int cupsStartDocument(string http, string name, int job_id, string docname, string format, int last_document);
// http_status_t cupsStartDocument (http_t *http, const char *name, int job_id, const char *docname, const char *format, int last_document);
[DllImport("libcups", CharSet = CharSet.Ansi)]
static extern int cupsWriteRequestData(string http, string buffer, int length);
// http_status_t cupsWriteRequestData (http_t *http, const char *buffer, size_t length);
[DllImport("libcups", CharSet = CharSet.Ansi)]
static extern int cupsFinishDocument(string http, string name);
// ipp_status_t cupsFinishDocument (http_t *http, const char *name);
// ReSharper restore InconsistentNaming
#endregion
internal override bool Print(string printerName, string documentName, string printingText)
{
var cupsOption = new IntPtr(0);
const string CUPS_HTTP_DEFAULT = "CUPS_HTTP_DEFAULT";
var jobId = cupsCreateJob(CUPS_HTTP_DEFAULT, printerName, documentName, 0, cupsOption);
if (jobId > 0)
{
const string format = "CUPS_FORMAT_TEXT";
cupsStartDocument(CUPS_HTTP_DEFAULT, printerName, jobId, documentName, format, 1);
cupsWriteRequestData(CUPS_HTTP_DEFAULT, printingText, printingText.Length);
cupsFinishDocument(CUPS_HTTP_DEFAULT, printerName);
}
return jobId > 0;
}
}
}