removed pdf::table declaration, not needed anymore.
[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 $pdf->setAlign('left');
175 $pdf->setSize(9);
176
177 my $page_break_count = $startrow;
178
179 foreach $item (@resultsloop) {
180
181     my $firstrow = 0;
182
183     $pdf->setAddTextPos( 36, ( $y_pos - 15 ) );    # INIT START POS
184     ( $hPos, $vPos )  = $pdf->getAddTextPos();
185     ( $hPos, $vPos1 ) = $pdf->getAddTextPos();
186
187     if ( $conf_data->{'dewey'} && $item->{'dewey'} ) {
188
189         ( $hPos, $vPos1 ) = $pdf->getAddTextPos();
190         $pdf->addText( $item->{'dewey'}, 10, 72, 90 );
191         ( $hPos, $vPos1 ) = $pdf->getAddTextPos();
192         $firstrow = 1;
193     }
194
195     if ( $conf_data->{'isbn'} && $item->{'isbn'} ) {
196         if ( $vPos1 == $vPos && $firstrow != 0 ) {
197             $pdf->setAddTextPos( 36, ( $vPos - 15 ) );
198         }
199         else {
200             $pdf->setAddTextPos( 36, $vPos1 - 5 );    #add a space
201         }
202
203         ( $hPos, $vPos ) = $pdf->getAddTextPos();
204         $pdf->addText( $item->{'isbn'}, 10, 72, 90 );
205         ( $hPos, $vPos1 ) = $pdf->getAddTextPos();
206         $firstrow = 1;
207     }
208
209     if ( $conf_data->{'class'} && $item->{'classification'} ) {
210
211         if ( $vPos1 == $vPos && $firstrow != 0 ) {
212             $pdf->setAddTextPos( 36, ( $vPos - 15 ) );
213         }
214         else {
215             $pdf->setAddTextPos( 36, $vPos1 - 5 );    #add a space
216         }
217
218         ( $hPos, $vPos ) = $pdf->getAddTextPos();
219         $pdf->addText( $item->{'classification'}, 10, 72, 90 );
220         ( $hPos, $vPos1 ) = $pdf->getAddTextPos();
221         $firstrow = 1;
222     }
223
224     if ( $conf_data->{'itemtype'} && $item->{'itemtype'} ) {
225
226         if ( $vPos1 == $vPos && $firstrow != 0 ) {
227             $pdf->setAddTextPos( 36, ( $vPos - 15 ) );
228         }
229         else {
230             $pdf->setAddTextPos( 36, $vPos1 - 5 );    #add a space
231         }
232
233         ( $hPos, $vPos ) = $pdf->getAddTextPos();
234         $pdf->addText( $item->{'itemtype'}, 10, 72, 90 );
235         ( $hPos, $vPos1 ) = $pdf->getAddTextPos();
236         $firstrow = 1;
237     }
238
239     #$pdf->drawRect(
240     #    $x_pos_spine, $y_pos,
241     #    ( $x_pos_spine + $spine_width ),
242     #    ( $y_pos - $label_height )
243     #);
244
245     $y_pos = ( $y_pos - $label_height );
246     if ( $page_break_count == 8 ) {
247         $pagenumber++;
248         $pdf->openpage($pagenumber);
249
250         $page_break_count = 0;
251         $i2               = 0;
252         $y_pos            = ( $y_pos_initial + 90 );
253     }
254
255     $page_break_count++;
256     $i2++;
257
258 }
259 $DB::single = 1;
260 $pdf->saveAs($file);
261
262 #------------------------------------------------
263
264 print $cgi->redirect("/intranet-tmpl/barcodes/new.pdf");
265
266 # draw boxes------------------
267 sub draw_boxes {
268
269     my $y_pos_initial = ( ( 792 - 36 ) - 90 );
270     my $y_pos         = $y_pos_initial;
271     my $i             = 1;
272
273     for ( $i = 1 ; $i <= 8 ; $i++ ) {
274
275         &drawbox( $x_pos_spine, $y_pos, ($spine_width), ($label_height) );
276
277         &drawbox( $x_pos_circ1, $y_pos, ($circ_width), ($label_height) );
278         &drawbox( $x_pos_circ2, $y_pos, ($circ_width), ($label_height) );
279
280         $y_pos = ( $y_pos - $label_height );
281
282     }
283 }
284
285 # draw boxes------------------
286
287 sub build_circ_barcode {
288     my ( $x_pos_circ, $y_pos, $value, $barcodetype ) = @_;
289
290     #$DB::single = 1;
291
292     if ( $barcodetype eq 'EAN13' ) {
293         eval {
294             PDF::Reuse::Barcode::EAN13(
295                 x     => ( $x_pos_circ + 32 ),
296                 y     => ( $y_pos + 18 ),
297                 value => $value,
298
299                 #            prolong => 2.96,
300                 xSize   => 1.5,
301                 ySize   => 1.2,
302             );
303         };
304         if ($@) {
305             $item->{'barcodeerror'} = 1;
306         }
307
308     }
309     elsif ( $barcodetype eq 'Code39' ) {
310
311         eval {
312             PDF::Reuse::Barcode::Code39(
313                 x     => ( $x_pos_circ + 9 ),
314                 y     => ( $y_pos + 15 ),
315                 value => "*$value*",
316                 hide_asterisk => 1,
317                 #           prolong => 2.96,
318                 xSize => .85,
319                 ySize => 1.3,
320             );
321         };
322         if ($@) {
323             $item->{'barcodeerror'} = 1;
324         }
325     }
326
327     elsif ( $barcodetype eq 'Matrix2of5' ) {
328
329         eval {
330             PDF::Reuse::Barcode::Matrix2of5(
331                 x     => ( $x_pos_circ + 27 ),
332                 y     => ( $y_pos + 15 ),
333                 value => $value,
334
335                 #        prolong => 2.96,
336                 #       xSize   => 1.5,
337
338                 # ySize   => 1.2,
339             );
340         };
341         if ($@) {
342             $item->{'barcodeerror'} = 1;
343         }
344     }
345
346     elsif ( $barcodetype eq 'EAN8' ) {
347
348         eval {
349             PDF::Reuse::Barcode::EAN8(
350                 x       => ( $x_pos_circ + 42 ),
351                 y       => ( $y_pos + 15 ),
352                 value   => $value,
353                 prolong => 2.96,
354                 xSize   => 1.5,
355
356                 # ySize   => 1.2,
357             );
358         };
359
360         if ($@) {
361             $item->{'barcodeerror'} = 1;
362         }
363
364     }
365
366     elsif ( $barcodetype eq 'UPC-E' ) {
367         eval {
368             PDF::Reuse::Barcode::UPCE(
369                 x       => ( $x_pos_circ + 27 ),
370                 y       => ( $y_pos + 15 ),
371                 value   => $value,
372                 prolong => 2.96,
373                 xSize   => 1.5,
374
375                 # ySize   => 1.2,
376             );
377         };
378
379         if ($@) {
380             $item->{'barcodeerror'} = 1;
381         }
382     }
383     elsif ( $barcodetype eq 'NW7' ) {
384         eval {
385             PDF::Reuse::Barcode::NW7(
386                 x       => ( $x_pos_circ + 27 ),
387                 y       => ( $y_pos + 15 ),
388                 value   => $value,
389                 prolong => 2.96,
390                 xSize   => 1.5,
391
392                 # ySize   => 1.2,
393             );
394         };
395
396         if ($@) {
397             $item->{'barcodeerror'} = 1;
398         }
399     }
400     elsif ( $barcodetype eq 'ITF' ) {
401         eval {
402             PDF::Reuse::Barcode::ITF(
403                 x       => ( $x_pos_circ + 27 ),
404                 y       => ( $y_pos + 15 ),
405                 value   => $value,
406                 prolong => 2.96,
407                 xSize   => 1.5,
408
409                 # ySize   => 1.2,
410             );
411         };
412
413         if ($@) {
414             $item->{'barcodeerror'} = 1;
415         }
416     }
417     elsif ( $barcodetype eq 'Industrial2of5' ) {
418         eval {
419             PDF::Reuse::Barcode::Industrial2of5(
420                 x       => ( $x_pos_circ + 27 ),
421                 y       => ( $y_pos + 15 ),
422                 value   => $value,
423                 prolong => 2.96,
424                 xSize   => 1.5,
425
426                 # ySize   => 1.2,
427             );
428         };
429         if ($@) {
430             $item->{'barcodeerror'} = 1;
431         }
432     }
433     elsif ( $barcodetype eq 'IATA2of5' ) {
434         eval {
435             PDF::Reuse::Barcode::IATA2of5(
436                 x       => ( $x_pos_circ + 27 ),
437                 y       => ( $y_pos + 15 ),
438                 value   => $value,
439                 prolong => 2.96,
440                 xSize   => 1.5,
441
442                 # ySize   => 1.2,
443             );
444         };
445         if ($@) {
446             $item->{'barcodeerror'} = 1;
447         }
448
449     }
450
451     elsif ( $barcodetype eq 'COOP2of5' ) {
452         eval {
453             PDF::Reuse::Barcode::COOP2of5(
454                 x       => ( $x_pos_circ + 27 ),
455                 y       => ( $y_pos + 15 ),
456                 value   => $value,
457                 prolong => 2.96,
458                 xSize   => 1.5,
459
460                 # ySize   => 1.2,
461             );
462         };
463         if ($@) {
464             $item->{'barcodeerror'} = 1;
465         }
466     }
467     elsif ( $barcodetype eq 'UPC-A' ) {
468
469         eval {
470             PDF::Reuse::Barcode::UPCA(
471                 x       => ( $x_pos_circ + 27 ),
472                 y       => ( $y_pos + 15 ),
473                 value   => $value,
474                 prolong => 2.96,
475                 xSize   => 1.5,
476
477                 # ySize   => 1.2,
478             );
479         };
480         if ($@) {
481             $item->{'barcodeerror'} = 1;
482         }
483     }
484 }
485
486 #-----------------------------
487
488 sub drawbox {
489     my ( $llx, $lly, $urx, $ury ) = @_;
490
491     my $str = "q\n";    # save the graphic state
492     $str .= "1.0 0.0 0.0  RG\n";           # border color red
493     $str .= "1 1 1  rg\n";                 # fill color blue
494     $str .= "$llx $lly $urx $ury re\n";    # a rectangle
495     $str .= "B\n";                         # fill (and a little more)
496     $str .= "Q\n";                         # save the graphic state
497
498     prAdd($str);
499
500 }
501