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