*** empty log message ***
[koha.git] / barcodes / label-print-pdf.pl
1 #!/usr/bin/perl
2
3 #use strict;
4 use CGI;
5 use C4::Auth;
6 use C4::Output;
7 use C4::Interface::CGI::Output;
8 use C4::Context;
9 use HTML::Template;
10 use PDF::Reuse;
11 use PDF::Reuse::Barcode;
12 use PDF::Report;
13
14 my $htdocs_path = C4::Context->config('intrahtdocs');
15 my $cgi         = new CGI;
16
17 my $spine_text = "";
18
19 #--------------------------------------------------------
20 # get the printing settings
21
22 my $dbh    = C4::Context->dbh;
23 my $query2 = " SELECT * FROM labels_conf LIMIT 1 ";
24 my $sth    = $dbh->prepare($query2);
25 $sth->execute();
26
27 my $conf_data = $sth->fetchrow_hashref;
28
29 # get barcode type from $conf_data
30 my $barcodetype = $conf_data->{'barcodetype'};
31 my $startrow    = $conf_data->{'startrow'};
32
33 $sth->finish;
34
35 #------------------
36
37 # get the actual items to be printed.
38 my @data;
39 my $query3 = " Select * from labels ";
40 my $sth    = $dbh->prepare($query3);
41 $sth->execute();
42 my @resultsloop;
43 my $cnt = $sth->rows;
44 my $i1  = 1;
45 while ( my $data = $sth->fetchrow_hashref ) {
46
47     # lets get some summary info from each item
48     my $query1 =
49       " select *from biblio, biblioitems, items where itemnumber = ? and
50                                 items.biblioitemnumber=biblioitems.biblioitemnumber and
51                                 biblioitems.biblionumber=biblio.biblionumber";
52
53     my $sth1 = $dbh->prepare($query1);
54     $sth1->execute( $data->{'itemnumber'} );
55     my $data1 = $sth1->fetchrow_hashref();
56
57     push( @resultsloop, $data1 );
58     $sth1->finish;
59
60     $i1++;
61 }
62 $sth->finish;
63
64 # dimensions of gaylord paper
65 my $lowerLeftX  = 0;
66 my $lowerLeftY  = 0;
67 my $upperRightX = 612;
68 my $upperRightY = 792;
69
70 #----------------------------------
71 # setting up the pdf doc
72
73 prFile("$htdocs_path/barcodes/new.pdf");
74 prLogDir("$htdocs_path/barcodes");
75
76 #prMbox ( $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY );
77 prMbox( 0, 0, 612, 792 );
78
79 prFont('Times-Roman');    # Just setting a font
80 prFontSize(10);
81
82 my $margin = 36;
83
84 my $label_height = 90;
85 my $spine_width  = 72;
86 my $circ_width   = 207;
87 my $colspace     = 27;
88
89 my $x_pos_spine = 36;
90 my $x_pos_circ1 = 135;
91 my $x_pos_circ2 = 369;
92
93 my $pageheight = 792;
94
95 my $y_pos_initial = ( ( $pageheight - $margin ) - $label_height );
96 my $y_pos_initial_startrow =
97   ( ( $pageheight - $margin ) - ( $label_height * $startrow ) );
98
99 my $y_pos_initial = ( ( 792 - 36 ) - 90 );
100
101 my $y_pos = $y_pos_initial_startrow;
102
103 #my $y_pos            = $y_pos_initial;
104 my $rowspace         = 36;
105 my $page_break_count = $startrow;
106 my $codetype         = 'Code39';
107
108 # do border---------------
109 my $str = "q\n";    # save the graphic state
110 $str .= "4 w\n";                # border color red
111 $str .= "0.0 0.0 0.0  RG\n";    # border color red
112 $str .= "1 1 1 rg\n";           # fill color blue
113 $str .= "0 0 612 792 re\n";     # a rectangle
114 $str .= "B\n";                  # fill (and a little more)
115 $str .= "Q\n";                  # save the graphic state
116
117 # do border---------------
118
119 prAdd($str);
120 my $item;
121
122 my $i2 = 1;
123 foreach $item (@resultsloop) {
124     if ( $i2 == 1 ) {
125
126         #draw_boxes();
127     }
128
129     #building up spine text
130     my $line        = 75;
131     my $line_spacer = 16;
132
133     build_circ_barcode( $x_pos_circ1, $y_pos, $item->{'barcode'},
134         $conf_data->{'barcodetype'} );
135     build_circ_barcode( $x_pos_circ2, $y_pos, $item->{'barcode'},
136         $conf_data->{'barcodetype'} );
137
138 # added for xpdf compat. doesnt use type3 fonts., but increases filesize from 20k to 200k
139 # i think its embedding extra fonts in the pdf file.
140 #       mode => 'graphic',
141
142     $y_pos = ( $y_pos - $label_height );
143
144     # the gaylord labels have 8 rows per sheet, this pagebreaks after 8 rows
145     if ( $page_break_count == 8 ) {
146         prPage();
147
148         $page_break_count = 0;
149         $i2               = 0;
150         $y_pos            = $y_pos_initial;
151     }
152     $page_break_count++;
153     $i2++;
154 }
155
156 prEnd();
157
158 #----------------------------------------------------------------------------
159
160
161 $file = "$htdocs_path/barcodes/new.pdf";
162
163 my $pdf = new PDF::Report( File => $file );
164
165 # my $pdf = new PDF::Report(PageSize => "letter",
166 #                                  PageOrientation => "Landscape");
167
168 #$pdf->newpage($nopage);
169 my $pagenumber = 1;
170 $pdf->openpage($pagenumber);
171
172 ( $pagewidth, $pageheight ) = $pdf->getPageDimensions();
173 my $y_pos = ( $y_pos_initial_startrow + 90 );
174
175 # this left aligns the spine label text, centering would be better, 
176 # but word-wrapping breaks with centering on. :(
177 $pdf->setAlign('left');
178
179 # this sets the font size for the spine label text.
180 # if your text strings are extending past the right label border
181 # and arnt getting wrapped properly, reduce the font size some
182 # fyi: PDF::Report wont wrap continuious strings with no spaces in them, correctly
183
184 $pdf->setSize(9);
185
186
187 my $page_break_count = $startrow;
188
189 foreach $item (@resultsloop) {
190
191     my $firstrow = 0;
192
193     $pdf->setAddTextPos( 36, ( $y_pos - 15 ) );    # INIT START POS
194     ( $hPos, $vPos )  = $pdf->getAddTextPos();
195     ( $hPos, $vPos1 ) = $pdf->getAddTextPos();
196
197     if ( $conf_data->{'dewey'} && $item->{'dewey'} ) {
198
199         ( $hPos, $vPos1 ) = $pdf->getAddTextPos();
200         $pdf->addText( $item->{'dewey'}, 10, 72, 90 );
201         ( $hPos, $vPos1 ) = $pdf->getAddTextPos();
202         $firstrow = 1;
203     }
204
205     if ( $conf_data->{'isbn'} && $item->{'isbn'} ) {
206         if ( $vPos1 == $vPos && $firstrow != 0 ) {
207             $pdf->setAddTextPos( 36, ( $vPos - 15 ) );
208         }
209         else {
210             $pdf->setAddTextPos( 36, $vPos1 - 5 );    #add a space
211         }
212
213         ( $hPos, $vPos ) = $pdf->getAddTextPos();
214         $pdf->addText( $item->{'isbn'}, 10, 72, 90 );
215         ( $hPos, $vPos1 ) = $pdf->getAddTextPos();
216         $firstrow = 1;
217     }
218
219     if ( $conf_data->{'class'} && $item->{'classification'} ) {
220
221         if ( $vPos1 == $vPos && $firstrow != 0 ) {
222             $pdf->setAddTextPos( 36, ( $vPos - 15 ) );
223         }
224         else {
225             $pdf->setAddTextPos( 36, $vPos1 - 5 );    #add a space
226         }
227
228         ( $hPos, $vPos ) = $pdf->getAddTextPos();
229         $pdf->addText( $item->{'classification'}, 10, 72, 90 );
230         ( $hPos, $vPos1 ) = $pdf->getAddTextPos();
231         $firstrow = 1;
232     }
233
234     if ( $conf_data->{'itemtype'} && $item->{'itemtype'} ) {
235
236         if ( $vPos1 == $vPos && $firstrow != 0 ) {
237             $pdf->setAddTextPos( 36, ( $vPos - 15 ) );
238         }
239         else {
240             $pdf->setAddTextPos( 36, $vPos1 - 5 );    #add a space
241         }
242
243         ( $hPos, $vPos ) = $pdf->getAddTextPos();
244         $pdf->addText( $item->{'itemtype'}, 10, 72, 90 );
245         ( $hPos, $vPos1 ) = $pdf->getAddTextPos();
246         $firstrow = 1;
247     }
248
249     #$pdf->drawRect(
250     #    $x_pos_spine, $y_pos,
251     #    ( $x_pos_spine + $spine_width ),
252     #    ( $y_pos - $label_height )
253     #);
254
255     $y_pos = ( $y_pos - $label_height );
256     if ( $page_break_count == 8 ) {
257         $pagenumber++;
258         $pdf->openpage($pagenumber);
259
260         $page_break_count = 0;
261         $i2               = 0;
262         $y_pos            = ( $y_pos_initial + 90 );
263     }
264
265     $page_break_count++;
266     $i2++;
267
268 }
269 $DB::single = 1;
270 $pdf->saveAs($file);
271
272 #------------------------------------------------
273
274 print $cgi->redirect("/intranet-tmpl/barcodes/new.pdf");
275
276 # draw boxes------------------
277 sub draw_boxes {
278
279     my $y_pos_initial = ( ( 792 - 36 ) - 90 );
280     my $y_pos         = $y_pos_initial;
281     my $i             = 1;
282
283     for ( $i = 1 ; $i <= 8 ; $i++ ) {
284
285         &drawbox( $x_pos_spine, $y_pos, ($spine_width), ($label_height) );
286
287         &drawbox( $x_pos_circ1, $y_pos, ($circ_width), ($label_height) );
288         &drawbox( $x_pos_circ2, $y_pos, ($circ_width), ($label_height) );
289
290         $y_pos = ( $y_pos - $label_height );
291
292     }
293 }
294
295 # draw boxes------------------
296
297 sub build_circ_barcode {
298     my ( $x_pos_circ, $y_pos, $value, $barcodetype ) = @_;
299
300     #$DB::single = 1;
301
302     if ( $barcodetype eq 'EAN13' ) {
303         eval {
304             PDF::Reuse::Barcode::EAN13(
305                 x     => ( $x_pos_circ + 32 ),
306                 y     => ( $y_pos + 18 ),
307                 value => $value,
308
309                 #            prolong => 2.96,
310                 xSize   => 1.5,
311                 ySize   => 1.2,
312             );
313         };
314         if ($@) {
315             $item->{'barcodeerror'} = 1;
316         }
317
318     }
319     elsif ( $barcodetype eq 'Code39' ) {
320
321         eval {
322             PDF::Reuse::Barcode::Code39(
323                 x     => ( $x_pos_circ + 9 ),
324                 y     => ( $y_pos + 15 ),
325                 value => "*$value*",
326                 hide_asterisk => 1,
327                 #           prolong => 2.96,
328                 xSize => .85,
329                 ySize => 1.3,
330             );
331         };
332         if ($@) {
333             $item->{'barcodeerror'} = 1;
334         }
335     }
336
337     elsif ( $barcodetype eq 'Matrix2of5' ) {
338
339         eval {
340             PDF::Reuse::Barcode::Matrix2of5(
341                 x     => ( $x_pos_circ + 27 ),
342                 y     => ( $y_pos + 15 ),
343                 value => $value,
344
345                 #        prolong => 2.96,
346                 #       xSize   => 1.5,
347
348                 # ySize   => 1.2,
349             );
350         };
351         if ($@) {
352             $item->{'barcodeerror'} = 1;
353         }
354     }
355
356     elsif ( $barcodetype eq 'EAN8' ) {
357
358         eval {
359             PDF::Reuse::Barcode::EAN8(
360                 x       => ( $x_pos_circ + 42 ),
361                 y       => ( $y_pos + 15 ),
362                 value   => $value,
363                 prolong => 2.96,
364                 xSize   => 1.5,
365
366                 # ySize   => 1.2,
367             );
368         };
369
370         if ($@) {
371             $item->{'barcodeerror'} = 1;
372         }
373
374     }
375
376     elsif ( $barcodetype eq 'UPC-E' ) {
377         eval {
378             PDF::Reuse::Barcode::UPCE(
379                 x       => ( $x_pos_circ + 27 ),
380                 y       => ( $y_pos + 15 ),
381                 value   => $value,
382                 prolong => 2.96,
383                 xSize   => 1.5,
384
385                 # ySize   => 1.2,
386             );
387         };
388
389         if ($@) {
390             $item->{'barcodeerror'} = 1;
391         }
392     }
393     elsif ( $barcodetype eq 'NW7' ) {
394         eval {
395             PDF::Reuse::Barcode::NW7(
396                 x       => ( $x_pos_circ + 27 ),
397                 y       => ( $y_pos + 15 ),
398                 value   => $value,
399                 prolong => 2.96,
400                 xSize   => 1.5,
401
402                 # ySize   => 1.2,
403             );
404         };
405
406         if ($@) {
407             $item->{'barcodeerror'} = 1;
408         }
409     }
410     elsif ( $barcodetype eq 'ITF' ) {
411         eval {
412             PDF::Reuse::Barcode::ITF(
413                 x       => ( $x_pos_circ + 27 ),
414                 y       => ( $y_pos + 15 ),
415                 value   => $value,
416                 prolong => 2.96,
417                 xSize   => 1.5,
418
419                 # ySize   => 1.2,
420             );
421         };
422
423         if ($@) {
424             $item->{'barcodeerror'} = 1;
425         }
426     }
427     elsif ( $barcodetype eq 'Industrial2of5' ) {
428         eval {
429             PDF::Reuse::Barcode::Industrial2of5(
430                 x       => ( $x_pos_circ + 27 ),
431                 y       => ( $y_pos + 15 ),
432                 value   => $value,
433                 prolong => 2.96,
434                 xSize   => 1.5,
435
436                 # ySize   => 1.2,
437             );
438         };
439         if ($@) {
440             $item->{'barcodeerror'} = 1;
441         }
442     }
443     elsif ( $barcodetype eq 'IATA2of5' ) {
444         eval {
445             PDF::Reuse::Barcode::IATA2of5(
446                 x       => ( $x_pos_circ + 27 ),
447                 y       => ( $y_pos + 15 ),
448                 value   => $value,
449                 prolong => 2.96,
450                 xSize   => 1.5,
451
452                 # ySize   => 1.2,
453             );
454         };
455         if ($@) {
456             $item->{'barcodeerror'} = 1;
457         }
458
459     }
460
461     elsif ( $barcodetype eq 'COOP2of5' ) {
462         eval {
463             PDF::Reuse::Barcode::COOP2of5(
464                 x       => ( $x_pos_circ + 27 ),
465                 y       => ( $y_pos + 15 ),
466                 value   => $value,
467                 prolong => 2.96,
468                 xSize   => 1.5,
469
470                 # ySize   => 1.2,
471             );
472         };
473         if ($@) {
474             $item->{'barcodeerror'} = 1;
475         }
476     }
477     elsif ( $barcodetype eq 'UPC-A' ) {
478
479         eval {
480             PDF::Reuse::Barcode::UPCA(
481                 x       => ( $x_pos_circ + 27 ),
482                 y       => ( $y_pos + 15 ),
483                 value   => $value,
484                 prolong => 2.96,
485                 xSize   => 1.5,
486
487                 # ySize   => 1.2,
488             );
489         };
490         if ($@) {
491             $item->{'barcodeerror'} = 1;
492         }
493     }
494 }
495
496 #-----------------------------
497
498 sub drawbox {
499     my ( $llx, $lly, $urx, $ury ) = @_;
500
501     my $str = "q\n";    # save the graphic state
502     $str .= "1.0 0.0 0.0  RG\n";           # border color red
503     $str .= "1 1 1  rg\n";                 # fill color blue
504     $str .= "$llx $lly $urx $ury re\n";    # a rectangle
505     $str .= "B\n";                         # fill (and a little more)
506     $str .= "Q\n";                         # save the graphic state
507
508     prAdd($str);
509
510 }
511