Skip to content

Categories:

PDF for OpenCart

After many request for the code (very simple) for output in pdf of the invoice, i publish this simple how to valid until 1.3 version.

NB: this code is build for the italian version (differences are in fileds of the table order)

1) download the dompdf library (search in Google, or go to http://www.digitaljunkies.ca/dompdf/ or get it here)

2) unzip and create in the rooot the dompdf folder

3) edit catalogo/account/invoice.php and add this lines

public function pdf(){
if (!$this->customer->isLogged()) {
$this->session->data['redirect'] = $this->url->https(’account/account’);

$this->redirect($this->url->https(’account/login’));
}

$this->load->language(’account/order’);

$this->data['title'] = $this->language->get(’heading_title’) . ‘ #’ . $this->request->get['order_id'];
$this->data['base'] = (@$this->request->server['HTTPS'] != ‘on’) ? HTTP_SERVER : HTTPS_SERVER;
$this->data['direction'] = $this->language->get(’direction’);
$this->data['language'] = $this->language->get(’code’);

$this->data['text_invoice'] = $this->language->get(’text_invoice’);
$this->data['text_invoice_date'] = $this->language->get(’text_invoice_date’);
$this->data['text_invoice_no'] = $this->language->get(’text_invoice_no’);
$this->data['text_telephone'] = $this->language->get(’text_telephone’);
$this->data['text_fax'] = $this->language->get(’text_fax’);
$this->data['text_to'] = $this->language->get(’text_to’);
$this->data['text_ship_to'] = $this->language->get(’text_ship_to’);

$this->data['column_product'] = $this->language->get(’column_product’);
$this->data['column_model'] = $this->language->get(’column_model’);
$this->data['column_quantity'] = $this->language->get(’column_quantity’);
$this->data['column_price'] = $this->language->get(’column_price’);
$this->data['column_total'] = $this->language->get(’column_total’);

$this->load->model(’account/order’);

$order_info = $this->model_account_order->getOrder($this->request->get['order_id']);

$this->data['order_id'] = $order_info['order_id'];
$this->data['date_added'] = date(’d/m/Y’, strtotime($order_info['date_added']));
//for manage order number
$this->data['invoice_number'] = $order_info['invoice_number'];
$this->data['invoice_data'] = date(’d/m/Y’, strtotime($order_info['invoice_data']));

$this->data['store'] = $this->config->get(’config_store’);
$this->data['address'] = nl2br($this->config->get(’config_address’));
$this->data['telephone'] = $this->config->get(’config_telephone’);
$this->data['fax'] = $this->config->get(’config_fax’);
$this->data['email'] = $this->config->get(’config_email’);
$this->data['website'] = trim(HTTP_SERVER, ‘/’);

if ($order_info['shipping_address_format']) {
$format = $order_info['shipping_address_format'];
} else {
$format = ‘{firstname} {lastname}’ . “\n” . ‘{company}’ . “\n” . ‘{address_1}’ . “\n” . ‘{address_2}’ . “\n” . ‘{city} {postcode}’ . “\n” . ‘{zone}’ . “\n” . ‘{country}’;
}

$find = array(
‘{firstname}’,
‘{lastname}’,
‘{company}’,
‘{address_1}’,
‘{address_2}’,
‘{city}’,
‘{postcode}’,
‘{zone}’,
‘{country}’
);

$replace = array(
‘firstname’ => $order_info['shipping_firstname'],
‘lastname’ => $order_info['shipping_lastname'],
‘company’ => $order_info['shipping_company'],
‘address_1′ => $order_info['shipping_address_1'],
‘address_2′ => $order_info['shipping_address_2'],
‘city’ => $order_info['shipping_city'],
‘postcode’ => $order_info['shipping_postcode'],
‘zone’ => $order_info['shipping_zone'],
‘country’ => $order_info['shipping_country']
);

$this->data['shipping_address'] = str_replace(array(”\r\n”, “\r”, “\n”), ‘
‘, preg_replace(array(”/\s\s+/”, “/\r\r+/”, “/\n\n+/”), ‘
‘, trim(str_replace($find, $replace, $format))));

if ($order_info['payment_address_format']) {
$format = $order_info['payment_address_format'];
} else {
//FOR ITALIAN DISTRO ONLY

//$format = ‘{firstname} {lastname}’ . “\n” . ‘{codicefiscale}’ .”\n” . ‘{partitaiva}’ . “\n” . ‘{company}’ . “\n” . ‘{address_1}’ . “\n” . ‘{address_2}’ . “\n” . ‘{city} {postcode}’ . “\n” . ‘{zone}’ . “\n” . ‘{country}’;

$format = ‘{firstname} {lastname}’ . “\n” .  ’{company}’ . “\n” . ‘{address_1}’ . “\n” . ‘{address_2}’ . “\n” . ‘{city} {postcode}’ . “\n” . ‘{zone}’ . “\n” . ‘{country}’;
}

$find = array(
‘{firstname}’,
‘{lastname}’,
//ITALIAN ‘{codicefiscale}’,
//ITALIAN ‘{partitaiva}’,
‘{company}’,
‘{address_1}’,
‘{address_2}’,
‘{city}’,
‘{postcode}’,
‘{zone}’,
‘{country}’
);

$replace = array(
‘firstname’ => $order_info['payment_firstname'],
‘lastname’ => $order_info['payment_lastname'],
//ITALIAN ‘codicefiscale’ => $order_info['shipping_codicefiscale'],
//ITALIAN ‘partitaiva’ => ($order_info['shipping_partitaiva']==”"?NULL:$order_info['shipping_partitaiva']),
‘company’ => $order_info['payment_company'],
‘address_1′ => $order_info['payment_address_1'],
‘address_2′ => $order_info['payment_address_2'],
‘city’ => $order_info['payment_city'],
‘postcode’ => $order_info['payment_postcode'],
‘zone’ => $order_info['payment_zone'],
‘country’ => $order_info['payment_country']
);

$this->data['payment_address'] = str_replace(array(”\r\n”, “\r”, “\n”), ‘
‘, preg_replace(array(”/\s\s+/”, “/\r\r+/”, “/\n\n+/”), ‘
‘, trim(str_replace($find, $replace, $format))));

$this->data['products'] = array();

$products = $this->model_account_order->getOrderProducts($this->request->get['order_id']);

foreach ($products as $product) {
$options = $this->model_account_order->getOrderOptions($this->request->get['order_id'], $product['order_product_id']);

$option_data = array();

foreach ($options as $option) {
$option_data[] = array(
‘name’ => $option['name'],
‘value’ => $option['value']
);
}

$this->data['products'][] = array(
‘name’ => $product['name'],
‘model’ => $product['model'],
‘option’ => $option_data,
‘quantity’ => $product['quantity'],
‘price’ => $this->currency->format($product['price'], $order_info['currency'], $order_info['value']),

//’price’ => $this->currency->format($product['price'], $order_info['currency'], $order_info['value']),
‘discount’ => (ceil($product['discount']) ? $this->currency->format($product['price'] – $product['discount'], $order_info['currency'], $order_info['value']) : NULL),
‘total’ => $this->currency->format($product['total'], $order_info['currency'], $order_info['value'])
);
}

$this->data['totals'] = $this->model_account_order->getOrderTotals($this->request->get['order_id']);

$this->template = $this->config->get(’config_template’) . ‘account/order_invoice.tpl’;
//print the pdf after encoding charset
$a=($this->to_pdf());
$a=utf8_decode($a);
$a=html_entity_decode((htmlentities($a, ENT_QUOTES, ‘ISO-8859-1′)), ENT_QUOTES);
//FOR EURO ONLY $a=str_replace(”?”,”€”,$a);
require_once(”dompdf/dompdf_config.inc.php”);
if ( get_magic_quotes_gpc() )
$a = stripslashes($a);
$old_limit = ini_set(”memory_limit”, “16M”);
$dompdf = new DOMPDF();
$dompdf->load_html($a);
$dompdf->set_paper(”a4″, “portrait”);
$dompdf->render();
$dompdf->stream(”invoice.pdf”);
exit(0);
}

