1#!/usr/bin/env perl 2use warnings; 3use strict; 4use diagnostics; 5use PDF::Table; 6 7# Please use TABSTOP=4 for best view 8# ------------- 9# -A or -B on command line to select preferred library (if available) 10# then look for PDFpref file and read A or B forms 11my ($PDFpref, $rcA, $rcB); # which is available? 12my $prefFile = "./PDFpref"; 13my $prefDefault = "B"; # PDF::Builder default if no prefFile, or both installed 14if (@ARGV) { 15 # A or -A argument: set PDFpref to A else B 16 if ($ARGV[0] =~ m/^-?([AB])/i) { 17 $PDFpref = uc($1); 18 } else { 19 print STDERR "Unknown command line flag $ARGV[0] ignored.\n"; 20 } 21} 22if (!defined $PDFpref) { 23 if (-f $prefFile && -r $prefFile) { 24 open my $FH, '<', $prefFile or die "error opening $prefFile: $!\n"; 25 $PDFpref = <$FH>; 26 if ($PDFpref =~ m/^A/i) { 27 # something starting with A, assume want PDF::API2 28 $PDFpref = 'A'; 29 } elsif ($PDFpref =~ m/^B/i) { 30 # something starting with B, assume want PDF::Builder 31 $PDFpref = 'B'; 32 } elsif ($PDFpref =~ m/^PDF:{1,2}A/i) { 33 # something starting with PDF:A or PDF::A, assume want PDF::API2 34 $PDFpref = 'A'; 35 } elsif ($PDFpref =~ m/^PDF:{1,2}B/i) { 36 # something starting with PDF:B or PDF::B, assume want PDF::Builder 37 $PDFpref = 'B'; 38 } else { 39 print STDERR "Don't see A... or B..., default to $prefDefault\n"; 40 $PDFpref = $prefDefault; 41 } 42 close $FH; 43 } else { 44 # no preference expressed, default to PDF::Builder 45 print STDERR "No preference file found, so default to $prefDefault\n"; 46 $PDFpref = $prefDefault; 47 } 48} 49foreach (1 .. 2) { 50 if ($PDFpref eq 'A') { # A(PI2) preferred 51 $rcA = eval { 52 require PDF::API2; 53 1; 54 }; 55 if (!defined $rcA) { $rcA = 0; } # else is 1; 56 if ($rcA) { $rcB = 0; last; } 57 $PDFpref = 'B'; 58 } 59 if ($PDFpref eq 'B') { # B(uilder) preferred 60 $rcB = eval { 61 require PDF::Builder; 62 1; 63 }; 64 if (!defined $rcB) { $rcB = 0; } # else is 1; 65 if ($rcB) { $rcA = 0; last; } 66 $PDFpref = 'A'; 67 } 68} 69if (!$rcA && !$rcB) { 70 die "Neither PDF::API2 nor PDF::Builder is installed!\n"; 71} 72# ------------- 73 74our $VERSION = '1.002'; # VERSION 75my $LAST_UPDATE = '1.000'; # manually update whenever code is changed 76 77my $outfile = $0; 78if ($outfile =~ m#[\\/]([^\\/]+)$#) { $outfile = $1; } 79$outfile =~ s/\.pl$/.pdf/; 80 81my $pdftable = PDF::Table->new(); 82# ------------- 83my $pdf; 84if ($rcA) { 85 print STDERR "Using PDF::API2 library\n"; 86 $pdf = PDF::API2->new( -file => $outfile ); 87} else { 88 print STDERR "Using PDF::Builder library\n"; 89 $pdf = PDF::Builder->new( -file => $outfile ); 90} 91# ------------- 92my $page = $pdf->page(); 93$pdf->mediabox('A4'); 94 95# A4 as defined by PDF::API2 is h=842 w=545 for portrait 96 97# some data to lay out. notice that there are 9 rows with the raw data for 98# 'two' and 'four' being split up into multiple lines = multiple single rows, 99# as well as 'four' being split on max_word_length 100my $some_data = [ 101 # H. dk blue on yellow, underlined (each page) 102 [ 'HeaderA', 'HeaderB' ], 103 # 1. white on red,blue underlined (page 1) 104 [ 'foo', 'bar Aye' ], 105 # 2. dk gray on light gray, underlined (page 2) 106 [ 'one', 'twosie' ], 107 # 3. light gray on dk gray, col 2 underlined, split multiple pages 3-6 108 [ 'two', 'four score and seven years ago our forefathers brought forth' ], 109 # 4. dk gray on light gray, underlined (page 7) 110 [ 'three', 'six pack' ], 111 # 5. light gray on dk gray, underlined, split multiple pages 8-9 112 [ 'four', 'abcdefghijklmnopqrstuvwxyz' ], 113]; 114 115# build the table layout 116# this will show the header and one line (row), meaning two rows will be 117# split up into multiple pages 118$pdftable->table( 119 120 # required params 121 $pdf, 122 $page, 123 $some_data, 124 x => 10, 125 w => 255, 126 start_y => 700, # or y 127 next_y => 700, 128 start_h => 69, # or h. just enough height for two rows per page 129 next_h => 69, # (repeated header + next data row single line) 130 131 # some optional params 132 bg_color_odd => "#666666", 133 bg_color_even => "#EEEEEE", 134 fg_color_odd => "#EEEEEE", 135 fg_color_even => "#666666", 136 padding_left => 10, # new 137 padding_right => 10, 138 border => 0, # no frame or rules (default thin line) 139 font_size => 20, # default leading about 25 140 #font_underline => 'auto', # underline everything with thin line 141 font_underline => [3, 2], # or underline. thick underline for all 142 # text (including header) unless override 143 max_word_length => 13, # force alphabet row to split in half 144 145 header_props => { 146 # font size defaults to 22, leading to 27.5 147 background_color => 'yellow', # or bg_color 148 repeat => 1 # is now default 149 }, 150 cell_props => [ 151 [], # header row no cell overrides 152 [ # first data row (foo bar Aye) 153 { background_color => 'red', fg_color => 'white' }, 154 { background_color => 'blue', fg_color => 'white' } 155 ], 156 [], # second data row, no cell overrides 157 [ # third data row, no underline first column 'two four...' 158 { underline => [] } # 'none' OK for PDF::Builder 159 ], 160 ], 161); 162 163$pdf->save(); 164