[Samba] PDF printer using ps2pdf ?

René Nieuwenhuizen R.Nieuwenhuizen at cpb.nl
Wed Oct 30 14:11:01 GMT 2002


Hi,

This is the perlscript we use to print/mail PDF's. It uses ghostscript 
and perl MIME-tools to create and mail the PDF back to the user who 
printed the document. The printerdriver used is "QMS ColorScript 1000 
Level 2".

The config looks like this:

[prtpdf]
comment = PDF mailer
path = /var/spool/samba
print command = /usr/local/bin/prtpdf.pl %u %s
browseable = yes
writeable = no
printable = yes

CU

René Nieuwenhuizen
Sysadmin
CPB Netherlands Bureau for Economic Policy Analysis

PS. I've also attached a script to create and mail clipped EPS-files 
with preview. It must be used with nearly the same driver accept it must 
be Level 1 and the driver should output Encapsulated Postscript 
(Advanced Box somewhere...)

S. Ancelot heeft geschreven:

> Hi,
> Is it possible to set up a printer in samba
> that will use ps2pdf converter to create pdf documents when printing 
> on it ?
> Best Regards
> Steph
>
>


--
================================================================================
Aan dit bericht kunnen geen rechten worden ontleend.
Het bericht is alleen bestemd voor de geadresseerde.
Indien dit bericht niet voor u is bestemd, verzoeken wij u dit onmiddellijk aan
ons te melden en de inhoud van het bericht te vernietigen.

This message shall not constitute any obligations.
This message is intended solely for the addressee.
If you have received this message in error, please inform us immediately and
delete its contents.
================================================================================
-------------- next part --------------
#!/usr/bin/perl

use MIME::Entity;

my $username = shift() . '@cpb.nl';
my $psfile = shift;

my $GS = '/usr/local/bin/gs';

my $title = '';
open(PSFILE,$psfile);
while(<PSFILE>) {
    s/\s+$//;
    if (s/^%%Title:\s+//) {
	$title = $_;
	last;
    }
}
close(PSFILE);

my $pdffile = "doc$$.pdf";
my $ps2pdfcmd = "$GS -dSAFER -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$pdffile -c .setpdfwrite -f $psfile";
system($ps2pdfcmd);

my $top = MIME::Entity->build(
        From	=> "Samba Server <trash\@cpb.nl>",
        To	=> $username,
        Subject	=> "Uw document als PDF-bestand.",
	Data => ["Uw document ($title) als PDF-bestand (zie attachment).\n\n",
		 "Your document ($title) as PDF-file (see attachment)."]
);

$top->attach(	Path	=> $pdffile,
		Type	=> "application/pdf",
		Encoding=> "base64");

my $SENDMAIL = "/usr/lib/sendmail";
my $pid = open(SENDMAIL,"|-");
die "Can't fork: $!\n" unless defined $pid;
if ($pid) {
    $top->print(\*SENDMAIL);
    close SENDMAIL;
} else {
    exec $SENDMAIL,'-f','trash at cpb.nl',$username;
    die "Can't exec $SENDMAIL: $!\n";
}

unlink $pdffile;
unlink $psfile;
-------------- next part --------------
#!/usr/bin/perl

use MIME::Entity;

my $username = shift() . '@cpb.nl';
my $eps_filename = shift;

my $bbox_filename = "$eps_filename.$$.bbox";
my $tiff_filename = "$eps_filename.$$.tiff";

# DOS EPS Binary File Header
# Bytes	Description
#  0-3	Must be hex C5D0D3C6 (byte 0=C5).	(big-endian)
#  4-7	Byte position in file for start of PostScript language code section.
#  8-11	Byte length of PostScript language section.
# 12-15	Byte position in file for start of Metafile screen representation.
# 16-19	Byte length of Metafile section (PSize).
# 20-23	Byte position of TIFF representation.
# 24-27	Byte length of TIFF section.
# 28-29	Checksum of header (XOR of bytes 0-27). If Checksum is FFFF
#	then ignore it.
#
# Since this is a DOS format all values should be little endian.
#
# It is assumed that either the Metafile or the TIFF position and length fields
# are zero. That is, only one or the other of these two formats is included in
# the EPS file.

my $DOS_EPS_BF_HEADER_FORMAT = 'NVVVVVVv';
my $DOS_EPS_BF_MAGIC = 0xc5d0d3c6;
my $GS = '/usr/local/bin/gs';

my($lower,$left,$upper,$right);

&purifyFile;
&adjustBoundingBox;
&addTiffPreview;

my $title = '';
open(BBOXFILE,$bbox_filename);
while(<BBOXFILE>) {
    s/\s+$//;
    if (s/^%%Title:\s+//) {
	$title = $_;
	last;
    }
}
close(BBOXFILE);