4) Into system/engine/controller.php add this function

//aggiunta per stampa pdf
protected function to_pdf() {
foreach ($this->children as $child) {
$file = DIR_APPLICATION . ‘controller/’ . $child . ‘.php’;
$class = ‘Controller’ . preg_replace(’/[^a-zA-Z0-9]/’, ”, $child);

if (file_exists($file)) {
require_once($file);

$controller = new $class();

$controller->index();

$this->data[$controller->id] = $controller->output;
} else {
exit(’Error: Could not load controller ‘ . $child . ‘!’);
}
}

$this->output = $this->fetch($this->template);

if ($this->layout) {
$file = DIR_APPLICATION . ‘controller/’ . $this->layout . ‘.php’;
$class = ‘Controller’ . preg_replace(’/[^a-zA-Z0-9]/’, ”, $this->layout);

if (file_exists($file)) {
require_once($file);

$controller = new $class();

$controller->data[$this->id] = $this->output;

$controller->index();

$this->output = $controller->output;
} else {
exit(’Error: Could not load controller ‘ . $this->layout . ‘!’);
}
}
return $this->output;
//$this->response->setOutput($this->output);
}

5) Edit the account/invoice.tpl file and add the line for create the button

<a onclick=”location=’index.php?route=account/invoice/pdf&amp;order_id=<?php echo $order_id; ?>’”><span>PDF</span></a>

after <a href=”index.php?route=account/invoice/print_invoice&amp;order_id=<?php echo $order_id; ?>” target=”_blank”><span><?php echo $text_print?></span></a>

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.