Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silent Printing with TideSDK

UPDATE: I've decided to use appjs for my project, rather than TideSDK. With AppJS, you are able to easily make node modules, in which you can add C++ modules for easy use of silent printing. I'd recommend anyone interested in the topic to check it out. Best of luck to you all!

I'm developing an application with TideSDK - It's a really awesome framework, if you haven't tried it, set some time aside!

Anyway, I'm attempting to print using Javascript, but a Print Settings dialog comes up each time, as it would with other browsers. I'm trying to eliminate that box as well as pass along the printer I would like it to print with.

I'm aware that there are settings in FireFox for silent printing, that is was I used previously, but there aren't any similar options for TideSDK (That I have found).

After a ton of research, I think the only viable options consist of a python script, a C++ module or editing the TideSDK source and recompiling. All three sound like loosers to me. Modifying the TideSDK source and removing the dialog box from webkit_ui_delegate.cpp will most likely result in some issues when updating this program down the road - in addition to not being able to pass the printer name easily. The python script would require third party modules and would have to process the contents of the page, I doubt I would be able to write something that could do a quality job in that respect. Finally, I think the C++ solution is the most feasible since it has access to win32api, but again, it would have to process HTML to a print language, something i'm not familiar with. I guess i'm looking for more of a webkit solution that handles the rendering for me.

What should I do to implement silent printing on TideSDK? Please include code examples.

I'm looking for a windows solution primarily, I've already figured out the other platforms.

Thank you for your time and I appreciate the feedback!

like image 797
Jonathan Avatar asked Jan 10 '13 23:01

Jonathan


1 Answers

NOTE: this is a rewrite of an earlier question.

The way Windows/GDI+ printing works is that it uses a PrintDialog to obtain a printer identifier (and to also let the user know what printer they are printing to). So in order to have silent printing, you need to obtain a printer identifier without showing a dialog.

So REMOVE this code:

// Open a printing dialog to fetch the HDC of the desired printer.
PRINTDLG dialog;
ZeroMemory(&dialog, sizeof(PRINTDLG));
dialog.lStructSize = sizeof(PRINTDLG);
dialog.Flags = PD_PRINTSETUP | PD_RETURNDC;
BOOL dialogResult = ::PrintDlg(&dialog);

if (!dialogResult) // Error or cancel.
{
    DWORD reason = CommDlgExtendedError();
    if (!reason) // User cancelled.
        return S_OK;

    logger->Error("Could not print page, dialog error code: %i",
        reason);
    return E_FAIL;
}

HDC hdc = dialog.hDC;

And then replace the last line (HDC hdc = ...) with some other way of getting a printer HDC. You could use GetDefaultPrinter() to get the name of the default printer, and then get a HDC using CreateDC().

Optionally, you could select a printer or create a custom print dialog using the EnumPrinters() function.

like image 138
waddlesplash Avatar answered Nov 27 '22 18:11

waddlesplash