Home General
New Blog Posts: Merging Reports - Part 1 and Part 2

Set default printer while report generates

edited January 2012 in General

Setup:
I am programming on Delphi 2010 and Report builder 14. The application is
targeting a laptop running windows 7 that needs to be connected to a docking
station in order to print.

The project:
I am doing some printer checking prior (Find the default printer for that
document and check/wait until that printer is online) to sending the
document to the printer.
I am having some unexpected results.

Approach 1:
Procedure ppReport1PrintDialogCreate(Sender: TObject)
begin
SetDefaultPrinter; //set the default printer on windows.
ppReportShiftLog.Printer := 'Some printer'

while not PrinterIsOnline do
begin
sleep(100);
Application.ProcessMessages;
if fw.bCancelPrinting then //pop cancel form
begin
. Close report
end;
end;
End;

The print dialog selects the first printer not the default.
I am guessing that my timing is off. The print dialog has been created
before I set the default printer.


Approach 2:
Procedure ppReport1PreviewFormCreate
Begin
// Change the on click event and handle the printing myself
TppPrintPreview(ppReportShiftLog.PreviewForm).PrintButton.OnClick :=
OnReportPrintButtonClick;
End

Procedure OnReportPrintButtonClick(Sender: TObject);
Begin
SetDefaultPrinter; //set the default printer on windows.
ppReport1.Printer := 'Some printer'

while not PrinterIsOnline do
begin
sleep(100);
Application.ProcessMessages;
if fw.bCancelPrinting then //pop cancel form
begin
. Close report
end;
end;

//sent the report to the printer
ppReport1.DeviceType := 'Printer';
ppReport1.Print;
end

Here the print dialog displays the correct printer but the document sent to
the printer seems to be incomplete. Most of the time the printer window
displays the document with pages = 'N/A' and nothing would print. I am
guessing that the report has not yet finished generating. I have also tried
to wait until the footer band has been printer by the preview but I get a
similar result. I am also suspect the problem may be due to the
OnReportPrintButtonClick being called by a different Thread.


Please help
Thanks

Temoc Navarro

Comments

  • edited January 2012
    Is it possible to access and change the printer of the report Print Dialog
    on procedure ppReport1PrintDialogCreate ?


    Temoc Navarro




  • edited January 2012
    Hi Temoc,

    From your description, it sounds like this is a timing issue more than
    anything else. It seems perhaps that the default printer has not been
    fully realized by Windows or your app before you are trying to print the
    report. My suggestion would be to use an earlier event perhaps when the
    application is loaded or to create a timer to allow enough time for the
    default printer to be assigned before printing.

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited January 2012
    Thanks for your help but I still unable to accomplish my goal. I hope you
    can give me other options.

    I do have a timer that starts checking for the printer as soon as the form
    is loaded. Once the printer is detected, it is set it as the default
    printer. This part works.
    The problem occurs when the user clicks the print button before setting the
    laptop on the docking station connected to the printer. At this point the
    printer has not yet been detected and a waiting window pops waiting for the
    printer to be online.
    If I force the report to wait (on any other point like on open pipeline, on
    print, etc.) for the printer to be online, the user will not be able to
    preview the report unless there is a printer.


    I created a custom button following the wiki example and I modified it as
    follows:

    procedure TmyPreview.ehFilebutton_Click(Sender: TObject);
    var
    fw : TfWaitingWindow;
    i : integer;
    begin
    try
    try
    //Create the waiting form if the printer is not available yet.
    if not CheckForPrinter then
    begin
    fw := TfWaitingWindow.Create(nil);
    fw.Show;
    end;
    //if we are still looking for a printer wait
    while not CheckForPrinter do // CheckForPrinter checks and sets the
    printer
    begin
    sleep(100);
    Application.ProcessMessages;
    if fw.bCancelPrinting then
    begin
    exit;
    end;
    end;
    finally
    if fw <> nil then
    FreeAndNil(fw);
    end;
    except
    on E: Exception do
    begin
    ShowMessage(E.Message);
    end;
    end;

    //wait another 5 seconds
    for I := 0 to 10 do
    begin
    sleep(500);
    Application.ProcessMessages;
    end;
    // at this point I manually open windows printers and confirm that the
    corrected printer is the default
    showmessage('Stop, Check default printer on Devices and Printer');

    print;
    end;

    Right away this code gets call prior to the Procedure
    ppReport1PreviewFormCreate

    Base on the previous code I confirmed that the printer is set prior to
    calling the print function. Yet the print Dialog displayed a different
    printer.

    Is there a wait to try to pass the printer name to the report or the print
    dialog inside the ehFilebutton_Click ?

    My goal is to stop the print dialog from showing until the printer is
    online. Then display the dialog with the correct printer selected. Is there
    any other wait where I can accomplish this behavior?

    Thanks again.
    Temoc Navarro

  • edited January 2012
    Hi Temoc,

    I would wonder if you could get this to work correctly even if you do so
    before the report is previewed. As a test, try executing your "waiting"
    code another place in your application (perhaps a temporary button),
    then before event previewing the report, plug the laptop into the
    docking station, preview the report, then print.

    It seems a better solution would be to simply give your users an error
    message if they try to print without a valid printer connected.

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited January 2012
    I tested waiting for the printer before calling the preview and it works
    fine but it is not acceptable solution.

    do you have any idea at what point is the print dialog generated?

    is there away to set the ppReport1.Printer inside ehFilebutton_Click ?

    Temoc Navarro


  • edited January 2012
    Hi Temoc,

    Thanks for the info. Since you are able to get this to work before
    calling Print, perhaps we can get it working with an earlier event. Try
    moving your code to the OnInitializeParameters event of the report and
    see if that helps. This is the first event fired once the report is
    printed.

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited January 2012
    OnInitializeParameter gets call before the preview is display. I think the
    issue is that the print dialog is generated during the preview. thus making
    it to late for me to set default printer any time after the preview is
    generated.

    I think that is best to take a different approach. I will be printing to pdf
    instead of the screen and displaying a pdf viewer as the preview with my own
    print button. If you see anything wrong with this approach, please let me
    know.

    Thank you for your help.

    Temoc Navarro


This discussion has been closed.