make two-page pdf using pdftk
[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 Ime Prezime\n" unless @ARGV;
8
9 my ($card_svg,$nr,$ime,$prezime) = @ARGV;
10
11 warn "# svg: $card_svg nr: $nr $ime $prezime\n";
12
13 my $mapping = {
14 '200908109999' => $nr,
15 'Knjižničarko' => $ime,
16 'Čitalić' => $prezime,
17 };
18
19 sub mapping { $mapping->{ $_[0] } }
20
21 my $re = join('|', keys %$mapping);
22
23 mkdir 'out' unless -d 'out';
24 my $out = 'out/' . $nr;
25
26 foreach my $existing ( glob $out . '*' ) {
27         warn "# remove $existing ", -s $existing, " bytes\n";
28         unlink $existing;
29 }
30
31 open(my $svg_template, '<', $card_svg);
32 open(my $svg,          '>', "$out.svg");
33
34 while(<$svg_template>) {
35
36         if ( m{($re)} ) {
37                 warn "mapping $1\n";
38                 s{($1)}{mapping($1)}e;
39         }
40
41         print $svg $_;
42
43 }
44
45 close($svg_template);
46 close($svg);
47
48 open(my $inkscape, '|-', 'inkscape --shell --without-gui');
49
50 sub inkscape_export {
51         my $part = shift;
52
53         my $shell = "$out.svg --export-area-page --export-id $part";
54
55         $part =~ s/print-//; # FIXME change svg files
56
57         print $inkscape "$shell --export-pdf $out.$part.pdf\n";
58         print $inkscape "$shell --export-png $out.$part.png --export-dpi 150\n";
59 }
60
61 inkscape_export 'print-front';
62 inkscape_export 'print-back';
63
64 # export visible
65 print $inkscape "$out.svg --export-png $out.png --export-dpi 300\n";
66
67 close($inkscape);
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 -sDEVICE=pbmraw -sOutputFile=$pbm -f $pdf";
74 }
75
76 system "pdftk $out.front.pdf $out.back.pdf cat output $out.duplex.pdf";
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";