Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++Window
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;
}
 
 

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
Posted 28-Aug-12 23:25pm
Comments
Chris Meech at 29-Aug-12 11:02am
   
Just guessing, but it seems like you are sending the PDF directly to the printer and bypassing all printing operations. Some software, ie. Adobe or others, will still be required to render the PDF in order to print it.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Member 4462457 at 4-Apr-13 14:44pm
   
Friends I want to use WritePrinter API How do I specify the Location of Text to Printed...I mean is there any Equivalent of VB6 Functions Currentx and CurrentY Thanks Arun

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 910
1 OriginalGriff 523
2 Abhinav S 253
3 thatraja 245
4 Emre Ataseven 210
0 Sergey Alexandrovich Kryukov 8,282
1 OriginalGriff 4,750
2 Peter Leow 3,784
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 29 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid