CUFP
QuillMeantTen
<p>Greetings fellow monks,<br><br>
Yesterday my lady asked me if I could do her a favor, gallant fool that I am I answered at once<br><br>
"Of course, my paramour, it would be my honor to take on any quest, to slay any foe metaphorical or otherwise for your honor". For my defense the day before I forgot to make her lasagnas as promised and she was quite disappointed with my chicken and mushroom pie so that's why I did not ask what the task would be before accepting.<br><br>
A strange activity was asked of me : writing raffle tickets with nice images in such a way that they could be easily printed and then cut out as individual tickets.<br> As more than 3 hundreds of them were required by 6 pm the same day (someone had dumped the task on her without warning) she told me that using Excel was a solution but as it was computer related she hoped I knew some wizardry to accomplish the task in mere moments.<br><br>
So I grabbed my spear, shield, poleaxe, steed and my trusted GCC (gnu compiler cat, an universal syntax checker that will sit on my lap while I code and try to rip my throat if I move and sometimes when I make syntax mistakes)<br><br>
I soon came up with this idea :
Write it using latex, break the latex doc in discrete parts and then use a perl script to rearrange them as needed<br><br>
Here is the resulting script : you give it a path to the image you want, the number of tickets you want and the scale for the image and you should (hopefully) get some kind of result.<br>Also you will need pdflatex.
<br>
As usual, I'm looking for ways to get better so constructive criticism is welcome.
<br><br>
<b>Update: added autodie, thanks athanasius<br>use constant, rewrite the first loop, thanks anonymous monk, I'm going to read on the use of the DATA section</b><br><br>
<b>Aaaand another update, it's been a long time but I had to modify my script
so it could do better:<br>
Now you can pass more arguments: number of tickets in each column and output filename
</p>
<readmore><code>#!/usr/bin/perl -w
use strict;
use autodie;
use warnings;
use Carp qw(croak);
use Getopt::Std;
sub main{
my ($image,$number,$scale,$COLS) = @_;
my $header =
'\documentclass[12pt]{report}
\usepackage{graphicx}
\usepackage{array}
\usepackage{makecell}
\usepackage[T1]{fontenc}
\usepackage[margin=0.25in]{geometry}
\usepackage[utf8]{inputenc}
\begin{document}
';
my $footer = '\end{document}';
open my $tombolatex, '>','int.tex';
print $tombolatex $header;
my $body =
'\begin{tabular}{'.('|c|' x $COLS).'}
\hline'.
(('\thead{Ticket} &'."\n") x ($COLS-1)).
'\thead{Ticket}'."\n".'\\\\[6ex] \hline'.
(('\makecell{\includegraphics[scale=ISCALE]{image}\\\\ Ticket} &'."\n")x ($COLS-1)).
'\makecell{\includegraphics[scale=ISCALE]{image}\\\\ Ticket} \\\\[12ex] \hline
\end{tabular}
';
print $tombolatex $body x (1 + $number / $COLS);
print $tombolatex $footer;
close $tombolatex;
open $tombolatex, '>','res.tex';
open my $input,'<','int.tex';
my $tn = 1;
my $iter = 0;
my $find = 'Ticket';
$find = quotemeta $find;
while(<$input>){
my $replace = "Ticket $tn";
my $line= $_;
$line=~s/image/$image/g;
$line =~s/ISCALE/$scale/g;
if($line =~ s/$find/$replace/g){
if($tn % $COLS == 0){
if($iter == 1){
$tn++;
$iter = 0;
}
else{
$tn -= ($COLS-1);
$iter++;
}
}
else{
$tn++;
}
}
print $tombolatex $line;
}
}
our ($opt_i,#image switch
$opt_n,#number of tickets
$opt_s,#image_scale
$opt_c,#number of columns
$opt_o);#output filename
getopts('i:n:s:c:o:');
my @shortargs= ($opt_i,$opt_n,$opt_s,$opt_c,$opt_o);
if(!defined($shortargs[0])|| !defined
$shortargs[1] || !defined $shortargs[2]||
!defined $shortargs[3]||!defined $shortargs[4]){
croak <<"END"
-i image to put on tickets: $shortargs[0]
-n number of tickets: $shortargs[1]
-s image scale: $shortargs[2]
-c number of columns: $shortargs[3]
-o filename: $shortargs[4]
END
}
main @shortargs;
`pdflatex res.tex`;
unlink 'int.tex';
rename 'res.pdf', $shortargs[4];
my @res = glob "res.*";
unlink @res;</code></readmore>