removed hardcoded template url with "$htdocs_path/barcodes/new.pdf".
[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 = "$htdocs_path/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         eval {
296             PDF::Reuse::Barcode::EAN13(
297                 x     => ( $x_pos_circ + 32 ),
298                 y     => ( $y_pos + 18 ),
299                 value => $value,
300
301                 #            prolong => 2.96,
302                 xSize   => 1.5,
303                 ySize   => 1.2,
304             );
305         };
306         if ($@) {
307             $item->{'barcodeerror'} = 1;
308         }
309
310     }
311     elsif ( $barcodetype eq 'Code39' ) {
312
313         eval {
314             PDF::Reuse::Barcode::Code39(
315                 x     => ( $x_pos_circ + 9 ),
316                 y     => ( $y_pos + 15 ),
317                 value => "*$value*",
318                 hide_asterisk => 1,
319                 #           prolong => 2.96,
320                 xSize => .85,
321                 ySize => 1.3,
322             );
323         };
324         if ($@) {
325             $item->{'barcodeerror'} = 1;
326         }
327     }
328
329     elsif ( $barcodetype eq 'Matrix2of5' ) {
330
331         eval {
332             PDF::Reuse::Barcode::Matrix2of5(
333                 x     => ( $x_pos_circ + 27 ),
334                 y     => ( $y_pos + 15 ),
335                 value => $value,
336
337                 #        prolong => 2.96,
338                 #       xSize   => 1.5,
339
340                 # ySize   => 1.2,
341             );
342         };
343         if ($@) {
344             $item->{'barcodeerror'} = 1;
345         }
346     }
347
348     elsif ( $barcodetype eq 'EAN8' ) {
349
350         eval {
351             PDF::Reuse::Barcode::EAN8(
352                 x       => ( $x_pos_circ + 42 ),
353                 y       => ( $y_pos + 15 ),
354                 value   => $value,
355                 prolong => 2.96,
356                 xSize   => 1.5,
357
358                 # ySize   => 1.2,
359             );
360         };
361
362         if ($@) {
363             $item->{'barcodeerror'} = 1;
364         }
365
366     }
367
368     elsif ( $barcodetype eq 'UPC-E' ) {
369         eval {
370             PDF::Reuse::Barcode::UPCE(
371                 x       => ( $x_pos_circ + 27 ),
372                 y       => ( $y_pos + 15 ),
373                 value   => $value,
374                 prolong => 2.96,
375                 xSize   => 1.5,
376
377                 # ySize   => 1.2,
378             );
379         };
380
381         if ($@) {
382             $item->{'barcodeerror'} = 1;
383         }
384     }
385     elsif ( $barcodetype eq 'NW7' ) {
386         eval {
387             PDF::Reuse::Barcode::NW7(
388                 x       => ( $x_pos_circ + 27 ),
389                 y       => ( $y_pos + 15 ),
390                 value   => $value,
391                 prolong => 2.96,
392                 xSize   => 1.5,
393
394                 # ySize   => 1.2,
395             );
396         };
397
398         if ($@) {
399             $item->{'barcodeerror'} = 1;
400         }
401     }
402     elsif ( $barcodetype eq 'ITF' ) {
403         eval {
404             PDF::Reuse::Barcode::ITF(
405                 x       => ( $x_pos_circ + 27 ),
406                 y       => ( $y_pos + 15 ),
407                 value   => $value,
408                 prolong => 2.96,
409                 xSize   => 1.5,
410
411                 # ySize   => 1.2,
412             );
413         };
414
415         if ($@) {
416             $item->{'barcodeerror'} = 1;
417         }
418     }
419     elsif ( $barcodetype eq 'Industrial2of5' ) {
420         eval {
421             PDF::Reuse::Barcode::Industrial2of5(
422                 x       => ( $x_pos_circ + 27 ),
423                 y       => ( $y_pos + 15 ),
424                 value   => $value,
425                 prolong => 2.96,
426                 xSize   => 1.5,
427
428                 # ySize   => 1.2,
429             );
430         };
431         if ($@) {
432             $item->{'barcodeerror'} = 1;
433         }
434     }
435     elsif ( $barcodetype eq 'IATA2of5' ) {
436         eval {
437             PDF::Reuse::Barcode::IATA2of5(
438                 x       => ( $x_pos_circ + 27 ),
439                 y       => ( $y_pos + 15 ),
440                 value   => $value,
441                 prolong => 2.96,
442                 xSize   => 1.5,
443
444                 # ySize   => 1.2,
445             );
446         };
447         if ($@) {
448             $item->{'barcodeerror'} = 1;
449         }
450
451     }
452
453     elsif ( $barcodetype eq 'COOP2of5' ) {
454         eval {
455             PDF::Reuse::Barcode::COOP2of5(
456                 x       => ( $x_pos_circ + 27 ),
457                 y       => ( $y_pos + 15 ),
458                 value   => $value,
459                 prolong => 2.96,
460                 xSize   => 1.5,
461
462                 # ySize   => 1.2,
463             );
464         };
465         if ($@) {
466             $item->{'barcodeerror'} = 1;
467         }
468     }
469     elsif ( $barcodetype eq 'UPC-A' ) {
470
471         eval {
472             PDF::Reuse::Barcode::UPCA(
473                 x       => ( $x_pos_circ + 27 ),
474                 y       => ( $y_pos + 15 ),
475                 value   => $value,
476                 prolong => 2.96,
477                 xSize   => 1.5,
478
479                 # ySize   => 1.2,
480             );
481         };
482         if ($@) {
483             $item->{'barcodeerror'} = 1;
484         }
485     }
486 }
487
488 #-----------------------------
489
490 sub drawbox {
491     my ( $llx, $lly, $urx, $ury ) = @_;
492
493     my $str = "q\n";    # save the graphic state
494     $str .= "1.0 0.0 0.0  RG\n";           # border color red
495     $str .= "1 1 1  rg\n";                 # fill color blue
496     $str .= "$llx $lly $urx $ury re\n";    # a rectangle
497     $str .= "B\n";                         # fill (and a little more)
498     $str .= "Q\n";                         # save the graphic state
499
500     prAdd($str);
501
502 }
503