aboutsummaryrefslogtreecommitdiffstats
path: root/php/gen-pdf.php
blob: 629a6cb0d349cfbb8eff6bd791513c3ba8a355ea (plain) (blame)
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
<?php
// Setting locale
date_default_timezone_set("Europe/Warsaw");

// Include the main TCPDF library (search for installation path).
require_once("tcpdf/tcpdf.php");

// Custom Header and Footer
class MYPDF extends TCPDF
{
  //Page header
  public function Header()
  {
    // Logo
    $image_file = K_PATH_IMAGES . 'logo_example.jpg';
    /* $this->Image($image_file, 10, 10, 15, '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false); */
    // Set font
    $this->SetFont('helvetica', 'B', 12);
    // Title
    $this->Cell(0, 15, 'PDF wygenerowano: ' . (new DateTime())->format("d-m-Y H:i:s"), 0, false, 'C', 0, '', 0, false, 'M', 'M');
  }

  // Page footer
  public function Footer()
  {
    // Position at 15 mm from bottom
    $this->SetY(-15);
    // Set font
    $this->SetFont('helvetica', 'I', 8);
    // Page number
    $this->Cell(0, 10, 'Page ' . $this->getAliasNumPage() . '/' . $this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
  }

  // Colored table
  public function ColoredTable($header = ["dzień", "temp."], $data = [[1, 2], [3, 4], [4, 5]])
  {
    require "get-data.php";
    $data = [];
    $d = getChartData()[0];
    foreach ($d as $k => $v) {
      if ($v === NULL)
        array_push($data, [$k + 1, "brak pom."]);
      elseif ($v < 0)
        array_push($data, [$k + 1, "choroba"]);
      else
        array_push($data, [$k + 1, $v]);
    }
    // Colors, line width and bold font
    /* $this->SetFillColor(255, 0, 0); */
    /* $this->SetTextColor(255); */
    /* $this->SetDrawColor(128, 0, 0); */
    /* $this->SetLineWidth(0.3); */
    /* $this->SetFont('', 'B'); */
    $this->SetFont('dejavusans', 'B', 10, '', true);
    // Header
    $x = 150;
    $y = 120;
    $w = array(16, 35);
    $this->SetXY($x, $y);
    $num_headers = count($header);

    for ($i = 0; $i < $num_headers; ++$i) {
      $this->Cell($w[$i], 6, $header[$i], 1, 0, 'C', 1);
    }
    $this->Ln();
    $this->SetFont('dejavusans', '', 10, '', true);
    // Color and font restoration
    /* $this->SetFillColor(224, 235, 255); */
    /* $this->SetTextColor(0); */
    /* $this->SetFont('Arial 15'); */
    // Data
    /* $fill = 0; */
    /* $this->SetXY($x, $y + 7); */
    foreach ($data as $key => $row) {
      /* $this->Cell($w[0], 6, $row[0], 'LR', 0, 'L', $fill); */
      //Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=0, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M')
      /* T : cell top C : center B : cell bottom</li><li>A : font top</li><li>L : font baseline</li><li>D : font bottom */
      $this->SetXY($x, $y + 6 + (5 * $key));
      $this->Cell($w[0], 5, $row[0], 'LRB', align: 'C');
      $this->SetXY($x + $w[0], $y + 6 + (5 * $key));
      $this->Cell($w[1], 5, $row[1], 'LRB', align: 'C');
      /* $this->Cell($w[2], 6, number_format($row[2]), 'LR', 0, 'R', $fill); */
      /* $this->Cell($w[3], 6, $row[3], 'LR', 0, 'R', $fill); */
      $this->Ln();
      /* $fill = !$fill; */
    }
    /* $this->Cell(array_sum($w), 0, '', 'T'); */
  }
}

// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Maksymilian Jopek');
$pdf->SetTitle('Temperature of bb');
$pdf->SetSubject('Temperatures');
$pdf->SetKeywords('temperatures, women, chart, table');

// set default header data
/* $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE . ' 001', PDF_HEADER_STRING, array(0, 64, 255), array(0, 64, 128)); */
/* $pdf->setFooterData(array(0, 64, 0), array(0, 64, 128)); */

// set header and footer fonts
/* $pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); */
/* $pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); */

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// ---------------------------------------------------------

// set default font subsetting mode
$pdf->setFontSubsetting(true);

// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
$pdf->SetFont('dejavusans', '', 14, '', true);

// Add a page
// This method has several options, check the source code documentation for more information.
$pdf->AddPage();

// set text shadow effect
$pdf->setTextShadow(array('enabled' => true, 'depth_w' => 0.2, 'depth_h' => 0.2, 'color' => array(196, 196, 196), 'opacity' => 1, 'blend_mode' => 'Normal'));

// Set some content to print
/* <div style="width: 5px; height: 5px; border-radius: 100%; background-color: red;">\2022</div> - Choroba<br> */
$html = <<<EOD
Legenda:<br>
&nbsp;&nbsp; - Choroba<br>
&nbsp;&nbsp; - Brak pomiaru
EOD;

// Print text using writeHTMLCell()
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->Image('@' . `php ./generate-chart.php`, x: 5, y: 10, w: 200, h: 100, type: "PNG");

/* include "generate-png.php"; */

/* $pdf->Image('@' . genPng(), x: 5, y: 10, w: 300, h: 150, type: "PNG"); */
// writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=0, $reseth=true, $align='', $autopadding=true)
$pdf->writeHTMLCell(w: 0, h: 0, x: 10, y: 120, html: $html, ln: 0, align: 'L');

$pdf->writeHTMLCell(w: 0, h: 0, x: 120, y: 120, html: "Pomiary:", ln: 1, align: '');
$pdf->ColoredTable();

$pdf->Circle(12.5, 135.6, 2, style: 'F', fill_color: array(255, 0, 0));
$pdf->Circle(12.5, 129.6, 2, style: 'F', fill_color: array(100, 100, 100));

// ---------------------------------------------------------

// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$pdf->Output('bb-temperatures.pdf', 'I');

//============================================================+
// END OF FILE
//============================================================+