Last active
August 29, 2015 14:02
-
-
Save 0robustus1/9382ccc921619ab95a88 to your computer and use it in GitHub Desktop.
Serverprinting done easy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/env perl | |
| #use strict; | |
| use warnings; | |
| use Getopt::Std; | |
| use File::Basename; | |
| use constant SCP => "/usr/bin/scp"; | |
| use constant SSH => "/usr/bin/ssh"; | |
| my %opts = ( | |
| 'p' => "Stuga", | |
| 's' => "stuga", | |
| 'n' => 1 | |
| ); | |
| sub help_text { | |
| print "Usage: \n", | |
| "serverprint -p Printer -s Server -f File -n NoOfCopies -c -o '-o sides=two-sided-long-edge'\n"; | |
| exit 0; | |
| } | |
| sub try_load { | |
| my $mod = shift; | |
| eval("use $mod"); | |
| if ($@) { | |
| return(0); | |
| } else { | |
| return(1); | |
| } | |
| } | |
| sub check_size { | |
| $actual_size = shift; | |
| $wanted_size = shift; | |
| if (($actual_size-0.5)<=$wanted_size && ($actual_size+0.5)>=$wanted_size){ | |
| return 1; | |
| } else { | |
| return 0; | |
| } | |
| } | |
| getopts('o:f:p:s:n:c', \%opts); # -o, -f, -p, & -s take arguments. Values can be found in %opts | |
| die help_text unless $opts{f}; | |
| my $additional_opts = $opts{o} ? $opts{o} : ""; | |
| my $command = SCP." ".$opts{f}." ".$opts{s}.": && ".SSH." ".$opts{s}." lpr ". $additional_opts ." -r -P ".$opts{p}." ".basename($opts{f})." -#".$opts{n}; | |
| if ($opts{c}) { | |
| open(PDF,"pdfinfo ".$opts{f}." |") || die "Failed: not able to complete dimensionscheck\n","try again without the -c switch..."; | |
| my $width; | |
| my $height; | |
| while ( <PDF> ) | |
| { | |
| if ($_ =~ m/^Page size:\s*(\d+?\.?\d*?)\s*x\s*(\d+?\.?\d*?)\s*pts/){ | |
| $width = sprintf("%.2f",$1/72); | |
| $height = sprintf("%.2f",$2/72); | |
| } | |
| } | |
| #print "width:".$width.",height:".$height; | |
| my $portrait = (check_size($width,8.3) && check_size($height,11.7)); | |
| my $landscape = (check_size($height,8.3) && check_size($width,11.7)); | |
| unless ($portrait || $landscape) { | |
| print "file dimension are not matching Din A4\n"; | |
| print "trying to convert..."; | |
| system("pdftops -paper A4 ".$opts{f}." /tmp/".basename($opts{f}).".ps"."\n"); | |
| system("pstopdf /tmp/".basename($opts{f}).".ps -o /tmp/".basename($opts{f})."\n"); | |
| print "ok\n"; | |
| $command = SCP." /tmp/".basename($opts{f})." ".$opts{s}.": && ".SSH." ".$opts{s}." lpr ".$additional_opts." -r -P ".$opts{p}." ".basename($opts{f})." -#".$opts{n}; | |
| # print $command, "\n"; | |
| system($command."\n"); | |
| exit 0; | |
| } | |
| print "file dimensions ok, printing...\n" | |
| } | |
| # print $command, "\n"; | |
| system($command); | |
| die "unhappy result" if ($? >> 8) == -1; | |
| __END__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment