logo       

Re: Simple accounting filter/backend (With Perl or another script language): msg#00046

printing.cups.devel

Subject: Re: Simple accounting filter/backend (With Perl or another script language)

Jerome Alet wrote:
> If you want you can even use the file in question (pdlanalyzer.py)
> completely independantly of PyKota (no need to install anything
> beside Python and this file, although Python-Psyco is recommended
> for performance reasons). This will give you instant support
> for PostScript (binary and DSC compliant), PDF, PCL3, PCL4, PCL5,
> PCLXL (aka PCL6), and ESC/P2. So instead of launching
> gs + bbox, just launch "python pdlanalyzer.py file1.ps ..."
> (or use a pipe)

Ok, just a few more tests and I'll consider this solution. I mean, this is
python, and I don't know this language. Moreover, it'd force me to ship two
scripts in two different languages... To be honest, i'd prefer _one_ monolithic
script and _one_ language I can handle. But your script seem really more
powerful and versatile, so I keep it at hand.

> Just keep in mind that it's NOT perfect, and that some day you'll
> find a PostScript file which fools your program either intentionnally
> or not.

That's ok. Remember, I just wanted to provide a simple solution, only to keep
track of printer usage by users, no billing, and maybe no quotas... Well, for
the quotas, I'd like to, so I may try if the first stage completes well, thanks
to your help ;-) !

Here's my first snippet of the new page counting part. I seems to work, I just
need to reorganize it to "[use the most accurate method] or [fallback to a less
accurate] or [fallback to a less accurate] ... or [Pages=-1 ;-)]". Comments ?

---------------------------------------------------------
#!/usr/bin/perl -w
use strict;

my $doc="toto.ps";

my ($page_num_pages,$page_num_copies,$copies_num_copies,$page_bbox)=(0,0,0,0);

open DOC, $doc;
while (<DOC>){
if( $_ =~ m/^%%Pages:\s+([0-9]+)$/ ){
$page_num_pages=$1;
}
if ( $_ =~ m/1\s+dict\s+dup\s+\/NumCopies\s+([0-9]+)\s+/ ){
$copies_num_copies=$1;
$page_num_copies++;
}
}

my $page_bbox_cmd = "/usr/bin/gs -sDEVICE=bbox -dNOPAUSE -c save pop -f
".$doc." -c quit 2>&1";

my $page_bbox_result = `$page_bbox_cmd`;
if (!defined $page_bbox_result || !$!){
die "$0: Unable to count pages with bbox method !\n";
}

foreach my $page_bbox_line ( $page_bbox_result ){
if ($page_bbox_line =~ m/%%BoundingBox/){
$page_bbox++;
}
}

print "Page count data:\n page_num_pages: $page_num_pages\n
copies_num_copies: $copies_num_copies\n";
print " page_num_copies: $page_num_copies\n page_bbox: $page_bbox\n";
---------------------------------------------------------

At execution (toto is my test PS - 37 copies - 2 pages):
$ ./compte_ps
Page count data:
page_num_pages: 2
copies_num_copies: 37
page_num_copies: 2
page_bbox: 1 (?)

So, what I think, now:

if ("NumCopies")
use this to guess number of copies and pages
else if ("%%Pages:")
use this for pages
else
fallback to bbox for pages
else
nothing accurate found, use "NULL"
end
total_pages=pages*copies or NULL

Well, the flaw is that if someone ask for 500 copies of a 1 page document,
we're screwed...


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise