[*] PDF : PDF is now rendered by TCPDF throught HTML template

This commit is contained in:
fBrignoli
2011-11-17 17:15:23 +00:00
parent 73e98a4f56
commit fddc403a09
33 changed files with 1917 additions and 213 deletions
+151
View File
@@ -0,0 +1,151 @@
<?php
/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 8797 $
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/**
* @since 1.5
*/
abstract class HTMLTemplateCore
{
public $title;
public $date;
public $address;
public $available_in_your_account = true;
public $smarty;
/**
* Returns the template's HTML header
* @return string HTML header
*/
public function getHeader()
{
$this->assignHookData();
$this->smarty->assign(array(
'logo_path' => $this->getLogo(),
'img_ps_dir' => 'http://'.Tools::getMediaServer(_PS_IMG_)._PS_IMG_,
'img_update_time' => Configuration::get('PS_IMG_UPDATE_TIME'),
'title' => $this->title,
'date' => $this->date,
'shop_name' => Configuration::get('PS_SHOP_NAME')
));
return $this->smarty->fetch(_PS_THEME_DIR_.'/pdf/header.tpl');
}
/**
* Returns the template's HTML footer
* @return string HTML footer
*/
public function getFooter()
{
$shop_address = '';
if (isset($this->address) && $this->address instanceof Address)
$shop_address = AddressFormat::generateAddress($this->address, array(), ' - ', ' ');
$this->smarty->assign(array(
'available_in_your_account' => $this->available_in_your_account,
'shop_address' => $shop_address,
'shop_fax' => Configuration::get('PS_SHOP_FAX'),
'shop_phone' => Configuration::get('PS_SHOP_PHONE'),
'shop_details' => Configuration::get('PS_SHOP_DETAILS'),
'free_text' => Configuration::get('PS_INVOICE_FREE_TEXT')
));
return $this->smarty->fetch(_PS_THEME_DIR_.'/pdf/footer.tpl');
}
/**
* Returns the invoice logo
*/
protected function getLogo()
{
$logo = '';
if (file_exists(_PS_IMG_DIR_.'logo_invoice.jpg'))
$logo = 'img/logo_invoice.jpg';
else if (file_exists(_PS_IMG_DIR_.'logo.jpg'))
$logo = 'img/logo.jpg';
return Tools::getShopDomain(true).__PS_BASE_URI__.'/'.$logo;
}
/**
* Returns the HTML content of the template's footer
*/
public function assignHookData()
{
$data = array('title' => 'cool',
'delivery' => array('date' => '25/11/11', 'delay' => '3'));
foreach ($data as $key => $value)
$this->smarty->assign($key, $value);
}
/**
* Returns the template's HTML content
* @return string HTML content
*/
abstract public function getContent();
/**
* Returns the template filename
* @return string filename
*/
abstract public function getFilename();
/**
* Returns the template filename when using bulk rendering
* @return string filename
*/
abstract public function getBulkFilename();
/**
* Translatation method
* @param string $string
* @return string translated text
*/
protected static function l($string)
{
$iso = Context::getContext()->language->iso_code;
if (!Validate::isLangIsoCode($iso))
die('Invalid iso lang ('.$iso.')');
if (@!include(_PS_THEME_DIR_.'pdf/'.'fr'.'.php'))
die('Cannot include PDF translation language file : '._PS_THEME_DIR_.'pdf/'.$iso.'.php');
if (!isset($_LANGPDF) OR !is_array($_LANGPDF))
return str_replace('"', '&quot;', $string);
$key = md5(str_replace('\'', '\\\'', $string));
$str = (key_exists('PDF_invoice'.$key, $_LANGPDF) ? $_LANGPDF['PDF_invoice'.$key] : $string);
return $str;
}
}
+97
View File
@@ -0,0 +1,97 @@
<?php
/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 8797 $
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/**
* @since 1.5
*/
class HTMLTemplateDeliverySlipCore extends HTMLTemplate
{
public $order;
public function __construct(Order $order, $smarty)
{
$this->order = $order;
$this->smarty = $smarty;
// header informations
$this->date = Tools::displayDate($order->invoice_date, (int)$order->id_lang);
$this->title = 'Invoice '.Configuration::get('PS_INVOICE_PREFIX').sprintf('%06d', $order->invoice_number);
// footer informations
$shop = new Shop((int)$order->id_shop);
$this->address = $shop->getAddress();
}
/**
* Returns the template's HTML content
* @return string HTML content
*/
public function getContent()
{
$country = new Country((int)$this->order->id_address_invoice);
$delivery_address = new Address((int)$this->order->id_address_delivery);
$formatted_delivery_address = AddressFormat::generateAddress($delivery_address, array(), '<br />', ' ');
$formatted_invoice_address = '';
if ($this->order->id_address_delivery != $this->order->id_address_invoice)
{
$invoice_address = new Address((int)$id_address_invoice);
$formatted_invoice_address = AddressFormat::generateAddress($invoice_address, array(), '<br />', ' ');
}
$customer = new Customer($this->order->id_customer);
$this->smarty->assign(array(
'order' => $this->order,
'order_details' => $this->order->getProducts(),
'delivery_address' => $formatted_delivery_address,
'invoice_address' => $formatted_invoice_address,
));
return $this->smarty->fetch(_PS_THEME_DIR_.'/pdf/delivery-slip.tpl');
}
/**
* Returns the template filename when using bulk rendering
* @return string filename
*/
public function getBulkFilename()
{
return 'deliveries.pdf';
}
/**
* Returns the template filename
* @return string filename
*/
public function getFilename()
{
return Configuration::get('PS_DELIVERY_PREFIX').sprintf('%06d', $this->order->invoice_number).'.pdf';
}
}
+137
View File
@@ -0,0 +1,137 @@
<?php
/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 8797 $
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/**
* @since 1.5
*/
class HTMLTemplateInvoiceCore extends HTMLTemplate
{
public $order;
public $available_in_your_account = false;
public function __construct(Order $order, $smarty)
{
$this->order = $order;
$this->smarty = $smarty;
// header informations
$this->date = Tools::displayDate($order->invoice_date, (int)$order->id_lang);
$this->title = self::l('Invoice ').Configuration::get('PS_INVOICE_PREFIX').sprintf('%06d', $order->invoice_number);
// footer informations
$shop = new Shop((int)$order->id_shop);
$this->address = $shop->getAddress();
}
/**
* Returns the template's HTML content
* @return string HTML content
*/
public function getContent()
{
$country = new Country((int)$this->order->id_address_invoice);
$invoice_address = new Address((int)$this->order->id_address_invoice);
$formatted_invoice_address = AddressFormat::generateAddress($invoice_address, array(), '<br />', ' ');
$formatted_delivery_address = '';
if ($this->order->id_address_delivery != $this->order->id_address_invoice)
{
$delivery_address = new Address((int)$this->order->id_address_delivery);
$formatted_delivery_address = AddressFormat::generateAddress($delivery_address, array(), '<br />', ' ');
}
$customer = new Customer($this->order->id_customer);
$this->smarty->assign(array(
'order' => $this->order,
'order_details' => $this->order->getProducts(),
'delivery_address' => $formatted_delivery_address,
'invoice_address' => $formatted_invoice_address,
'tax_excluded_display' => Group::getPriceDisplayMethod($customer->id_default_group),
'tax_tab' => $this->getTaxTabContent()
));
return $this->smarty->fetch($this->getTemplate($country->iso_code));
}
/**
* Returns the tax tab content
*/
public function getTaxTabContent()
{
$invoice_address = new Address((int)$this->order->id_address_invoice);
$tax_exempt = Configuration::get('VATNUMBER_MANAGEMENT')
AND !empty($invoiceAddress->vat_number)
AND $invoiceAddress->id_country != Configuration::get('VATNUMBER_COUNTRY');
$this->smarty->assign(array(
'tax_exempt' => $tax_exempt,
'use_one_after_another_method' => $this->order->useOneAfterAnotherTaxComputationMethod(),
'product_tax_breakdown' => $this->order->getProductTaxesBreakdown(),
'shipping_tax_breakdown' => $this->order->getShippingTaxesBreakdown(),
'ecotax_tax_breakdown' => $this->order->getEcoTaxTaxesBreakdown(),
'order' => $this->order,
));
return $this->smarty->fetch(_PS_THEME_DIR_.'/pdf/invoice.tax-tab.tpl');
}
/**
* Returns the invoice template associated to the country iso_code
* @param string $iso_country
*/
protected function getTemplate($iso_country)
{
$template = _PS_THEME_DIR_.'/pdf/invoice.tpl';
$iso_template = _PS_THEME_DIR_.'/pdf/invoice.'.$iso_country.'.tpl';
if (file_exists($iso_template))
$template = $iso_template;
return $template;
}
/**
* Returns the template filename when using bulk rendering
* @return string filename
*/
public function getBulkFilename()
{
return 'invoices.pdf';
}
/**
* Returns the template filename
* @return string filename
*/
public function getFilename()
{
return Configuration::get('PS_INVOICE_PREFIX').sprintf('%06d', $this->order->invoice_number).'.pdf';
}
}
+96
View File
@@ -0,0 +1,96 @@
<?php
/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 8797 $
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/**
* @since 1.5
*/
class HTMLTemplateOrderReturnCore extends HTMLTemplate
{
public $order_return;
public $order;
public function __construct(OrderReturn $order_return, $smarty)
{
$this->order_return = $order_return;
$this->smarty = $smarty;
$this->order = new Order($order_return->id_order);
// header informations
$this->date = Tools::displayDate($this->order->invoice_date, (int)$this->order->id_lang);
$this->title = 'Order Return '.sprintf('%06d', $this->order_return->id); // TODO
// footer informations
$shop = new Shop((int)$this->order->id_shop);
$this->address = $shop->getAddress();
}
/**
* Returns the template's HTML content
* @return string HTML content
*/
public function getContent()
{
$delivery_address = new Address((int)$this->order->id_address_delivery);
$formatted_delivery_address = AddressFormat::generateAddress($delivery_address, array(), '<br />', ' ');
$formatted_invoice_address = '';
if ($this->order->id_address_delivery != $this->order->id_address_invoice)
{
$invoice_address = new Address((int)$id_address_invoice);
$formatted_invoice_address = AddressFormat::generateAddress($invoice_address, array(), '<br />', ' ');
}
$this->smarty->assign(array(
'order_return' => $this->order_return,
'return_nb_days' => (int)Configuration::get('PS_ORDER_RETURN_NB_DAYS'),
'products' => OrderReturn::getOrdersReturnProducts($this->order_return->id, $this->order),
'delivery_address' => $formatted_delivery_address,
'invoice_address' => $formatted_invoice_address,
'shop_address' => AddressFormat::generateAddress($this->address, array(), '<br />', ' ')
));
return $this->smarty->fetch(_PS_THEME_DIR_.'/pdf/order-return.tpl');
}
/**
* Returns the template filename
* @return string filename
*/
public function getFilename()
{
return sprintf('%06d', $this->order_return->id).'.pdf'; // TODO
}
/**
* Returns the template filename when using bulk rendering
* @return string filename
*/
public function getBulkFilename()
{
return 'invoices.pdf';
}
}
+106
View File
@@ -0,0 +1,106 @@
<?php
/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 8797 $
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/**
* @since 1.5
*/
class HTMLTemplateOrderSlipCore extends HTMLTemplateInvoice
{
public $order;
public $order_slip;
public function __construct(OrderSlip $order_slip, $smarty)
{
$this->order_slip = $order_slip;
$this->order = new Order((int)$order_slip->id_order);
$products = OrderSlip::getOrdersSlipProducts($this->order_slip->id, $this->order);
$customizedDatas = Product::getAllCustomizedDatas((int)($this->order->id_cart));
Product::addCustomizationPrice($products, $customizedDatas);
$this->order->products = $products;
$this->smarty = $smarty;
// header informations
$this->date = Tools::displayDate($this->order->invoice_date, (int)$this->order->id_lang);
$this->title = self::l('Slip #').sprintf('%06d', $this->order_slip->id);
// footer informations
$shop = new Shop((int)$this->order->id_shop);
$this->address = $shop->getAddress();
}
/**
* Returns the template's HTML content
* @return string HTML content
*/
public function getContent()
{
$country = new Country((int)$this->order->id_address_invoice);
$invoice_address = new Address((int)$this->order->id_address_invoice);
$formatted_invoice_address = AddressFormat::generateAddress($invoice_address, array(), '<br />', ' ');
$formatted_delivery_address = '';
if ($this->order->id_address_delivery != $this->order->id_address_invoice)
{
$delivery_address = new Address((int)$this->order->id_address_delivery);
$formatted_delivery_address = AddressFormat::generateAddress($delivery_address, array(), '<br />', ' ');
}
$customer = new Customer($this->order->id_customer);
$this->smarty->assign(array(
'order' => $this->order,
'order_details' => $this->order->products,
'delivery_address' => $formatted_delivery_address,
'invoice_address' => $formatted_invoice_address,
'tax_excluded_display' => Group::getPriceDisplayMethod($customer->id_default_group),
'tax_tab' => '',
));
return $this->smarty->fetch(_PS_THEME_DIR_.'/pdf/invoice.tpl');
}
/**
* Returns the template filename when using bulk rendering
* @return string filename
*/
public function getBulkFilename()
{
return 'order-slips.pdf';
}
/**
* Returns the template filename
* @return string filename
*/
public function getFilename()
{
return 'order-slip-'.sprintf('%06d', $this->order_slip->id).'.pdf';
}
}
+101
View File
@@ -0,0 +1,101 @@
<?php
/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 8797 $
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/**
* @since 1.5
*/
class PDFCore
{
public $filename;
public $pdf_renderer;
public $objects;
public $template;
const TEMPLATE_INVOICE = 'Invoice';
const TEMPLATE_ORDER_RETURN = 'OrderReturn';
const TEMPLATE_ORDER_SLIP = 'OrderSlip';
const TEMPLATE_DELIVERY_SLIP = 'DeliverySlip';
public function __construct($objects, $template, $smarty)
{
$this->pdf_renderer = new PDFGenerator();
$this->template = $template;
$this->smarty = $smarty;
$this->objects = $objects;
if (!is_array($objects))
$this->objects = array($objects);
}
public function render()
{
$render = false;
$this->pdf_renderer->setFontForLang('fr');
foreach ($this->objects as $object)
{
$template = $this->getTemplateObject($object);
if (!$template)
continue;
if (empty($this->filename))
{
$this->filename = $template->getFilename();
if (count($this->objects) > 1)
$this->filename = $template->getBulkFilename();
}
$this->pdf_renderer->createHeader($template->getHeader());
$this->pdf_renderer->createFooter($template->getFooter());
$this->pdf_renderer->createContent($template->getContent());
$this->pdf_renderer->writePage();
$render = true;
unset($template);
}
if ($render)
$this->pdf_renderer->render($this->filename);
}
public function getTemplateObject($object)
{
$class = false;
$classname = 'HTMLTemplate'.$this->template;
if (class_exists($classname))
{
$class = new $classname($object, $this->smarty);
if (!($class instanceof HTMLTemplate))
throw new PrestashopException('Invalid class. It should be an instance of HTMLTemplate');
}
return $class;
}
}
+145
View File
@@ -0,0 +1,145 @@
<?php
require_once(_PS_TOOL_DIR_.'tcpdf/config/lang/eng.php');
require_once(_PS_TOOL_DIR_.'tcpdf/tcpdf.php');
/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 8797 $
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/**
* @since 1.5
*/
class PDFGeneratorCore extends TCPDF
{
const DEFAULT_FONT = 'dejavusans';
public $header;
public $footer;
public $content;
public $font;
public $font_by_lang = array('jp' => 'cid0jp');
/**
* set the PDF encoding
* @param string $encoding
*/
public function setEncoding($encoding)
{
$this->encoding = $encoding;
}
/**
*
* set the PDF header
* @param string $header HTML
*/
public function createHeader($header)
{
$this->header = $header;
}
/**
*
* set the PDF footer
* @param string $footer HTML
*/
public function createFooter($footer)
{
$this->footer = $footer;
}
/**
*
* create the PDF content
* @param string $content HTML
*/
public function createContent($content)
{
$this->content = $content;
}
/**
* Change the font
* @param unknown_type $iso_lang
*/
public function setFontForLang($iso_lang)
{
$this->font = self::DEFAULT_FONT;
if (array_key_exists($iso_lang, $this->font_by_lang))
$this->font = $this->font_by_lang[$iso_lang];
}
/**
* @see TCPDF::Header()
*/
public function Header()
{
$this->setFont($this->font);
$this->writehtml($this->header);
}
/**
* @see TCPDF::Footer()
*/
public function Footer()
{
$this->setFont($this->font);
$this->writehtml($this->footer);
}
/**
* Render the pdf file
*
* @param string $filename
* @throws PrestashopException
*/
public function render($filename)
{
if (empty($filename))
throw new PrestashopException('Missing filename.');
$this->lastPage();
$this->output($filename, 'I');
}
/**
* Write a PDF page
*/
public function writePage()
{
$this->SetHeaderMargin(5);
$this->SetFooterMargin(18);
$this->setMargins(10, 40, 10);
$this->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM);
$this->AddPage();
$this->writehtml($this->content, true, false, true, false, '');
}
}