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 # Latest change: Son Mär 18 19:31:15 CET 2007 [mika]
8 ################################################################################
9 # Downloaded from http://www.linuxprinting.org/download/printing/alignmargins
11 $0 =~ m!^(.*)/[^/]+$!;
13 my $printcommand = '/usr/bin/lpr -P ';
14 my $egrep = '/bin/egrep';
16 my $cut = '/usr/bin/cut';
17 my $head = '/usr/bin/head';
18 my $tail = '/usr/bin/tail';
19 my $wc = '/usr/bin/wc';
20 my $adjustmentpagename = 'align.ps';
21 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';
22 my $ppddir = '/etc/cups/ppd';
23 my $printerconffile = '/etc/cups/printers.conf';
28 for $path (split(":", $adjustmentpagepath)) {
29 if (-r "$path/$adjustmentpagename") {
30 $adjustmentpage = "$path/$adjustmentpagename";
35 # Are we running as root?
37 if (!(-w $printerconffile)) {die "\"alignmargins\" must be run logged in as \"root\"!";}
39 # Check whether there are local printer queues
41 open NUMBEROFQUEUES, "$cat $printerconffile | $egrep '<Printer|<DefaultPrinter' | $wc -l |" or die "Cannot read local printer configuration!";
43 my $nqueues = <NUMBEROFQUEUES>;
46 # Ask the user which printer he wants to align
49 print "CUPS printer margin and offset alignment\n";
50 print "----------------------------------------\n";
52 print "(C) 2001 by Till Kamppeter\n";
53 print "Free software under the terms of the GNU General Public License (GPL)\n";
58 print "With this program you can adjust the margins and the position of the\n";
59 print "printed contents on the paper. This way you get well-centered printouts and\n";
60 print "you can make use of the whole imageable area of your printer, The driver\n";
61 print "settings are overridden when this adjustment is used.\n";
63 print "This is especially important when your printer is used with a driver for\n";
64 print "another printer to which yours is compatible (for example many laser\n";
65 print "printers are compatible to the HP LaserJet printers). Your printer prints\n";
66 print "with this driver, but the non-printable margins are usually different or the\n";
67 print "contents is even not centered. With this program you can fix these problems\n";
69 print "The program can only be applied to local printer queues. The following\n";
70 print "queues are available:\n";
73 system "$cat $printerconffile | $egrep '<Printer|<DefaultPrinter' | $cut -d ' ' -f 2 | $cut -d '>' -f 1 | $cat -n";
76 print "Please enter the number of the desired printer and make sure that it is\n";
77 print "connected to your computer and turned on.\n";
83 if ( $input =~ m/^\s*(\d+)\D*/ ) {
85 if (($number > 0) && ($number <= $nqueues)) {
86 open QUEUE, "$cat $printerconffile | $egrep '<Printer|<DefaultPrinter' | $cut -d ' ' -f 2 | $cut -d '>' -f 1| $head -$number | $tail -1 |";
93 print "\nWrong input, try again!\n";
95 } until ($queue ne "");
100 print "Printing margin/offset adjustment page ...\n";
103 # The "%!" which is needed in a file to be recognized as a PostScript file
104 # is missing in the adjustment page, therefore it is preceeded to the file
106 if (system "(echo %!; $cat $adjustmentpage) | $printcommand$queue") {
107 die "Could not print the adjustment page.";
110 print "Please read the instructions on the margin adjustment page and determine the\n";
111 print "six numbers mentioned there. If you measure in cm and not in inches, devide\n";
112 print "the measured quantities by 2.54 before you insert them into the equations\n";
113 print "shown on the page. You do not need to create any file with PostScript\n";
114 print "commands, this program will insert your settings into your printer's\n";
115 print "configuration.\n";
117 print "If the adjustment page did not come out of your printer, this method cannot\n";
118 print "be applied, press Ctrl + C to stop this program. This can especially happen\n";
119 print "with very old PostScript printers.\n";
121 print "Note also that this adjustment does not necessarily work with every driver.\n";
122 print "The concept is taken from GhostScript and the implementation of this program\n";
123 print "is not much tested yet.\n";
126 print "Please enter your results now:\n";
140 if ( $input =~ m/^\s*([+-]?[\d\.]+)\D*/ ) {
142 if (($number >= -100000) && ($number <= 100000)) {
146 print "Wrong input, try again!\n";
148 } until ($ml != 9999999.);
154 if ( $input =~ m/^\s*([+-]?[\d\.]+)\D*/ ) {
156 if (($number >= -100000) && ($number <= 100000)) {
160 print "Wrong input, try again!\n";
162 } until ($mb != 9999999.);
168 if ( $input =~ m/^\s*([+-]?[\d\.]+)\D*/ ) {
170 if (($number >= -100000) && ($number <= 100000)) {
174 print "Wrong input, try again!\n";
176 } until ($mr != 9999999.);
182 if ( $input =~ m/^\s*([+-]?[\d\.]+)\D*/ ) {
184 if (($number >= -100000) && ($number <= 100000)) {
188 print "Wrong input, try again!\n";
190 } until ($mt != 9999999.);
196 if ( $input =~ m/^\s*([+-]?[\d\.]+)\D*/ ) {
198 if (($number >= -100000) && ($number <= 100000)) {
202 print "Wrong input, try again!\n";
204 } until ($x != 9999999.);
210 if ( $input =~ m/^\s*([+-]?[\d\.]+)\D*/ ) {
212 if (($number >= -100000) && ($number <= 100000)) {
216 print "Wrong input, try again!\n";
218 } until ($y != 9999999.);
220 my $ppdfilename = "$ppddir/$queue.ppd";
222 print "Saving your settings in $ppdfilename ...\n";
225 my @marginsoption = (
226 "*OpenUI *Margins/Page Margins/Offsets: PickOne\n",
227 "*DefaultMargins: Custom\n",
228 "*Margins Default/Driver Default: \"\"\n",
229 "*Margins Custom/Custom (set with 'alignmargins'): \"<</.HWMargins[$ml $mb $mr $mt] /Margins[$x $y]>>setpagedevice\"\n",
230 "*CloseUI: *Margins\n"
233 # Read PPD file of the chosen printer
235 if (!(-f $ppdfilename)) {die "No PPD file $ppdfilename!"};
236 open PPDFILE, "$ppdfilename" or die "Can't open $ppdfilename!";
237 my @ppdfile = <PPDFILE>;
240 # Remove an old margin adjustment option
242 ($_ =~ m!^\s*\*OpenUI\s*\*Margins/.*:! and $_="") foreach @ppdfile;
243 ($_ =~ m!^\s*\*DefaultMargins:! and $_="") foreach @ppdfile;
244 ($_ =~ m!^\s*\*Margins\s*.*/.*:! and $_="") foreach @ppdfile;
245 ($_ =~ m!^\s*\*CloseUI:\s*\*Margins! and $_="") foreach @ppdfile;
247 # Insert the new margin adjustment option
249 splice(@ppdfile,-1,0,@marginsoption);
251 # Write back PPD file
253 open PPDFILE, ">$ppdfilename" or die "Can't open $ppdfilename";
254 print PPDFILE @ppdfile;
257 # Re-initialize CUPS (must be done after a "manual" change on the PPD file)
259 system("killall -HUP cupsd");
264 print "Now your printer \"$queue\" will use the new margin and offset settings by\n";
265 print "default. You can turn them off by switching the option \"Page Margins/Offsets\"\n";
266 print "to \"Driver Default\" in kprinter, GTKlp, or XPP.\n";
268 print "On the command line (\"lpr\", \"lp\", \"lpoptions\", ...) use the option\n";
269 print "\"-o Margins=Default\" to turn off and \"-o Margins=Custom\" to turn on your\n";
275 ## END OF FILE #################################################################