2 # Filename: alignmargins
3 # Purpose: adjust the margins and the position of the printed contents on the paper
4 # Authors: (C) 2001 by Till Kamppeter
5 # Bug-Reports: see http://grml.org/bugs/
6 # License: Free software under the terms of the GNU General Public License (GPL)
7 ################################################################################
8 # Downloaded from http://www.linuxprinting.org/download/printing/alignmargins
10 $0 =~ m!^(.*)/[^/]+$!;
12 my $printcommand = '/usr/bin/lpr -P ';
13 my $egrep = '/bin/egrep';
15 my $cut = '/usr/bin/cut';
16 my $head = '/usr/bin/head';
17 my $tail = '/usr/bin/tail';
18 my $wc = '/usr/bin/wc';
19 my $adjustmentpagename = 'align.ps';
20 my $adjustmentpagepath = ($programpath ? "${programpath}:" : "") . '.:~:/usr/share/grml-scripts:/usr/share/alignmargins:/usr/local/share/alignmargins:/usr/share:/usr/local/share:/usr/share/printer-testpages:/usr/local/share/printer-testpages:/usr/share/ghostscript/*/lib:/usr/local/share/ghostscript/*/lib';
21 my $ppddir = '/etc/cups/ppd';
22 my $printerconffile = '/etc/cups/printers.conf';
27 for $path (split(":", $adjustmentpagepath)) {
28 if (-r "$path/$adjustmentpagename") {
29 $adjustmentpage = "$path/$adjustmentpagename";
34 # Are we running as root?
36 if (!(-w $printerconffile)) {die "\"alignmargins\" must be run logged in as \"root\"!";}
38 # Check whether there are local printer queues
40 open NUMBEROFQUEUES, "$cat $printerconffile | $egrep '<Printer|<DefaultPrinter' | $wc -l |" or die "Cannot read local printer configuration!";
42 my $nqueues = <NUMBEROFQUEUES>;
45 # Ask the user which printer he wants to align
48 print "CUPS printer margin and offset alignment\n";
49 print "----------------------------------------\n";
51 print "(C) 2001 by Till Kamppeter\n";
52 print "Free software under the terms of the GNU General Public License (GPL)\n";
57 print "With this program you can adjust the margins and the position of the\n";
58 print "printed contents on the paper. This way you get well-centered printouts and\n";
59 print "you can make use of the whole imageable area of your printer, The driver\n";
60 print "settings are overridden when this adjustment is used.\n";
62 print "This is especially important when your printer is used with a driver for\n";
63 print "another printer to which yours is compatible (for example many laser\n";
64 print "printers are compatible to the HP LaserJet printers). Your printer prints\n";
65 print "with this driver, but the non-printable margins are usually different or the\n";
66 print "contents is even not centered. With this program you can fix these problems\n";
68 print "The program can only be applied to local printer queues. The following\n";
69 print "queues are available:\n";
72 system "$cat $printerconffile | $egrep '<Printer|<DefaultPrinter' | $cut -d ' ' -f 2 | $cut -d '>' -f 1 | $cat -n";
75 print "Please enter the number of the desired printer and make sure that it is\n";
76 print "connected to your computer and turned on.\n";
82 if ( $input =~ m/^\s*(\d+)\D*/ ) {
84 if (($number > 0) && ($number <= $nqueues)) {
85 open QUEUE, "$cat $printerconffile | $egrep '<Printer|<DefaultPrinter' | $cut -d ' ' -f 2 | $cut -d '>' -f 1| $head -$number | $tail -1 |";
92 print "\nWrong input, try again!\n";
94 } until ($queue ne "");
99 print "Printing margin/offset adjustment page ...\n";
102 # The "%!" which is needed in a file to be recognized as a PostScript file
103 # is missing in the adjustment page, therefore it is preceeded to the file
105 if (system "(echo %!; $cat $adjustmentpage) | $printcommand$queue") {
106 die "Could not print the adjustment page.";
109 print "Please read the instructions on the margin adjustment page and determine the\n";
110 print "six numbers mentioned there. If you measure in cm and not in inches, devide\n";
111 print "the measured quantities by 2.54 before you insert them into the equations\n";
112 print "shown on the page. You do not need to create any file with PostScript\n";
113 print "commands, this program will insert your settings into your printer's\n";
114 print "configuration.\n";
116 print "If the adjustment page did not come out of your printer, this method cannot\n";
117 print "be applied, press Ctrl + C to stop this program. This can especially happen\n";
118 print "with very old PostScript printers.\n";
120 print "Note also that this adjustment does not necessarily work with every driver.\n";
121 print "The concept is taken from GhostScript and the implementation of this program\n";
122 print "is not much tested yet.\n";
125 print "Please enter your results now:\n";
139 if ( $input =~ m/^\s*([+-]?[\d\.]+)\D*/ ) {
141 if (($number >= -100000) && ($number <= 100000)) {
145 print "Wrong input, try again!\n";
147 } until ($ml != 9999999.);
153 if ( $input =~ m/^\s*([+-]?[\d\.]+)\D*/ ) {
155 if (($number >= -100000) && ($number <= 100000)) {
159 print "Wrong input, try again!\n";
161 } until ($mb != 9999999.);
167 if ( $input =~ m/^\s*([+-]?[\d\.]+)\D*/ ) {
169 if (($number >= -100000) && ($number <= 100000)) {
173 print "Wrong input, try again!\n";
175 } until ($mr != 9999999.);
181 if ( $input =~ m/^\s*([+-]?[\d\.]+)\D*/ ) {
183 if (($number >= -100000) && ($number <= 100000)) {
187 print "Wrong input, try again!\n";
189 } until ($mt != 9999999.);
195 if ( $input =~ m/^\s*([+-]?[\d\.]+)\D*/ ) {
197 if (($number >= -100000) && ($number <= 100000)) {
201 print "Wrong input, try again!\n";
203 } until ($x != 9999999.);
209 if ( $input =~ m/^\s*([+-]?[\d\.]+)\D*/ ) {
211 if (($number >= -100000) && ($number <= 100000)) {
215 print "Wrong input, try again!\n";
217 } until ($y != 9999999.);
219 my $ppdfilename = "$ppddir/$queue.ppd";
221 print "Saving your settings in $ppdfilename ...\n";
224 my @marginsoption = (
225 "*OpenUI *Margins/Page Margins/Offsets: PickOne\n",
226 "*DefaultMargins: Custom\n",
227 "*Margins Default/Driver Default: \"\"\n",
228 "*Margins Custom/Custom (set with 'alignmargins'): \"<</.HWMargins[$ml $mb $mr $mt] /Margins[$x $y]>>setpagedevice\"\n",
229 "*CloseUI: *Margins\n"
232 # Read PPD file of the chosen printer
234 if (!(-f $ppdfilename)) {die "No PPD file $ppdfilename!"};
235 open PPDFILE, "$ppdfilename" or die "Can't open $ppdfilename!";
236 my @ppdfile = <PPDFILE>;
239 # Remove an old margin adjustment option
241 ($_ =~ m!^\s*\*OpenUI\s*\*Margins/.*:! and $_="") foreach @ppdfile;
242 ($_ =~ m!^\s*\*DefaultMargins:! and $_="") foreach @ppdfile;
243 ($_ =~ m!^\s*\*Margins\s*.*/.*:! and $_="") foreach @ppdfile;
244 ($_ =~ m!^\s*\*CloseUI:\s*\*Margins! and $_="") foreach @ppdfile;
246 # Insert the new margin adjustment option
248 splice(@ppdfile,-1,0,@marginsoption);
250 # Write back PPD file
252 open PPDFILE, ">$ppdfilename" or die "Can't open $ppdfilename";
253 print PPDFILE @ppdfile;
256 # Re-initialize CUPS (must be done after a "manual" change on the PPD file)
258 system("killall -HUP cupsd");
263 print "Now your printer \"$queue\" will use the new margin and offset settings by\n";
264 print "default. You can turn them off by switching the option \"Page Margins/Offsets\"\n";
265 print "to \"Driver Default\" in kprinter, GTKlp, or XPP.\n";
267 print "On the command line (\"lpr\", \"lp\", \"lpoptions\", ...) use the option\n";
268 print "\"-o Margins=Default\" to turn off and \"-o Margins=Custom\" to turn on your\n";
274 ## END OF FILE #################################################################