Linux printing should work perfectly now.
To print first install printer with cups then give local printername as printer (not the URI).
This commit is contained in:
@ -5,43 +5,86 @@ namespace Tanshu.Accounts.Print
|
|||||||
{
|
{
|
||||||
internal class PrinterLinux : PlatformPrinter
|
internal class PrinterLinux : PlatformPrinter
|
||||||
{
|
{
|
||||||
#region API Declarations
|
|
||||||
// ReSharper disable InconsistentNaming
|
// ReSharper disable InconsistentNaming
|
||||||
[DllImport("libcups", CharSet = CharSet.Ansi)]
|
#region API Declarations
|
||||||
static extern int cupsPrintFile(string printer, string filename, string title, int num_options, IntPtr options);
|
enum http_status_t /**** HTTP status codes ****/
|
||||||
// int cupsPrintFile (const char *name, const char *filename, const char *title, int num_options, cups_option_t *options);
|
{
|
||||||
|
HTTP_ERROR = -1, /* An error response from httpXxxx() */
|
||||||
|
HTTP_CONTINUE = 100, /* Everything OK, keep going... */
|
||||||
|
HTTP_SWITCHING_PROTOCOLS, /* HTTP upgrade to TLS/SSL */
|
||||||
|
HTTP_OK = 200, /* OPTIONS/GET/HEAD/POST/TRACE command was successful */
|
||||||
|
HTTP_CREATED, /* PUT command was successful */
|
||||||
|
HTTP_ACCEPTED, /* DELETE command was successful */
|
||||||
|
HTTP_NOT_AUTHORITATIVE, /* Information isn't authoritative */
|
||||||
|
HTTP_NO_CONTENT, /* Successful command, no new data */
|
||||||
|
HTTP_RESET_CONTENT, /* Content was reset/recreated */
|
||||||
|
HTTP_PARTIAL_CONTENT, /* Only a partial file was recieved/sent */
|
||||||
|
HTTP_MULTIPLE_CHOICES = 300, /* Multiple files match request */
|
||||||
|
HTTP_MOVED_PERMANENTLY, /* Document has moved permanently */
|
||||||
|
HTTP_MOVED_TEMPORARILY, /* Document has moved temporarily */
|
||||||
|
HTTP_SEE_OTHER, /* See this other link... */
|
||||||
|
HTTP_NOT_MODIFIED, /* File not modified */
|
||||||
|
HTTP_USE_PROXY, /* Must use a proxy to access this URI */
|
||||||
|
HTTP_BAD_REQUEST = 400, /* Bad request */
|
||||||
|
HTTP_UNAUTHORIZED, /* Unauthorized to access host */
|
||||||
|
HTTP_PAYMENT_REQUIRED, /* Payment required */
|
||||||
|
HTTP_FORBIDDEN, /* Forbidden to access this URI */
|
||||||
|
HTTP_NOT_FOUND, /* URI was not found */
|
||||||
|
HTTP_METHOD_NOT_ALLOWED, /* Method is not allowed */
|
||||||
|
HTTP_NOT_ACCEPTABLE, /* Not Acceptable */
|
||||||
|
HTTP_PROXY_AUTHENTICATION, /* Proxy Authentication is Required */
|
||||||
|
HTTP_REQUEST_TIMEOUT, /* Request timed out */
|
||||||
|
HTTP_CONFLICT, /* Request is self-conflicting */
|
||||||
|
HTTP_GONE, /* Server has gone away */
|
||||||
|
HTTP_LENGTH_REQUIRED, /* A content length or encoding is required */
|
||||||
|
HTTP_PRECONDITION, /* Precondition failed */
|
||||||
|
HTTP_REQUEST_TOO_LARGE, /* Request entity too large */
|
||||||
|
HTTP_URI_TOO_LONG, /* URI too long */
|
||||||
|
HTTP_UNSUPPORTED_MEDIATYPE, /* The requested media type is unsupported */
|
||||||
|
HTTP_REQUESTED_RANGE, /* The requested range is not satisfiable */
|
||||||
|
HTTP_EXPECTATION_FAILED, /* The expectation given in an Expect header field was not met */
|
||||||
|
HTTP_UPGRADE_REQUIRED = 426, /* Upgrade to SSL/TLS required */
|
||||||
|
HTTP_SERVER_ERROR = 500, /* Internal server error */
|
||||||
|
HTTP_NOT_IMPLEMENTED, /* Feature not implemented */
|
||||||
|
HTTP_BAD_GATEWAY, /* Bad gateway */
|
||||||
|
HTTP_SERVICE_UNAVAILABLE, /* Service is unavailable */
|
||||||
|
HTTP_GATEWAY_TIMEOUT, /* Gateway connection timed out */
|
||||||
|
HTTP_NOT_SUPPORTED, /* HTTP version not supported */
|
||||||
|
HTTP_AUTHORIZATION_CANCELED = 1000 /* User canceled authorization */
|
||||||
|
} ;
|
||||||
|
|
||||||
[DllImport("libcups", CharSet = CharSet.Ansi)]
|
[DllImport("libcups", CharSet = CharSet.Ansi)]
|
||||||
static extern int cupsCreateJob(string http, string name, string title, int num_options, IntPtr options);
|
static extern int cupsCreateJob(IntPtr 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);
|
// int cupsCreateJob (http_t *http, const char *name, const char *title, int num_options, cups_option_t *options);
|
||||||
|
|
||||||
[DllImport("libcups", CharSet = CharSet.Ansi)]
|
[DllImport("libcups", CharSet = CharSet.Ansi)]
|
||||||
static extern int cupsStartDocument(string http, string name, int job_id, string docname, string format, int last_document);
|
static extern int cupsStartDocument(IntPtr 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);
|
// 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)]
|
[DllImport("libcups", CharSet = CharSet.Ansi)]
|
||||||
static extern int cupsWriteRequestData(string http, string buffer, int length);
|
static extern int cupsWriteRequestData(IntPtr http, string buffer, int length);
|
||||||
// http_status_t cupsWriteRequestData (http_t *http, const char *buffer, size_t length);
|
// http_status_t cupsWriteRequestData (http_t *http, const char *buffer, size_t length);
|
||||||
|
|
||||||
[DllImport("libcups", CharSet = CharSet.Ansi)]
|
[DllImport("libcups", CharSet = CharSet.Ansi)]
|
||||||
static extern int cupsFinishDocument(string http, string name);
|
static extern int cupsFinishDocument(IntPtr http, string name);
|
||||||
// ipp_status_t cupsFinishDocument (http_t *http, const char *name);
|
// ipp_status_t cupsFinishDocument (http_t *http, const char *name);
|
||||||
// ReSharper restore InconsistentNaming
|
|
||||||
|
const string CUPS_FORMAT_TEXT = "text/plain";
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
internal override bool Print(string printerName, string documentName, string printingText)
|
internal override bool Print(string printerName, string documentName, string printingText)
|
||||||
{
|
{
|
||||||
var cupsOption = new IntPtr(0);
|
var cupsOption = new IntPtr(0);
|
||||||
const string CUPS_HTTP_DEFAULT = "CUPS_HTTP_DEFAULT";
|
var CUPS_HTTP_DEFAULT = new IntPtr(0);
|
||||||
var jobId = cupsCreateJob(CUPS_HTTP_DEFAULT, printerName, documentName, 0, cupsOption);
|
var jobId = cupsCreateJob(CUPS_HTTP_DEFAULT, printerName, documentName, 0, cupsOption);
|
||||||
if (jobId > 0)
|
if (jobId > 0)
|
||||||
{
|
{
|
||||||
const string format = "CUPS_FORMAT_TEXT";
|
cupsStartDocument(CUPS_HTTP_DEFAULT, printerName, jobId, documentName, CUPS_FORMAT_TEXT, 1);
|
||||||
cupsStartDocument(CUPS_HTTP_DEFAULT, printerName, jobId, documentName, format, 1);
|
|
||||||
cupsWriteRequestData(CUPS_HTTP_DEFAULT, printingText, printingText.Length);
|
cupsWriteRequestData(CUPS_HTTP_DEFAULT, printingText, printingText.Length);
|
||||||
cupsFinishDocument(CUPS_HTTP_DEFAULT, printerName);
|
cupsFinishDocument(CUPS_HTTP_DEFAULT, printerName);
|
||||||
}
|
}
|
||||||
return jobId > 0;
|
return jobId > 0;
|
||||||
}
|
}
|
||||||
|
// ReSharper restore InconsistentNaming
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
|
|
||||||
namespace Tanshu.Accounts.Print
|
namespace Tanshu.Accounts.Print
|
||||||
{
|
{
|
||||||
@ -16,15 +15,7 @@ namespace Tanshu.Accounts.Print
|
|||||||
internal static bool RunningOnLinux()
|
internal static bool RunningOnLinux()
|
||||||
{
|
{
|
||||||
var platform = (int)Environment.OSVersion.Platform;
|
var platform = (int)Environment.OSVersion.Platform;
|
||||||
if ((platform == 4) || (platform == 6) || (platform == 128))
|
return (platform == 4) || (platform == 6) || (platform == 128);
|
||||||
{
|
|
||||||
if (Environment.GetEnvironmentVariable("not_supported_MONO_MWF_USE_NEW_X11_BACKEND") != null ||
|
|
||||||
Environment.GetEnvironmentVariable("MONO_MWF_MAC_FORCE_X11") != null)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user