-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathex.php
86 lines (68 loc) · 1.55 KB
/
ex.php
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
<?php
declare(strict_types=1);
require dirname(dirname(__DIR__)) . '/fpdf/fpdf.php';
require __DIR__ . '/PDFMultiCellsTableTrait.php';
use FPDF\Scripts\PDFMultiCellsTable\PDFMultiCellsTableTrait;
/**
* Get a random word
*
* @return string
*/
function GenerateWord () : string {
$nb = rand(3,10);
$word = '';
while (strlen($word) <= $nb) {
$word .= chr(rand(ord('a'), ord('z')));
}
return $word;
}
/**
* Get a random sentence
*
* @return string
*/
function GenerateSentence () : string {
$nw = rand(1, 10);
$words = [];
while (count($words) < $nw) {
$words[] = GenerateWord();
}
return implode(' ', $words);
}
/**
* Generate a table of random sentences
*
* @param int $cols
* @param int $rows
* @return array
*/
function GenerateTable (int $cols, int $rows) : array {
$table = [];
while (count($table) < $rows) {
$row = [];
while (count($row) < $cols) {
$row[] = GenerateSentence();
}
$table[] = $row;
}
return $table;
}
$pdf = new class extends FPDF {
use PDFMultiCellsTableTrait;
};
$pdf->AddPage();
$pdf->SetFont('Arial', '', 14);
//Table with 20 rows and 4 columns
$filename = __DIR__ . '/table.json';
if (!is_file($filename)) {
srand((int) (microtime(true) * 1000000));
$table = GenerateTable(4, 20);
file_put_contents($filename, json_encode($table));
} else {
$table = json_decode(file_get_contents($filename), true);
}
$pdf->SetWidths([30, 50, 30, 40]);
foreach ($table as $row) {
$pdf->Row($row);
}
$pdf->Output('F', __DIR__ . '/example.pdf');