将原始数据发送到打印机时出现问题
嘿伙计们,
我正在尝试将pdf文件发送到我公司的多功能kyocera网络打印机(kyocera taskalfa 400ci)。打印机不通过网络共享,但出于安全原因,它作为本地打印机(kyocera打印机驱动程序,protocoll是raw,gdi enable)包含在内。我可以自己打印文件而不会有任何问题。
现在我想打印自动生成的pdf文件。我尝试了不同的解决方案,例如通过采用读卡器打印,但我不想使用其他软件。
我发现这个例子直接将数据发送到gdi打印机:
http:// msdn .microsoft.com / zh-CN / library / windows / desktop / dd162959%28v = vs.85%29.aspx [ ^ ]
我用过这段代码:
Hey guys,
im am trying do send a pdf file to a multi-functional kyocera network printer (kyocera taskalfa 400ci) in my company. The printer is not shared over the network, but it is included as a local printer (kyocera printer driver, protocoll is raw, gdi enable) for security reasons. I can print files without any problems on my own.
Now I want to print automatically generated pdf files. I tried different solution for example like printing via adopbe reader but i dont want to use other software.
I found this example to send data directly to a gdi printer:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd162959%28v=vs.85%29.aspx[^]
I used this code:
BOOL RawDataToPrinter(_WSTRING szPrinterName, LPBYTE lpData, DWORD dwCount) {
BOOL bStatus = FALSE;
HANDLE hPrinter = NULL;
DOC_INFO_1 DocInfo;
DWORD dwJob = 0L;
DWORD dwBytesWritten = 0L;
// Open a handle to the printer.
bStatus = OpenPrinter( &szPrinterName[0], &hPrinter, NULL );
if (bStatus) {
// Fill in the structure with info about this "document."
DocInfo.pDocName = (LPTSTR)_T("test");
DocInfo.pOutputFile = NULL;
DocInfo.pDatatype = (LPTSTR)_T("RAW");
// Inform the spooler the document is beginning.
dwJob = StartDocPrinter( hPrinter, 1, (LPBYTE)&DocInfo );
if (dwJob > 0) {
// Start a page.
bStatus = StartPagePrinter( hPrinter );
if (bStatus) {
// Send the data to the printer.
bStatus = WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten);
EndPagePrinter (hPrinter);
}
// Inform the spooler that the document is ending.
EndDocPrinter( hPrinter );
}
// Close the printer handle.
ClosePrinter( hPrinter );
}
// Check to see if correct number of bytes were written.
if (!bStatus || (dwBytesWritten != dwCount)) {
bStatus = FALSE;
} else {
bStatus = TRUE;
}
return bStatus;
}
long getFileSize(FILE *file) {
long lCurPos, lEndPos;
lCurPos = ftell(file);
fseek(file, 0, 2);
lEndPos = ftell(file);
fseek(file, lCurPos, 0);
return lEndPos;
}
bool sendFile(_WSTRING szFileName, _WSTRING szPrinterName) {
BYTE *fileBuf; // Pointer to our buffered data
FILE *file = NULL; // File pointer
// Open the file in binary mode using the "rb" format string
// This also checks if the file exists and/or can be opened for reading correctly
if ((file = _wfopen(szFileName.c_str(), L"rb")) == NULL)
printf("Could not open specified file \n");
else
printf("File opened successfully \n");
// Get the size of the file in bytes
long fileSize = getFileSize(file);
// Allocate space in the buffer for the whole file
fileBuf = new BYTE[fileSize];
// Read the file in to the buffer
fread(fileBuf, fileSize, 1, file);
if(RawDataToPrinter(szPrinterName, fileBuf, fileSize)) {
wprintf(L"print successfull [%s] \n", szFileName.c_str());
} else {
printf("print not successfull \n");
};
delete[]fileBuf;
fclose(file);
return 0;
}
每次打印作业后,我都会打印成功。打印机似乎找不到,它也可以发送数据(bStatus&&(dwBytesWritten == dwCount - > TRUE),但不会打印任何内容。
任何我应该找到错误的想法?
感谢你的帮助
祝你好运
After every print job i get "print successfull". The printer seems to be find and it can also send data ( bStatus && (dwBytesWritten == dwCount --> TRUE), but nothing will be printed.
Any ideas where i should find the error?
Thanks for your help
Best regards
正如Chris Meech在评论中所说(他猜),我不猜,你发送pdf文件内容,我可以在你的代码中看到。打印机不知道如何处理pdf文件。要么你必须解析并创建可打印的信息,要么正如Chris Meech所说,你将使用任何可以执行它的软件。
创建pdf文件后,您可以使用默认的pdf阅读器打开并打印您的数据。
As Chris Meech said in the comment(he guessed), I am not guessing, you send pdf file content, I can see in your code. Printer does not know how to process a pdf file. Either you will have to parse and create the printable information or as Chris Meech says, you will have use any software that can perform it.
after creating the pdf file you can open your with the default pdf reader and print your data.
您使用rawprinterhelper类将数据或文档发送到您的打印机
参考这个
如何使用V将原始数据发送到打印机isual C#.NET
you use rawprinterhelper class to send data or document to your printer
refer this
How to send raw data to a printer by using Visual C# .NET