【发布时间】:2015-01-13 16:18:30
【问题描述】:
有人有向打印机发送字节数组的经验吗?我将 pdf 文件直接发送到打印机的 IP 地址,它在某些打印机上打印,在其他打印机上打印空白页。这是打印机驱动问题吗?这是打印的代码:
public class PrintService : IPrintService
{
ManualResetEvent connectDone;
ManualResetEvent sendDone;
/// <inheritDoc />
public void PrintData(byte[] data, string printerName)
{
connectDone = new ManualResetEvent(false);
sendDone = new ManualResetEvent(false);
IPAddress ip = IPAddress.Parse(printerName);
IPEndPoint remoteEP = new IPEndPoint(ip, 9100);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.NoDelay = true;
try
{
client.BeginConnect(remoteEP, new AsyncCallback(connectCallback), client);
connectDone.WaitOne();
client.BeginSend(data, 0, data.Length, 0, new AsyncCallback(sendCallback), client);
sendDone.WaitOne();
}
finally
{
// Shutdown the client
this.shutDown(client);
}
}
private void connectCallback(IAsyncResult ar)
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete the connection.
client.EndConnect(ar);
// Signal that the connection has been made.
connectDone.Set();
}
private void sendCallback(IAsyncResult ar)
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = client.EndSend(ar);
// Signal that all bytes have been sent.
sendDone.Set();
}
private void shutDown(Socket client)
{
client.Shutdown(SocketShutdown.Both);
client.Close();
}
}
【问题讨论】:
-
我们有一个打印预览选项,当用户可以在 Web 浏览器中看到 PDF 并且可以选择所有打印选项时,它可以从那里很好地打印。我的替代方案是什么?
-
在这种情况下,可能是浏览器进行渲染,而不是打印机。您的替代方法是使用库或外部进程(ghostscript、acrobat 阅读器等)在应用程序中渲染 PDF 文件)。这个话题在 SO 之前已经讨论过很多次了。
-
我认为您对这台打印机的看法可能是正确的,因为我们在另一台打印机上进行了测试并且它工作正常。打印预览有效但原始数据无效,这似乎很奇怪。我正在使用
wkhtmltopdf.exe将 HTML 转换为 PDF。 -
我不明白为什么这对你来说很奇怪......正如我所说,打印预览可能是由打印应用程序创建的,你是否使用 chrome 来“测试”这个打印场景? Chrome 能够开箱即用地在屏幕上呈现 PDF 文件。
-
odd 不是正确的选择词哈哈,真的一点都不奇怪,你是绝对正确的。令人沮丧是正确的词。