#!/usr/bin/perl
# description:
#       creates postscript from plaintext file using background provided in eps file
#       output is generated as postscript, and can easily converted to pdf using ps2pdf
#       optionally a printer name can be given as an argument, 
#       the file will then be printed using the lpr command

use strict;
use PostScript::Simple;
use PostScript::Simple::EPS;

my $sizex=600;
my $sizey=780;
my $x=0;
my $y=$sizey-10;
my $linepitch=10;
my $fontsize=10;
my $font="Courier";

print "#" x 70;
print "\n    trendprint v0.1 - copyright 2005 by it-TREND, Michael Wagner\n";
print "#" x 70;
print "\n";

if ($#ARGV < 2)
{
        print "usage: $0 printfile background output [printer]\n";
        print "example: $0 test.txt test.eps /somepath/out.ps lpr0\n";
        exit(1);
}

# create postscript object
my $p = new PostScript::Simple(
        eps => 0,
        xsize => $sizex,
        ysize => $sizey,
        clip => 1,
        colour => 1,
        units => "bp",
        reencode => "ISOLatin1Encoding");

$p->newpage;

# create an eps object
my $e = new PostScript::Simple::EPS(file => $ARGV[1]) || die "unable to include background file\n";
# add eps to the current page
$p->importeps($e, 0,0);

$p->setcolour( "black" );
$p->setfont($font, $fontsize);

# open text file for inserting into postscript
open(INFILE, "<$ARGV[0]") || die "cannot open printfile\n";
my(@lines) = <INFILE>;
my($line);
foreach $line (@lines)
{
        $p->text($x,$y, $line);
        $y-=$linepitch;
}
close(INFILE);

# write the output
$p->output($ARGV[2]) || die "could not write output file\n";
print "created $ARGV[2]\n";

# print if printer was given as an argument
if ($ARGV[3])
{
        print "printing $ARGV[2] to $ARGV[3]\n";
        exec("lpr -P $ARGV[3] $ARGV[2]");
}
