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; }
<?php $p = new PDFlib(); $p->begin_document(null, null); $p->set_info('Creator', 'test-pdf.php'); $p->set_info('Author', 'entropy.ch'); $p->set_info('Title', 'PDFlib Test'); $p->begin_page_ext(595, 842, ""); $font = $p->load_font("Helvetica-Bold", "winansi", ""); $p->setfont($font, 24.0); $p->set_text_pos(50, 700); $p->show("Hello world!"); $p->continue_text("(says PHP)"); $p->end_page_ext(""); $p->end_document(""); $buf = $p->get_buffer(); $len = strlen($buf); header("Content-type: application/pdf"); header("Content-Length: {$len}"); header("Content-Disposition: inline; filename=hello.pdf"); print $buf; $p->delete(); /* PDF_set_info($p, "Creator", "hello.php"); PDF_set_info($p, "Author", "Rainer Schaaf"); PDF_set_info($p, "Title", "Hello world (PHP)!"); PDF_begin_page_ext($p, 595, 842, "");
/** * This function creates a PDF with a title, an image and a comment. * * @param $page_width (The page width in dpi. (inches x 72) ) * @param $page_height (The page height in dpi. (inches x 72) ) * @param $title (The title to insert in the pdf) * @param $comments (The comments to insert in the pdf) * @param $mapImageUrl (The url to the image to insert in the pdf) */ function createPDF($page_width, $page_height, $title, $comments, $mapImageUrl) { $p = new PDFlib(); /* open new PDF file; insert a file name to create the PDF on disk */ if ($p->begin_document($mapImageUrl . ".pdf", "") == 0) { die("Error: " . $p->get_errmsg()); } // These lines should set the PDF properties, but it does not seem to work. $p->set_info("Creator", "Vigilance"); $p->set_info("Author", "Vigilance"); $p->set_info("Title", $title); // Create a new PDF page. $p->begin_page_ext($page_width, $page_height, ""); // Loads the Helvetica font. $font = $p->load_font("Helvetica", "winansi", ""); $p->setfont($font, 20.0); // Displays the title as set by the user // On ne peut pas dire a pdflib de centrer le texte sur la ligne... donc $page_width / 2 - 20; $p->set_text_pos($page_width / 2 - 20, $page_height - 30); $p->show($title); // Adds the image to the PDF. $image = $p->load_image("auto", $mapImageUrl, ""); $p->fit_image($image, 30, 80, ""); $p->close_image($image); // Adds the comments to the PDF. $p->set_text_pos(10, 60); $p->setfont($font, 12.0); $p->continue_text($comments); // Ends the page and the document. $p->end_page_ext(""); $p->end_document(""); }
public function customerTransactionsPdf(Request $request, $id) { $transactionRepository = $this->getTransactionRepository(); $customerRepository = $this->getCustomerRepository(); $transactions = $transactionRepository->getCustomerTransactions($id); $customer = $customerRepository->getCustomerById($id); if (is_null($customer)) { return JsonErrorResponse::fromKey(JsonErrorResponse::INVALID_CUSTOMER_ID); } //add customer data foreach ($transactions as $trans) { $trans->from_customer = $customerRepository->getCustomerById($trans->from_id); $trans->to_customer = $customerRepository->getCustomerById($trans->to_id); } 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", "Transactions 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("Transactions 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)); $header = array('Time', 'From', 'To', 'Amount', 'Status'); $header = array_map(function ($str) { return wrapWithWhitespace($str, 19); }, $header); $p->continue_text(implode('|', $header)); foreach ($transactions as $trans) { $p->continue_text(str_repeat("-", 100)); $row = array($trans->timestamp, $trans->from_customer->firstname . ' ' . $trans->from_customer->lastname, $trans->to_customer->firstname . ' ' . $trans->to_customer->lastname, $trans->amount, $trans->status); $row = array_map(function ($str) { return wrapWithWhitespace($str, 19); }, $row); $p->continue_text(implode('|', $row)); $p->continue_text("Description: {$trans->description}"); } $p->continue_text(str_repeat("=", 100)); $p->continue_text(str_repeat(" ", 100)); $p->setfont($font, 12.0); $p->continue_text("Balance: " . $customerRepository->getCustomerBalance($customer->id)); $p->end_page_ext(""); $p->end_document(""); $buf = $p->get_buffer(); $len = strlen($buf); } catch (\Exception $e) { return JsonErrorResponse::fromKey(JsonErrorResponse::UNEXPECTED_ERROR); } header("Content-type: application/pdf"); header("Content-Length: {$len}"); header("Content-Disposition: inline; filename=transactions.pdf"); return $buf; }
try { $p = new PDFlib(); /* öffnet eine neue PDF-Datei; fügen Sie einen Dateinamen ein, um das PDF auf der Platte zu speichern */ if ($p->begin_document("", "") == 0) { die("Error: " . $p->get_errmsg()); } $p->set_info("Creator", "hallo.php"); $p->set_info("Author", "Rainer Schaaf"); $p->set_info("Title", "Hallo Welt (PHP)!"); $p->begin_page_ext(595, 842, ""); $font = $p->load_font("Helvetica-Bold", "winansi", ""); $p->setfont($font, 24.0); $p->set_text_pos(50, 700); $p->show("Hallo Welt!"); $p->continue_text("(sagt PHP)"); $p->end_page_ext(""); $p->end_document(""); $buf = $p->get_buffer(); $len = strlen($buf); header("Content-type: application/pdf"); header("Content-Length: {$len}"); header("Content-Disposition: inline; filename=hallo.pdf"); print $buf; } catch (PDFlibException $e) { die("Eine PDFlib-Exception ist aufgetreten im hallo-Schnipsel:\n" . "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " . $e->get_errmsg() . "\n"); } catch (Exception $e) { die($e); } $p = 0;