my $top = MIME::Entity->build(
        From    => "Samba Server <trash\@cpb.nl>",
        To      => $username,
        Subject => "Uw plaatje als EPS-bestand.",
        Data => ["Uw plaatjes ($title) als EPS-bestand (zie attachment).\n\n",
                 "Your picture ($title) as EPS-file (see attachment)."]
);

$top->attach(   Path    => $eps_filename,
                Type    => "application/ps",
                Encoding=> "base64");

my $SENDMAIL = "/usr/lib/sendmail";
my $pid = open(SENDMAIL,"|-");
die "Can't fork: $!\n" unless defined $pid;
if ($pid) {
    $top->print(\*SENDMAIL);
    close SENDMAIL;
} else {
    exec $SENDMAIL,'-f','trash at cpb.nl',$username;
    die "Can't exec $SENDMAIL: $!\n";
}

unlink $tiff_filename;
unlink $bbox_filename;
unlink $eps_filename;

exit 0;

# purifyFile extracts the pure PostScript code from an EPS-file if
# a preview has been added.

sub purifyFile {
    my $header;
    open(EPSFILE,$eps_filename);
    read(EPSFILE,$header,30) or die "Can't read header: $!\n";
    my($magic,$startPS,$lengthPS,$startWMF,$lengthWMF,$startTIFF,$lengthTIFF,
		$checksum) = unpack($DOS_EPS_BF_HEADER_FORMAT,$header);
    if ($magic == $DOS_EPS_BF_MAGIC) {
	my $postscript;
	seek(EPSFILE,$startPS,0);
	read(EPSFILE,$postscript,$lengthPS);
	close(EPSFILE);
	open(EPSFILE,">$eps_filename") or die "Can't write $eps_filename: $!\n";
	print EPSFILE $postscript;
    } else {
	die "$eps_filename doesn't seem to be postscript!\n" if $header !~ m/^%!/;
    }
    close(EPSFILE);
}

# Adjust BoundingBox.

sub adjustBoundingBox {
    my $cmd = "$GS -dSAFER -dNOPAUSE -dBATCH -q -sDEVICE=bbox $eps_filename 2>&1";
    open(GS,"$cmd|") or die "Can't run $GS: $!\n";
    while(<GS>) {
	chop;
	last if m/^%%BoundingBox:/;
    }
    close(GS);
    if (s/^%%BoundingBox:\s+//) {
	($left,$lower,$right,$upper) = split(/\s+/);
    } else {
	return 0;
    }
    if ($left > 5 && $lower > 5) {
	$left -= 5;
	$lower -= 5;
	$right += 5;
	$upper += 5;
    }
    open(EPSFILE,$eps_filename);
    open(BBOXFILE,">$bbox_filename");
    while (<EPSFILE>) {
	last if m/^%%BoundingBox:/;
	print BBOXFILE;
    }
    print BBOXFILE "%%BoundingBox: $left $lower $right $upper\n";
    while (<EPSFILE>) {
	print BBOXFILE;
    }
    close(BBOXFILE);
    close(EPSFILE);
}

sub addTiffPreview {
    my($width,$height) = ($right-$left,$upper-$lower);
    my $cmd = "$GS -dSAFER -dNOPAUSE -dBATCH -dDEVICEWIDTH=$width -dDEVICEHEIGHT=$height -q -r72 -sDEVICE=tiffpack -sOutputFile=$tiff_filename -";
    open(GS,"|$cmd") or die "Can't run $GS: $!\n";

    print GS<<EOF;
-$left -$lower translate
 /EPSTOOL_save save def
 /showpage {} def
 count /EPSTOOL_count exch def
 /EPSTOOL_countdictstack countdictstack def
EOF

    open(BBOXFILE,$bbox_filename);
    while (<BBOXFILE>) {
	print GS;
    }
    close(BBOXFILE);

    print GS<<EOF;
 count EPSTOOL_count sub {pop} repeat
 countdictstack EPSTOOL_countdictstack sub {end} repeat
 EPSTOOL_save restore
 showpage

quit
EOF

    close(GS);

    unlink $eps_filename;
    $eps_filename = "pic$$.eps";
    my($header) = pack($DOS_EPS_BF_HEADER_FORMAT,
		$DOS_EPS_BF_MAGIC,
		30,			# start postscript code
		(-s $bbox_filename),		# length postscript code
		0,			# start windows representation
		0,			# length windows representation
		30 + (-s $bbox_filename),	# start tiff representation
		(-s $tiff_filename),		# length tiff representation
		0xffff);		# no checksum
    open(EPSFILE,">$eps_filename");
    print EPSFILE $header;
    close(EPSFILE);
    system("cat $bbox_filename $tiff_filename >> $eps_filename");
}


More information about the samba mailing list