error_reporting(E_ALL); set_include_path('../src/' . PATH_SEPARATOR . get_include_path()); date_default_timezone_set('UTC'); include 'Cezpdf.php'; $pdf = new Cezpdf('a4', 'portrait'); // to test on windows xampp if (strpos(PHP_OS, 'WIN') !== false) { $pdf->tempPath = 'C:/temp'; } if (!isset($_GET['nocrypt'])) { // define the encryption mode (either RC4 40bit or RC4 128bit) $user = isset($_GET['user']) ? $_GET['user'] : ''; $owner = isset($_GET['owner']) ? $_GET['owner'] : ''; $mode = isset($_GET['mode']) && is_numeric($_GET['mode']) ? $_GET['mode'] : 1; $pdf->setEncryption($user, $owner, array(), $mode); } // select a font $pdf->selectFont('Times-Roman'); $pdf->openHere('Fit'); $pdf->ezText("This example shows how to crypt the PDF document\n"); $pdf->ezText("\nUse \"?mode=1\" for RC4 40bit encryption\n"); $pdf->ezText("\nUse \"?mode=2\" for RC4 128bit encryption\n"); $pdf->ezText("\nUse \"?nocrypt\" to disable the encryption\n"); $pdf->ezText("\nUse \"?user=password\" to set a user password\n"); $pdf->ezText("\nUse \"?owner=password\" to set a owner password\n"); if (isset($_GET['nocrypt'])) { $pdf->ezText("<b>Not encrypt</b> - nocrypt parameter found"); } if (isset($_GET['d']) && $_GET['d']) { echo $pdf->ezOutput(TRUE);
/** * Creates a PDF document and sends this pricelist to the client * * Unfortunately, ezpdf does not return anything after printing the * document, so there's no way to tell whether it has succeeded. * Thus, you should not rely on the return value, except when it is * false -- in that case, loading of some data failed. * @return boolean False on failure, true on supposed success */ function send_as_pdf() { global $objInit, $_ARRAYLANG; if (!$this->load()) { return \Message::error($_ARRAYLANG['TXT_SHOP_PRICELIST_ERROR_LOADING']); } $objPdf = new \Cezpdf('A4'); $objPdf->setEncryption('', '', array('print')); $objPdf->selectFont(\Cx\Core\Core\Controller\Cx::instanciate()->getCodeBaseLibraryPath() . '/ezpdf/fonts/' . $this->font); $objPdf->ezSetMargins(0, 0, 0, 0); // Reset margins $objPdf->setLineStyle(0.5); $marginTop = 30; $biggerCountTop = $biggerCountBottom = 0; $arrHeaderLeft = $arrHeaderRight = $arrFooterLeft = $arrFooterRight = array(); if ($this->header) { // header should be shown $arrHeaderLeft = explode("\n", $this->header_left); $arrHeaderRight = explode("\n", $this->header_right); $countLeft = count($arrHeaderLeft); $countRight = count($arrHeaderRight); $biggerCountTop = $countLeft > $countRight ? $countLeft : $countRight; $marginTop = $biggerCountTop * 14 + 36; } // Bottom margin $marginBottom = 20; $arrFooterRight = array(); if ($this->footer) { // footer should be shown // Old, obsolete: $this->footer_left = str_replace('<--DATE-->', date(ASCMS_DATE_FORMAT_DATE, time()), $this->footer_left); $this->footer_right = str_replace('<--DATE-->', date(ASCMS_DATE_FORMAT_DATE, time()), $this->footer_right); // New: $this->footer_left = str_replace('[DATE]', date(ASCMS_DATE_FORMAT_DATE, time()), $this->footer_left); $this->footer_right = str_replace('[DATE]', date(ASCMS_DATE_FORMAT_DATE, time()), $this->footer_right); $arrFooterLeft = explode("\n", $this->footer_left); $arrFooterRight = explode("\n", $this->footer_right); $countLeft = count($arrFooterLeft); $countRight = count($arrFooterRight); $biggerCountBottom = $countLeft > $countRight ? $countLeft : $countRight; $marginBottom = $biggerCountBottom * 20 + 20; } // Borders if ($this->border) { $linesForAllPages = $objPdf->openObject(); $objPdf->saveState(); $objPdf->setStrokeColor(0, 0, 0, 1); $objPdf->rectangle(10, 10, 575.28, 821.89); $objPdf->restoreState(); $objPdf->closeObject(); $objPdf->addObject($linesForAllPages, 'all'); } // Header $headerArray = array(); $startpointY = 0; if ($this->header) { $objPdf->ezSetY(830); $headerForAllPages = $objPdf->openObject(); $objPdf->saveState(); for ($i = 0; $i < $biggerCountTop; ++$i) { $headerArray[$i] = array('left' => isset($arrHeaderLeft[$i]) ? $arrHeaderLeft[$i] : '', 'right' => isset($arrHeaderRight[$i]) ? $arrHeaderRight[$i] : ''); } $tempY = $objPdf->ezTable($headerArray, '', '', array('showHeadings' => 0, 'fontSize' => $this->font_size_header, 'shaded' => 0, 'width' => 540, 'showLines' => 0, 'xPos' => 'center', 'xOrientation' => 'center', 'cols' => array('right' => array('justification' => 'right')))); $tempY -= 5; if ($this->border) { $objPdf->setStrokeColor(0, 0, 0); $objPdf->line(10, $tempY, 585.28, $tempY); } $startpointY = $tempY - 5; $objPdf->restoreState(); $objPdf->closeObject(); $objPdf->addObject($headerForAllPages, 'all'); } // Footer $pageNumbersX = $pageNumbersY = $pageNumbersFont = 0; if ($this->footer) { $footerForAllPages = $objPdf->openObject(); $objPdf->saveState(); $tempY = $marginBottom - 5; if ($this->border) { $objPdf->setStrokeColor(0, 0, 0); $objPdf->line(10, $tempY, 585.28, $tempY); } // length of the longest word $longestWord = 0; foreach ($arrFooterRight as $line) { if ($longestWord < strlen($line)) { $longestWord = strlen($line); } } for ($i = $biggerCountBottom - 1; $i >= 0; --$i) { if (empty($arrFooterLeft[$i])) { $arrFooterLeft[$i] = ''; } if (empty($arrFooterRight[$i])) { $arrFooterRight[$i] = ''; } if ($arrFooterLeft[$i] == '<--PAGENUMBER-->' || $arrFooterLeft[$i] == '[PAGENUMBER]') { $pageNumbersX = 65; $pageNumbersY = $tempY - 18 - $i * $this->font_size_footer; $pageNumbersFont = $this->font_size_list; } else { $objPdf->addText(25, $tempY - 18 - $i * $this->font_size_footer, $this->font_size_footer, $arrFooterLeft[$i]); } if ($arrFooterRight[$i] == '<--PAGENUMBER-->' || $arrFooterRight[$i] == '[PAGENUMBER]') { $pageNumbersX = 595.28 - 25; $pageNumbersY = $tempY - 18 - $i * $this->font_size_footer; $pageNumbersFont = $this->font_size_list; } else { // Properly align right $width = $objPdf->getTextWidth($this->font_size_footer, $arrFooterRight[$i]); $objPdf->addText(595.28 - $width - 25, $tempY - 18 - $i * $this->font_size_footer, $this->font_size_footer, $arrFooterRight[$i]); } } $objPdf->restoreState(); $objPdf->closeObject(); $objPdf->addObject($footerForAllPages, 'all'); } // Page numbers if (isset($pageNumbersX)) { $objPdf->ezStartPageNumbers($pageNumbersX, $pageNumbersY, $pageNumbersFont, '', $_ARRAYLANG['TXT_SHOP_PRICELIST_FORMAT_PAGENUMBER'], 1); } // Margins $objPdf->ezSetMargins($marginTop, $marginBottom, 30, 30); // Product table if (isset($startpointY)) { $objPdf->ezSetY($startpointY); } $objInit->backendLangId = $this->lang_id; $_ARRAYLANG = $objInit->loadLanguageData('Shop'); Currency::setActiveCurrencyId($this->currency_id); $currency_symbol = Currency::getActiveCurrencySymbol(); $category_ids = $this->category_ids(); if ($category_ids == '*') { $category_ids = null; } $count = 1000; // Be sensible! // Pattern is "%" because all-empty parameters will result in an // empty array! $arrProduct = Products::getByShopParams($count, 0, null, $category_ids, null, '%', null, null, '`category_id` ASC, `name` ASC'); $arrCategoryName = ShopCategories::getNameArray(); $arrOutput = array(); foreach ($arrProduct as $product_id => $objProduct) { $categoryIds = explode(',', $objProduct->category_id()); $arrCategoryNames = array(); foreach ($categoryIds as $categoryId) { $arrCategoryNames[] = $arrCategoryName[$categoryId]; } //$objProduct = new Product(); $arrOutput[$product_id] = array('product_name' => self::decode($objProduct->name()), 'category_name' => self::decode(implode(', ', $arrCategoryNames)), 'product_code' => self::decode($objProduct->code()), 'product_id' => self::decode($objProduct->id()), 'price' => ($objProduct->discount_active() ? "S " . Currency::formatPrice($objProduct->discountprice()) : Currency::formatPrice($objProduct->price())) . ' ' . $currency_symbol); } $objPdf->ezTable($arrOutput, array('product_name' => '<b>' . self::decode($_ARRAYLANG['TXT_SHOP_PRODUCT_NAME']) . '</b>', 'category_name' => '<b>' . self::decode($_ARRAYLANG['TXT_SHOP_CATEGORY_NAME']) . '</b>', 'product_code' => '<b>' . self::decode($_ARRAYLANG['TXT_SHOP_PRODUCT_CODE']) . '</b>', 'product_id' => '<b>' . self::decode($_ARRAYLANG['TXT_ID']) . '</b>', 'price' => '<b>' . self::decode($_ARRAYLANG['TXT_SHOP_PRICE']) . '</b>'), '', array('showHeadings' => 1, 'fontSize' => $this->font_size_list, 'width' => 530, 'innerLineThickness' => 0.5, 'outerLineThickness' => 0.5, 'shaded' => 2, 'shadeCol' => array(hexdec(substr($this->row_color_1, 0, 2)) / 255, hexdec(substr($this->row_color_1, 2, 2)) / 255, hexdec(substr($this->row_color_1, 4, 2)) / 255), 'shadeCol2' => array(hexdec(substr($this->row_color_2, 0, 2)) / 255, hexdec(substr($this->row_color_2, 2, 2)) / 255, hexdec(substr($this->row_color_2, 4, 2)) / 255), 'cols' => array('product_name' => array('width' => 255), 'category_name' => array('width' => 130), 'product_code' => array('width' => 50), 'product_id' => array('width' => 40, 'justification' => 'right'), 'price' => array('width' => 55, 'justification' => 'right')))); $objPdf->ezStream(); // Never reached return true; }