summaryrefslogtreecommitdiff
path: root/sheetprint
blob: 701b9b549002c6bf6be0ac476ea66375cc0945b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/perl
#
# Sometimes it is handy to print an a4 sheet of labels through a regular
# printer, rather than using a dedicated label printer. This script kind of
# makes it possible.
#
# Add something like this line to /etc/mat.conf first:
# print_command "mv /usr/local/mat/label.png /usr/local/mat/label-`date +%s`.png"

use strict;
use warnings;

use GD;

use constant DPI => 300;

sub mm2dots
{
  my ( $mm ) = @_;

  return ($mm/2.54/10*DPI);
}

my $a4 = new GD::Image(210/2.54/10*DPI, 297/2.54/10*DPI);
my $white = $a4->colorAllocate(255,255,255);
my $black = $a4->colorAllocate(0,0,0);
$a4->fill(0, 0, $white);

sub draw_rulers()
{
  # Draw a cross
  $a4->line(0, 0, $a4->width(), $a4->height, $black);
  $a4->line($a4->width(), 0, 0, $a4->height, $black);

  for ( my $i = 1; $i < 297; $i++) {
    if ( $i % 10 == 0 ) {
      $a4->line(0, mm2dots($i), 20, mm2dots($i), $black);
      $a4->line(mm2dots(105), mm2dots($i), mm2dots(105) + 20, mm2dots($i), $black);
    } elsif ( $i % 5 == 0 )  {
      $a4->line(0, mm2dots($i), 15, mm2dots($i), $black);
      $a4->line(mm2dots(105), mm2dots($i), mm2dots(105) + 15, mm2dots($i), $black);
    } else {
      $a4->line(0, mm2dots($i), 10, mm2dots($i), $black);
      $a4->line(mm2dots(105), mm2dots($i), mm2dots(105) + 10, mm2dots($i), $black);
    }
  }
  for ( my $i = 1; $i < 210; $i++) {
    if ( $i % 10 == 0 ) {
      $a4->line(mm2dots($i), mm2dots(297), mm2dots($i), mm2dots(297) - 20, $black);
      $a4->line(mm2dots($i), mm2dots(149), mm2dots($i), mm2dots(149) - 20, $black);
    } elsif ( $i % 5 == 0 )  {
      $a4->line(mm2dots($i), mm2dots(297), mm2dots($i), mm2dots(297) - 15, $black);
      $a4->line(mm2dots($i), mm2dots(149), mm2dots($i), mm2dots(149) - 15, $black);
    } else {
      $a4->line(mm2dots($i), mm2dots(297), mm2dots($i), mm2dots(297) - 10, $black);
      $a4->line(mm2dots($i), mm2dots(149), mm2dots($i), mm2dots(149) - 10, $black);
    }
  }
}

my $skip = 0;
my $x = mm2dots(25);
my $y = mm2dots(32) + $skip * mm2dots(25);

for my $filename ( `ls label-??????????.png` ) {
  my $label = GD::Image->newFromPng($filename);
  $a4->copy($label, $x, $y, 0, 0, $label->width(), $label->height());
  $y += mm2dots(25);
}

open(PNGFILE, ">a4.png");
print PNGFILE $a4->png;

my $density = DPI * 1/2.54;

system("convert -density $density -define pdf:fit-page=A4 a4.png a4.pdf");