update for new inkscape --actions
[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 sub inkscape_export {
52         my $part = shift;
53
54         my $actions = "file-open:$out.svg ; export-area-page ; export-id:$part ;";
55
56         $part =~ s/print-//; # FIXME change svg files
57
58         system qq{inkscape --actions="$actions ; export-type:pdf ; export-filename:$out.$part.pdf ; export-do ;"};
59 #       print $inkscape "$actions ; export-type:png ; export-filename:$out.$part.png ; export-dpi 150 ; export-do ;" if $png;
60 }
61
62 inkscape_export 'print-front';
63 inkscape_export 'print-back';
64
65 # export visible
66 #print $inkscape "$out.svg --export-png $out.png --export-dpi 300\n" if $png;
67
68
69 foreach my $pdf ( glob "$out*.pdf" ) {
70         my $pbm = $pdf;
71         $pbm =~ s/pdf$/pbm/;
72         warn "# rendering $pdf => $pbm using ghostscript\n";
73         system "gs -dNOPAUSE -dBATCH -q -r300x300 -dDEVICEWIDTHPOINTS=243 -dDEVICEHEIGHTPOINTS=155 -dPDFFitPage -sDEVICE=pbmraw -sOutputFile=$pbm -f $pdf";
74 }
75
76 system "pdftk $out.front.pdf $out.back.pdf cat output $out.duplex.pdf" if $ENV{DUPLEX};
77
78 __END__
79
80 #system "inkscape --file $out.print.svg  --export-pdf $out.pdf";
81
82 system "inkscape --file $out.print.svg --export-area-page --export-pdf $out.print-front.pdf --export-id print-front";
83 system "inkscape --file $out.print.svg --export-area-page --export-pdf $out.print-back.pdf --export-id print-back";
84 system "pdftk  $out.print-front.pdf $out.print-back.pdf cat output $out.print-duplex.pdf";
85
86 #system "inkscape --file $out.screen.svg --export-png $out.png --export-dpi 180";
87
88 #system "inkscape --file $out.screen.svg --export-png $out.300.png --export-dpi 300";
89
90 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";
91 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";
92
93
94 #system "qiv $out.png";
95 #system "xpdf $out.pdf";