njoyperl has asked for the wisdom of the Perl Monks concerning the following question:

I am new to Perl. I have built a simple GUI screen with some Lables and data in some text fields. How can I send the data (Labels and text box values) to a printer or to a file and then to a printer? I want to have a 'Print' button that needs to do this. Please point me in the right direction. Thanks for your help

Replies are listed 'Best First'.
Re: Printing via Win32 GUI
by davido (Cardinal) on Jul 20, 2012 at 22:30 UTC

    There's a pretty good node here: Printing in NT. One of the followups shows multiple ways to send output to a printer on Win32 (in the true spirit of Perl).

    As for sending output to files, that's covered in perlintro, and is easier than fiddling with printers.

    Beyond that, are you asking how to retrieve values from text boxes? We would need to know the type of framework you're using and probably see a bit of code to give a thorough answer on that topic.


    Dave

      Thank you for the link. That has very good information on what I am attempting to do. I will give those methods a try and get back to this thread.
Re: Printing via Win32 GUI
by dasgar (Priest) on Jul 20, 2012 at 22:03 UTC

    The labels and text boxes are objects. You just need to use the Text method to retrieve the values and then you do what you want with those values. If you're using an older version of Win32::GUI, it might be GetText instead of just Text.

      I think I can grab the values from the text boxes, labels etc. and send it to an output file. What I am not clear is how to initiate a print using the user's default windows printer and send the file to the printer. Thanks for your suggestions.
Re: Printing via Win32 GUI
by cavac (Prior) on Jul 21, 2012 at 22:29 UTC

    You can also generate a PDF file and use a number of programs, including Acrobat Reader to send that to a printer (this gives you also more or less complete control over the page layout).

    I found this article on stackoverflow for the acroread command line switches.

    On linux, you can also send the PDF directly to the printer queue using the lp command. I strongly suspect there is something similar on Windows, and if not, you should be able to use the acrobat method described above.

    Here's a simple example from my vast DarkPAN repo. In this program i use a pre-designed label (also a PDF file), fill in the text fields from the database and send that to a printer queue (which in this case prints to a PTouch label printer).

    #!/usr/bin/perl -w #---AUTOPRAGMASTART--- use 5.012; use strict; use warnings; use diagnostics; use mro 'c3'; use English qw( -no_match_vars ); use Carp; our $VERSION = 0.996; #---AUTOPRAGMAEND--- use Maplat::Helpers::ConfigLoader; use Sys::Hostname; use Time::HiRes qw(sleep usleep); use DBI; use PDF::Report; use Maplat::Helpers::Logo; our $APPNAME = "PTouch"; MaplatLogo($APPNAME, $VERSION); my $ps_appname = lc($APPNAME); $ps_appname =~ s/[^a-z]+/_/gio; $0 = $ps_appname; my $configfile = shift @ARGV; print "Loading config file $configfile\n"; my $config = LoadConfig($configfile, ForceArray => [ 'module', 'redirect', 'menu', 'vie +w', 'userlevel', 'rootfile' ],); my $dbh = DBI->connect($config->{dburl}, $config->{dbuser}, $config->{ +dbpassword}, {AutoCommit => 0}) or croak("can't connect to database"); my $cycleStartTime = time; while(1) { $0 = $ps_appname . " working"; my $workCount = printLabel(); $0 = $ps_appname . " idle"; my $tmptime = time; my $workTime = $tmptime - $cycleStartTime; if($workTime < 0) { $workTime = 0; # Handle winter->summer time switch } my $sleeptime = $config->{mincycletime} - $workTime; if($sleeptime > 0) { print "** Fast cycle ($workTime sec), sleeping for $sleeptime +sec **\n"; sleep($sleeptime); print "** Wake-up call **\n"; } else { print "** Cycle time $workTime sec **\n"; } $cycleStartTime = time; } sub printLabel { my $workCount = 0; my @labels; my $selsth = $dbh->prepare_cached("SELECT * FROM ptouch_queue WHERE printer_name = ? AND label_type = 'COMPUTER' ORDER BY queuetime") or croak($dbh->errstr); my $delsth = $dbh->prepare_cached("DELETE FROM ptouch_queue WHERE job_id = ?") or croak($dbh->errstr); # Read and parse data $selsth->execute($config->{printer}) or croak($dbh->errstr); while((my $label = $selsth->fetchrow_hashref)) { my %data = ( job_id => $label->{job_id}, ); my @rawdat = @{$label->{labeldata}}; foreach my $key (qw[computer_name account_user account_passwor +d account_domain line_id description net_prod_ip net_line_ip]) { $data{$key} = shift @rawdat || ''; } push @labels, \%data; } $selsth->finish; $dbh->rollback; # Now, print all labels and delete the finished jobs foreach my $label (@labels) { $0 = $ps_appname . " printing COMPUTER " . $label->{computer_n +ame}; my $pdf = PDF::Report->new(File => "template.pdf"); my $page = $pdf->openpage(1); my ($width,$height) = $pdf->getPageDimensions(); $pdf->setFont('Arial'); $pdf->setSize(12); $pdf->addRawText("Computer:", 15, 50, "black", undef, undef, 9 +0); $pdf->addRawText($label->{computer_name}, 15, 110, "black", un +def, undef, 90); $pdf->addRawText('Benutzer: ', 28, 50, "black", undef, undef, +90); $pdf->addRawText($label->{account_user}, 28, 110, "black", und +ef, undef, 90); $pdf->addRawText('Password: ', 41, 50, "black", undef, undef, +90); $pdf->addRawText($label->{account_password}, 41, 110, "black", + undef, undef, 90); $pdf->setSize(8); $pdf->addRawText($label->{line_id} . ' / ' . $label->{descript +ion} , 50, 50, "black", undef, undef, 90); $pdf->addRawText($label->{account_domain} . ' / ' . $label->{n +et_prod_ip} . ' / ' . $label->{net_line_ip}, 59, 50, "black", undef, +undef, 90); $pdf->saveAs($config->{tempfilename}); my $cmd = 'lp ' . $config->{tempfilename}; `$cmd`; $delsth->execute($label->{job_id}) or croak($dbh->errstr); $dbh->commit; $workCount++; } return $workCount; }

    Before you ask: Yes we do print the user/password combinations on this label. The reasons are a too complicated to explain here... and you don't wanna know anyway.

    "I know what i'm doing! Look, what could possibly go wrong? All i have to pull this lever like so, and then press this button here like ArghhhhhaaAaAAAaaagraaaAAaa!!!"
      Thanks for your input. Good idea to use PDF as a media to print. I will try all these suggestions and get back to this thread to post what worked for me. It might take a while as I am not full time working on this.
Re: Printing via Win32 GUI
by rpnoble419 (Pilgrim) on Jul 21, 2012 at 20:48 UTC
    Use Win32::Printer. I know its has not been updated in a few years, but I use daily to print reports and badges for a trade show application. You have all of the control over the printer you would have in the old VB6.
Re: Printing via Win32 GUI
by linuxkid (Sexton) on Jul 20, 2012 at 21:51 UTC

    Generate html and use firefox or something

    --linuxkid


    imrunningoutofideas.co.cc
      I was thinking to initiate a Windows print dialog, grab the output from a text file and let the user choose a printer(network) and print the file with data. If the printer selection can default to a default printer, that will be nice. I don't want the user to start another app like IE/Firefox to print it again. Thanks for your suggestion though. Appreciate it.

        What windowing toolkit?

        take a look at this: Re:Printing in Tk

        --linuxkid


        imrunningoutofideas.co.cc