use Bull; changed by use Serials;
[koha.git] / barcodes / label-print-pdf.pl
1 #!/usr/bin/perl
2
3 #----------------------------------------------------------------------
4 # this script is really divided into 2 differenvt section,
5
6 # the first section creates, and defines the new PDF file the barcodes
7 # using PDF::Reuse::Barcode, then saves the file to disk.
8
9 # the second section then opens the pdf file off disk, and places the spline label
10 # text in the left-most column of the page. then save the file again.
11
12 # the reason for this goofyness, it that i couldnt find a single perl package that handled both barcodes and decent text placement.
13
14 #use lib '/usr/local/hlt/intranet/modules';
15 #use C4::Context("/etc/koha-hlt.conf");
16
17 #use strict;
18 use CGI;
19 use C4::Labels;
20 use C4::Auth;
21 use C4::Serials;
22 use C4::Output;
23 use C4::Interface::CGI::Output;
24 use C4::Context;
25 use HTML::Template;
26 use PDF::Reuse;
27 use PDF::Reuse::Barcode;
28 use PDF::Report;
29 use Data::Dumper;
30
31 #use Acme::Comment;
32 #use Data::Dumper;
33
34 my $htdocs_path = C4::Context->config('intrahtdocs');
35 my $cgi         = new CGI;
36
37 my $spine_text = "";
38
39 # get the printing settings
40
41 my $conf_data   = get_label_options();
42 my @resultsloop = get_label_items();
43
44 warn Dumper $conf_data;
45
46
47 my $barcodetype  = $conf_data->{'barcodetype'};
48 my $printingtype = $conf_data->{'printingtype'};
49 my $guidebox  = $conf_data->{'guidebox'};
50 my $startrow     = $conf_data->{'startrow'};
51
52 if (!$printingtype) {
53         $printingtype = 'both';
54 }
55
56 warn $printingtype;
57 warn $guidebox;
58
59
60 #warn Dumper @resultsloop;
61
62 # dimensions of gaylord paper
63 my $lowerLeftX  = 0;
64 my $lowerLeftY  = 0;
65 my $upperRightX = 612;
66 my $upperRightY = 792;
67
68 #----------------------------------
69 # setting up the pdf doc
70
71 #remove the file before write, for testing
72 unlink "$htdocs_path/barcodes/new.pdf";
73
74 prFile("$htdocs_path/barcodes/new.pdf");
75 prLogDir("$htdocs_path/barcodes");
76
77 #prMbox ( $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY );
78 prMbox( 0, 0, 612, 792 );
79
80 prFont('Times-Roman');    # Just setting a font
81 prFontSize(10);
82
83 my $margin = 36;
84
85 my $label_height = 90;
86 my $spine_width  = 72;
87 my $circ_width   = 207;
88 my $colspace     = 27;
89
90 my $x_pos_spine = 36;
91 my $x_pos_circ1 = 135;
92 my $x_pos_circ2 = 369;
93
94 my $pageheight = 792;
95
96 warn "STARTROW = $startrow\n";
97
98 #my $y_pos_initial = ( ( 792 - 36 ) - 90 );
99 my $y_pos_initial = ( ( $pageheight - $margin ) - $label_height );
100 my $y_pos_initial_startrow =
101   ( ( $pageheight - $margin ) - ( $label_height * $startrow ) );
102
103 my $y_pos = $y_pos_initial_startrow;
104
105 warn "Y POS INITAL : $y_pos_initial";
106 warn "Y POS : $y_pos";
107 warn "Y START ROW = $y_pos_initial_startrow";
108
109 my $rowspace         = 36;
110 my $page_break_count = $startrow;
111 my $codetype         = 'Code39';
112
113 # do border---------------
114 my $str = "q\n";    # save the graphic state
115 $str .= "4 w\n";                # border color red
116 $str .= "0.0 0.0 0.0  RG\n";    # border color red
117 $str .= "1 1 1 rg\n";           # fill color blue
118 $str .= "0 0 612 792 re\n";     # a rectangle
119 $str .= "B\n";                  # fill (and a little more)
120 $str .= "Q\n";                  # save the graphic state
121
122 # do border---------------
123
124 prAdd($str);
125 my $item;
126
127 # for loop
128
129 my $i2 = 1;
130 foreach $item (@resultsloop) {
131     if ( $i2 == 1  && $guidebox  == 1) {
132         draw_boundaries(
133             $x_pos_spine, $x_pos_circ1,  $x_pos_circ2, $y_pos,
134             $spine_width, $label_height, $circ_width
135         );
136     }
137
138     #warn Dumper $item->{'itemtype'};
139     #warn "COUNT = $cnt1";
140
141     #building up spine text
142     my $line        = 75;
143     my $line_spacer = 16;
144
145     $DB::single = 1;
146
147     warn
148 "COUNT=$i2, PBREAKCNT=$page_break_count, X,Y POS x=$x_pos_circ1, y=$y_pos";
149  if ( $printingtype eq 'barcode' || $printingtype eq 'both' ) {
150     build_circ_barcode( $x_pos_circ1, $y_pos, $item->{'barcode'},
151         $conf_data->{'barcodetype'}, \$item );
152     build_circ_barcode( $x_pos_circ2, $y_pos, $item->{'barcode'},
153         $conf_data->{'barcodetype'}, \$item );
154 }
155 # added for xpdf compat. doesnt use type3 fonts., but increases filesize from 20k to 200k
156 # i think its embedding extra fonts in the pdf file.
157 #       mode => 'graphic',
158
159     $y_pos = ( $y_pos - $label_height );
160
161     # the gaylord labels have 8 rows per sheet, this pagebreaks after 8 rows
162     if ( $page_break_count == 8 ) {
163         prPage();
164
165         #warn "############# PAGEBREAK ###########";
166         $page_break_count = 0;
167         $i2               = 0;
168         $y_pos            = $y_pos_initial;
169     }
170     $page_break_count++;
171     $i2++;
172 }
173 ############## end of loop
174
175
176 prEnd();
177
178 #----------------------------------------------------------------------------
179 # this second section of the script uses a diff perl class than the previous section
180 # it opens the 'new.pdf' file that the previous section has just saved
181
182 if ( $printingtype eq 'spine' || $printingtype eq 'both' ) {
183
184     $file = "$htdocs_path/barcodes/new.pdf";
185
186     my $pdf = new PDF::Report( File => $file );
187
188     # my $pdf = new PDF::Report(PageSize => "letter",
189     #                                  PageOrientation => "Landscape");
190
191     #$pdf->newpage($nopage);
192     my $pagenumber = 1;
193     $pdf->openpage($pagenumber);
194
195     ( $pagewidth, $pageheight ) = $pdf->getPageDimensions();
196
197     #warn "PAGE DIM = $pagewidth, $pageheight";
198     #warn "Y START ROW = $y_pos_initial_startrow";
199     my $y_pos = ( $y_pos_initial_startrow + 90 );
200
201     #my $y_pos = ( $y_pos_initial_startrow  );
202     #warn "Y POS = $y_pos";
203
204     # now needed now we are using centerString().
205     #$pdf->setAlign('left');
206     
207     # SET THE FONT SIZE
208     $pdf->setSize(9);
209
210     my $page_break_count = $startrow;
211
212     #warn "INIT PAGEBREAK COUNT = $page_break_count";
213
214     #warn "#----------------------------------\n";
215     #warn "INIT VPOS = $vPos, hPos = $hPos";
216
217     my $vPosSpacer     = 15;
218     my $start_text_pos = 39;   # ( 36 - 5 = 31 ) 5 is an inside border for text.
219     my $spine_label_text_with = 67;
220
221     foreach $item (@resultsloop) {
222
223         #warn Dumper $item;
224         #warn "START Y_POS=$y_pos";
225         my $firstrow = 0;
226
227         $pdf->setAddTextPos( $start_text_pos, ( $y_pos - 20 ) )
228           ;                    # INIT START POS
229         ( $hPos, $vPos ) = $pdf->getAddTextPos();
230
231         my $hPosEnd = ( $hPos + $spine_label_text_with );    # 72
232         if ( $conf_data->{'dewey'} && $item->{'dewey'} ) {
233             ( $hPos, $vPos1 ) = $pdf->getAddTextPos();
234             $pdf->centerString( $hPos, $hPosEnd, $vPos, $item->{'dewey'} );
235             $vPos = $vPos - $vPosSpacer;
236         }
237
238         if ( $conf_data->{'isbn'} && $item->{'isbn'} ) {
239             ( $hPos, $vPos1 ) = $pdf->getAddTextPos();
240             $pdf->centerString( $hPos, $hPosEnd, $vPos, $item->{'isbn'} );
241             $vPos = $vPos - $vPosSpacer;
242         }
243
244         if ( $conf_data->{'class'} && $item->{'classification'} ) {
245             ( $hPos, $vPos1 ) = $pdf->getAddTextPos();
246             $pdf->centerString( $hPos, $hPosEnd, $vPos,
247                 $item->{'classification'} );
248             $vPos = $vPos - $vPosSpacer;
249         }
250
251         if ( $conf_data->{'itemtype'} && $item->{'itemtype'} ) {
252             ( $hPos, $vPos1 ) = $pdf->getAddTextPos();
253             $pdf->centerString( $hPos, $hPosEnd, $vPos, $item->{'itemtype'} );
254             $vPos = $vPos - $vPosSpacer;
255         }
256
257         #$pdf->drawRect(
258         #    $x_pos_spine, $y_pos,
259         #    ( $x_pos_spine + $spine_width ),
260         #    ( $y_pos - $label_height )
261         #);
262
263         $y_pos = ( $y_pos - $label_height );
264
265         #warn "END LOOP Y_POS =$y_pos";
266         #    warn "PAGECOUNT END LOOP=$page_break_count";
267         if ( $page_break_count == 8 ) {
268             $pagenumber++;
269             $pdf->openpage($pagenumber);
270
271             #warn "############# PAGEBREAK ###########";
272             $page_break_count = 0;
273             $i2               = 0;
274             $y_pos            = ( $y_pos_initial + 90 );
275         }
276
277         $page_break_count++;
278         $i2++;
279
280         #warn "#----------------------------------\n";
281
282     }
283     $DB::single = 1;
284     $pdf->saveAs($file);
285 }
286
287 #------------------------------------------------
288
289 print $cgi->redirect("/intranet-tmpl/barcodes/new.pdf");