/
PostScriptPrinter.cs
165 lines (142 loc) · 6.8 KB
/
PostScriptPrinter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Copyright 2011 Corey Bonnell and Sandro Lange
This file is part of Touch2PcPrinter for Windows.
Touch2PcPrinter for Windows is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Touch2PcPrinter for Windows is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Touch2PcPrinter for Windows. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using System.Text;
namespace Touch2PcPrinter
{
internal class PostScriptPrinter : IPrintServerComponent
{
private static readonly string[] PROGRAM_ARGUMENTS = new string[] { "gswin32c.exe", "-q", "-dQUIET", "-dBATCH", "-dNOPAUSE", "{0}", "{0}" };
private const int STATIC_ARGS_COUNT = 5;
private const string OUTPUT_PRINTER_TOKEN = "!DESTINATION_PRINTER_NAME!";
private const string OUTPUT_PLEX_MODE_TOKEN = "!JOB_PLEX_MODE!";
private readonly Action<string> logger;
private readonly string programFolder;
private readonly CancellationToken cancelToken;
public PostScriptPrinter(Action<string> logger, string programFolder, CancellationToken cancelToken)
{
if (logger == null)
{
throw new ArgumentNullException("logger");
}
if (programFolder == null)
{
throw new ArgumentNullException("programFolder");
}
this.logger = logger;
this.programFolder = programFolder;
this.cancelToken = cancelToken;
}
public void Start() { }
public void Stop() { }
public void Dispose() { }
private string writeSetupFile(string outputPrinter, PlexMode plexMode)
{
if (outputPrinter == null)
{
throw new ArgumentNullException("printerName");
}
string fileContent = Encoding.UTF8.GetString(Properties.Resources.setupPostScript);
fileContent = fileContent.Replace(PostScriptPrinter.OUTPUT_PRINTER_TOKEN, outputPrinter);
fileContent = fileContent.Replace(PostScriptPrinter.OUTPUT_PLEX_MODE_TOKEN, plexMode == PlexMode.Duplex ? "true" : "false");
string path = Path.GetTempFileName();
File.WriteAllText(path, fileContent);
this.logger.Invoke(String.Format("Wrote job setup file to \"{0}\"", path));
return path;
}
private string[] createArgs(string psFilePath, string outputPrinter, string setupFilePath)
{
if (psFilePath == null)
{
throw new ArgumentNullException("psFilePath");
}
if (outputPrinter == null)
{
throw new ArgumentNullException("printerName");
}
if (setupFilePath == null)
{
throw new ArgumentNullException("setupFilePath");
}
var args = new string[PostScriptPrinter.PROGRAM_ARGUMENTS.Length];
Array.Copy(PostScriptPrinter.PROGRAM_ARGUMENTS, args, PostScriptPrinter.STATIC_ARGS_COUNT);
args[PostScriptPrinter.STATIC_ARGS_COUNT] = String.Format(PostScriptPrinter.PROGRAM_ARGUMENTS[PostScriptPrinter.STATIC_ARGS_COUNT], setupFilePath);
args[PostScriptPrinter.STATIC_ARGS_COUNT + 1] = String.Format(PostScriptPrinter.PROGRAM_ARGUMENTS[PostScriptPrinter.STATIC_ARGS_COUNT + 1], psFilePath);
return args;
}
public void Print(string psFilePath, string outputPrinter, PlexMode plexMode)
{
if (psFilePath == null)
{
throw new ArgumentNullException("psFilePath");
}
if (outputPrinter == null)
{
throw new ArgumentNullException("printerName");
}
string setupFilePath = null;
try // try/finally block for setup file
{
setupFilePath = this.writeSetupFile(outputPrinter, plexMode);
var args = this.createArgs(psFilePath, outputPrinter, setupFilePath);
var hInstance = IntPtr.Zero;
try // try/finally block for GhostScript handle
{
int errorCode = NativeMethods.gsapi_new_instance(ref hInstance, IntPtr.Zero);
if (errorCode < 0)
{
throw new Exception(String.Format("gsapi_new_instance returned error code {0}", errorCode));
}
errorCode = NativeMethods.gsapi_init_with_args(hInstance, args.Length, args);
if (errorCode < 0)
{
throw new Exception(String.Format("gsapi_init_with_args returned error code {0}", errorCode));
}
NativeMethods.gsapi_exit(hInstance);
this.logger.Invoke("GhostScript completed processing successfully");
}
finally
{
if (hInstance != null)
{
NativeMethods.gsapi_delete_instance(hInstance);
}
}
}
finally
{
if (setupFilePath != null)
{
File.Delete(setupFilePath);
this.logger.Invoke("Deleted job setup file");
}
}
}
private static class NativeMethods
{
[DllImport("gsdll32", CharSet = CharSet.Ansi, ExactSpelling = true, EntryPoint = "gsapi_new_instance")]
public static extern int gsapi_new_instance(ref IntPtr hInstance, IntPtr callingHandle);
[DllImport("gsdll32", CharSet = CharSet.Ansi, ExactSpelling = true, EntryPoint = "gsapi_delete_instance")]
public static extern int gsapi_delete_instance(IntPtr hInstance);
[DllImport("gsdll32", CharSet = CharSet.Ansi, ExactSpelling = true, EntryPoint = "gsapi_init_with_args")]
public static extern int gsapi_init_with_args(IntPtr hInstance, int argc, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1, ArraySubType = UnmanagedType.LPStr), In] string[] argv);
[DllImport("gsdll32", CharSet = CharSet.Ansi, ExactSpelling = true, EntryPoint = "gsapi_exit")]
public static extern int gsapi_exit(IntPtr hInstance);
}
}
}