render pngs only with PNG=1 env variable
[Printer-EVOLIS.git] / scripts / inkscape-render.pl
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5 use autodie;
6
7 die "unsage: $0 card/template.svg 201008159999 login Ime Prezime\n" unless @ARGV;
8
9 my ($card_svg,$nr,$login,$ime,$prezime) = @ARGV;
10
11 my $png = $ENV{PNG} || 0;
12
13 warn "# svg: $card_svg nr: $nr $ime $prezime\n";
14
15 my $mapping = {
16 '200908109999' => $nr,
17 'login0123456789@ffzg.hr' => $login,
18 'Knjižničarko' => $ime,
19 'Čitalić' => $prezime,
20 };
21
22 sub mapping { $mapping->{ $_[0] } }
23
24 my $re = join('|', keys %$mapping);
25
26 mkdir 'out' unless -d 'out';
27 my $out = 'out/' . $nr;
28
29 foreach my $existing ( glob $out . '*' ) {
30         warn "# remove $existing ", -s $existing, " bytes\n";
31         unlink $existing;
32 }
33
34 open(my $svg_template, '<', $card_svg);
35 open(my $svg,          '>', "$out.svg");
36
37 while(<$svg_template>) {
38
39         if ( m{($re)} ) {
40                 warn "mapping $1\n";
41                 s{($1)}{mapping($1)}ge;
42         }
43
44         print $svg $_;
45
46 }
47
48 close($svg_template);
49 close($svg);
50
51 open(my $inkscape, '|-', 'inkscape --shell --without-gui');
52
53 sub inkscape_export {
54         my $part = shift;
55
56         my $shell = "$out.svg --export-area-page --export-id $part";
57
58         $part =~ s/print-//; # FIXME change svg files
59
60         print $inkscape "$shell --export-pdf $out.$part.pdf\n";
61         print $inkscape "$shell --export-png $out.$part.png --export-dpi 150\n" if $png;
62 }
63
64 inkscape_export 'print-front';
65 inkscape_export 'print-back';
66
67 # export visible
68 print $inkscape "$out.svg --export-png $out.png --export-dpi 300\n" if $png;
69
70 close($inkscape);
71
72 foreach my $pdf ( glob "$out*.pdf" ) {
73         my $pbm = $pdf;
74         $pbm =~ s/pdf$/pbm/;
75         warn "# rendering $pdf => $pbm using ghostscript\n";
76         system "gs -dNOPAUSE -dBATCH -q -r300x300 -dDEVICEWIDTHPOINTS=243 -dDEVICEHEIGHTPOINTS=155 -sDEVICE=pbmraw -sOutputFile=$pbm -f $pdf";
77 }
78
79 system "pdftk $out.front.pdf $out.back.pdf cat output $out.duplex.pdf";
80
81 __END__
82
83 #system "inkscape --file $out.print.svg  --export-pdf $out.pdf";
84
85 system "inkscape --file $out.print.svg --export-area-page --export-pdf $out.print-front.pdf --export-id print-front";
86 system "inkscape --file $out.print.svg --export-area-page --export-pdf $out.print-back.pdf --export-id print-back";
87 system "pdftk  $out.print-front.pdf $out.print-back.pdf cat output $out.print-duplex.pdf";
88
89 #system "inkscape --file $out.screen.svg --export-png $out.png --export-dpi 180";
90
91 #system "inkscape --file $out.screen.svg --export-png $out.300.png --export-dpi 300";
92
93 system "inkscape --file $out.print.svg --export-area-page --export-png $out.print-front.png --export-dpi 150 --export-id print-front --export-id-only";
94 system "inkscape --file $out.print.svg --export-area-page --export-png $out.print-back.png --export-dpi 150 --export-id print-back --export-id-only";
95
96
97 #system "qiv $out.png";
98 #system "xpdf $out.pdf";