File: border_rules.pl

package info (click to toggle)
libpdf-table-perl 1%3A1.003-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 548 kB
  • sloc: perl: 3,469; makefile: 14
file content (385 lines) | stat: -rw-r--r-- 10,064 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env perl
use warnings;
use strict;
use diagnostics;
use PDF::Table;

# Demonstrate a number of border and rule settings.

# Please use TABSTOP=4 for best view
# -------------
# -A or -B on command line to select preferred library (if available)
# then look for PDFpref file and read A or B forms
my ($PDFpref, $rcA, $rcB); # which is available?
my $prefFile = "./PDFpref";
my $prefDefault = "B"; # PDF::Builder default if no prefFile, or both installed
if (@ARGV) {
    # A or -A argument: set PDFpref to A else B
    if ($ARGV[0] =~ m/^-?([AB])/i) {
	$PDFpref = uc($1);
    } else {
	print STDERR "Unknown command line flag $ARGV[0] ignored.\n";
    }
}
if (!defined $PDFpref) {
    if (-f $prefFile && -r $prefFile) {
        open my $FH, '<', $prefFile or die "error opening $prefFile: $!\n";
        $PDFpref = <$FH>;
        if      ($PDFpref =~ m/^A/i) {
	    # something starting with A, assume want PDF::API2
	    $PDFpref = 'A';
        } elsif ($PDFpref =~ m/^B/i) {
	    # something starting with B, assume want PDF::Builder
	    $PDFpref = 'B';
        } elsif ($PDFpref =~ m/^PDF:{1,2}A/i) {
	    # something starting with PDF:A or PDF::A, assume want PDF::API2
	    $PDFpref = 'A';
        } elsif ($PDFpref =~ m/^PDF:{1,2}B/i) {
	    # something starting with PDF:B or PDF::B, assume want PDF::Builder
	    $PDFpref = 'B';
        } else {
	    print STDERR "Don't see A... or B..., default to $prefDefault\n";
	    $PDFpref = $prefDefault;
        }
        close $FH;
    } else {
        # no preference expressed, default to PDF::Builder
        print STDERR "No preference file found, so default to $prefDefault\n";
        $PDFpref = $prefDefault;
    }
}
foreach (1 .. 2) {
    if ($PDFpref eq 'A') { # A(PI2) preferred
        $rcA = eval {
            require PDF::API2;
            1;
        };
        if (!defined $rcA) { $rcA = 0; } # else is 1;
        if ($rcA) { $rcB = 0; last; }
	$PDFpref = 'B';
    } 
    if ($PDFpref eq 'B') { # B(uilder) preferred
        $rcB = eval {
            require PDF::Builder;
            1;
        };
        if (!defined $rcB) { $rcB = 0; } # else is 1;
	if ($rcB) { $rcA = 0; last; }
	$PDFpref = 'A';
    }
}
if (!$rcA && !$rcB) {
    die "Neither PDF::API2 nor PDF::Builder is installed!\n";
}
# -------------

our $VERSION = '1.003'; # VERSION
our $LAST_UPDATE = '1.000'; # manually update whenever code is changed

