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