public function getTan($value) { $dbh = $this->getDatabaseHandler(); $stmt = $dbh->prepare("SELECT * FROM tans WHERE tan_value = :tanValue;"); $stmt->bindParam(':tanValue', $value); $stmt->execute(); if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) { $tan = new Tan(); $tan->exchangeArray($data); return $tan; } return null; }
public function generateTanPdf(Customer $customer, $password) { $tanRepository = $this->getTanRepository(); $tans = array(); for ($i = 0; $i < self::NUMBER_OF_INIT_TANS; $i++) { $tan = Tan::generate($customer->id); if ($tanRepository->saveTan($tan)) { $tans[] = $tan; } } $tans = array_map(function ($tan) { return $tan->value; }, $tans); function wrapWithWhitespace($str, $length) { $neededPadding = $length - strlen($str); $front = floor($neededPadding / 2) > 0 ? floor($neededPadding / 2) : 0; $back = ceil($neededPadding / 2) > 0 ? ceil($neededPadding / 2) : 0; $str = str_repeat(" ", $front) . $str . str_repeat(" ", $back); return substr($str, 0, $length); } try { $p = new \PDFlib(); if ($p->begin_document("", "") == 0) { die("Error: " . $p->get_errmsg()); } $p->set_info("Creator", "SitzBank"); $p->set_info("Author", "SitzBank App"); $p->set_info("Title", "Tans for {$customer->firstname} {$customer->lastname}"); $p->set_parameter("textformat", "utf8"); $p->begin_page_ext(595, 842, ""); $font = $p->load_font("Helvetica-Bold", "winansi", ""); $p->setfont($font, 18.0); $p->set_text_pos(25, 780); $p->show("Tans for {$customer->firstname} {$customer->lastname}"); $font = $p->load_font("Courier", "winansi", ""); $p->setfont($font, 9.0); $p->set_text_pos(20, 750); $p->show(str_repeat("-", 100)); $tansPerRow = 4; for ($i = 0; $i < count($tans); $i += $tansPerRow) { $limit = min($i + $tansPerRow, count($tans)); $row = array_slice($tans, $i, $limit); $row = array_map(function ($str) { return wrapWithWhitespace($str, 25); }, $row); $p->continue_text(implode('|', $row)); } $p->continue_text(str_repeat("-", 100)); $p->end_page_ext(""); $p->end_document(""); $buf = $p->get_buffer(); } catch (\Exception $e) { return JsonErrorResponse::fromKey(JsonErrorResponse::UNEXPECTED_ERROR); } $temp_file = tempnam(sys_get_temp_dir(), 'SBTanPdf'); file_put_contents($temp_file, $buf); $temp_file_output = $temp_file . "-pw"; if (in_array(strtoupper(substr(PHP_OS, 0, 3)), array('DAR', 'WIN'))) { shell_exec("cp {$temp_file} {$temp_file_output}"); } else { shell_exec("/usr/bin/pdftk {$temp_file} output {$temp_file_output} user_pw {$password}"); } return $temp_file_output; }