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!!!"
|