my $outfile = $0;
if ($outfile =~ m#[\\/]([^\\/]+)$#) { $outfile = $1; }
$outfile =~ s/\.pl$/.pdf/;

my $pdftable = PDF::Table->new();
# -------------
my $pdf;
if ($rcA) {
    print STDERR "Using PDF::API2 library\n";
    $pdf      = PDF::API2->new( -file => $outfile );
} else {
    print STDERR "Using PDF::Builder library\n";
#   $pdf      = PDF::Builder->new( -file => $outfile );
    $pdf      = PDF::Builder->new( -file => $outfile, -compress=>'none' );
}
# -------------
my $page     = $pdf->page();

############################################################################
# illustrate thick border with inherited rules, thin rules, no rules
my $table = [
	# rows TTB, LTR 
	[ 'Thick', 'border', ],
	[ 'Inherit', 'rules', ],
];

my $font_size = 15;
my $font  = $pdf->corefont('Helvetica');
my $fontb = $pdf->corefont('Helvetica-Bold');

# build the table layout
$pdftable->table(

	# required params
	$pdf,
	$page,
	$table,
	x  => 10,
	w  => 100,
	y  => 700, 
	h  => 100,

	# some optional params
	justify    => "center",
	font       => $font,
	font_size  => $font_size,
	border_w   => 8,
	# default padding 2 isn't quite enough, but show effect

	cell_props => [
		[],
		[
			{ font => $fontb, },
		],
	],
);

$table = [
	# rows TTB, LTR 
	[ 'Thick', 'border', ],
	[ 'Thin', 'rules', ],
];

# build the table layout
$pdftable->table(

	# required params
	$pdf,
	$page,
	$table,
	x  => 160,
	w  => 100,
	y  => 700, 
	h  => 100,

	# some optional params
	justify    => "center",
	font       => $font,
	font_size  => $font_size,
	# borders both black, 8 wide
	border_w   => 8,
	# rules blue, 1 wide
	rule_w     => 1,
	rule_c     => 'blue',

	cell_props => [
		[],
		[
			{ font => $fontb, },
		],
	],
);

$table = [
	# rows TTB, LTR 
	[ 'Thick', 'border', ],
	[ 'No', 'rules', ],
];

# build the table layout
$pdftable->table(

	# required params
	$pdf,
	$page,
	$table,
	x  => 310,
	w  => 100,
	y  => 700, 
	h  => 100,

	# some optional params
	justify    => "center",
	font       => $font,
	font_size  => $font_size,
	# vertical borders thick, horizontal borders (override) thinner
	border_w   => 8,
	h_border_w => 3,
	# no visible rules
	rule_w     => 0,

	cell_props => [
		[],
		[
			{ font => $fontb, },
		],
	],
);

############################################################################
# illustrate lots of row, col, cell color and rules variations
$table = [
	# rows TTB, LTR 
	[ '0,0', '0,1',  '0,2',   '0,3', '0,4',  '0,5',  '0,6', '0,7' ],
	[ '1,0', '1,1',  '1,2',   '1,3', '1,4',  '1,5',  '1,6', '1,7' ],
	[ '2,0', '2,1',  '2,2',   '2,3', '2,4',  '2,5',  '2,6', '2,7' ],
	[ '3,0', '3,1',  '3,2',   '3,3', '3,4',  '3,5',  '3,6', '3,7' ],
	[ '4,0', '4,1',  '4,2',   '4,3', '4,4',  '4,5',  '4,6', '4,7' ],
	[ '5,0', '5,1',  '5,2',   '5,3', '5,4',  '5,5',  '5,6', '5,7' ],
	[ '6,0', '6,1',  '6,2',   '6,3', '6,4',  '6,5',  '6,6', '6,7' ],
	[ '7,0', '7,1',  '7,2',   '7,3', '7,4',  '7,5',  '7,6', '7,7' ],
];

# build the table layout
$pdftable->table(

	# required params
	$pdf,
	$page,
	$table,
	x  => 10,
	w  => 500,
	y  => 590, 
	h  => 500,

	# some optional params
	justify    => "center",
	font       => $font,
	font_size  => $font_size,
	padding    => 10,
	# thick borders 
	border_w   => 3,
	border_c   => 'green',
	# thin rules
	rule_w     => 1,
	# rule_c inherits from border_c (green)

        # play some games with colors and rules. note that varying rule widths
	# and changes of rule color may not align perfectly nicely in wild
	# examples like this!

	# 1. no rules around 1,1 (also affects 0,1 and 1,2) and bold font
	# 2. 4,5 and 4,6 (overlap pink column) light blue bg
	# 3. 6,3 and 7,3 (overlap yellow row) light green bg
	# 4. 4,1 left and bottom rules thick black
	cell_props => [
		[ # row 0
			{},
			{ h_rule_w => 0, }, # 0,1 no bottom rule (top of 1,1)
		],
		[ # row 1
		  	{}, # 1,0
			{ font => $fontb, rule_w => 0, }, # 1,1
			{ v_rule_w => 0, }, # 1,2 no left rule (right of 1,1)
		],
		[],[],
		[ # row 4
			{},
			{ rule_c => 'black', rule_w => 4, }, # 4,1
			{},{},{},
			{ bg_color => '#EEEEFF', }, # 4,5
			{ bg_color => '#EEEEFF', }, # 4,6
		],
		[],
		[ # row 6
			{},{},{},
			{ bg_color => '#AAFFAA', } # 6,3
		],
		[ # row 7
			{},{},{},
			{ bg_color => '#AAFFAA', } # 7,3
		],
	],

	# 5. col 4 thick left vertical rule (form full line)
	# 6. col 5 thick bottom rule
	# 7. col 6 pink background
	column_props => [
		{},{},{},{},
		{ v_rule_w => 4, v_rule_c => 'black', },
		{ h_rule_w => 4, h_rule_c => '#666666', },
		{ bg_color => 'pink', },
	],
	
	# 8.  row 3 thick left rule
	# 9.  row 5 bottom horizontal rule (form full line), thick red
	# 10. row 7 yellow background
	row_props => [
		{},{},{},
		{ v_rule_w => 5, v_rule_c => '#888888', },
		{},
		{ h_rule_w => 3, h_rule_c => 'red', },
		{},
		{ bg_color => 'yellow', },
	],
);

############################################################################
# illustrate different kinds of bottom/top borders on row/table splits

# Lorem Ipsum text, borrowed from PDF::Builder::examples/022_truefonts 
$table = [
    [ # 0,0
"Sed ut perspiciatis, unde omnis iste natus error sit ".
"voluptatem accusantium doloremque laudantium, totam rem aperiam eaque ipsa, ".
"quae ab illo inventore veritatis et quasi architecto beatae vitae dicta ".
"sunt, explicabo.",
      # 0,1
"Nemo enim ipsam voluptatem, quia voluptas sit, aspernatur ".
"aut odit aut fugit, sed quia consequuntur magni dolores eos, qui ratione ".
"dolor sit, voluptatem sequi nesciunt, neque porro quisquam est, qui dolorem ".
"ipsum, quia amet, consectetur, adipisci velit, sed quia non numquam eius ".
"modi tempora incidunt, ut labore et dolore magnam aliquam quaerat ".
"voluptatem."
    ],
    [ # 1,0
"Ut enim ad minima veniam, quis nostrum exercitationem ullam ".
"corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur?",
      # 1,1
"Quis autem vel eum iure reprehenderit, qui in ea voluptate velit esse, quam ".
"nihil molestiae consequatur, vel illum, qui dolorem eum fugiat, quo voluptas ".
"nulla pariatur?"
    ],
    [ # 2,0
"At vero eos et accusamus et iusto odio dignissimos ducimus, ".
"qui blanditiis praesentium voluptatum deleniti atque corrupti, quos dolores ".
"et quas molestias excepturi sint, obcaecati cupiditate non provident, ".
"similique sunt in culpa, qui officia deserunt mollitia animi, id est laborum ".
"et dolorum fuga.",
      # 2,1
"Et harum quidem rerum facilis est et expedita distinctio."
    ],
    [ # 3,0
"Nam libero tempore, cum soluta nobis est eligendi optio, cumque nihil ".
"impedit, quo minus id, quod maxime placeat, facere possimus, omnis voluptas ".
"assumenda est, omnis dolor repellendus.",
      # 3,1
"Temporibus autem quibusdam et aut ".
"officiis debitis aut rerum necessitatibus saepe eveniet, ut et voluptates ".
"repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur ".
"a sapiente delectus, ut aut reiciendis voluptatibus maiores alias ".
"consequatur aut perferendis doloribus asperiores repellat."
    ]
];

# build the table layout
$pdftable->table(

	# required params
	$pdf,
	$page,
	$table,
	x  => 10,
	w  => 240,
	y  => 200,      # start near bottom
	h  => 150,      # adjust to split in middle of row

	next_y => 700,
	next_h => 500,  # adjust to split at least once at row boundary
	                # 530 is also a good split

	# some optional params
	default_text => ' ',
	justify    => "left",
	font       => $font,
	font_size  => $font_size,
	padding    => 10,
	# thick borders 
	border_w   => 6,
	border_c   => '#222222',
	# thin rules
	rule_w     => 2,  # to distinguish from split-row solid/dashed
	# rule_c inherits from border_c (dark gray)

);

$pdf->save();