Home Communication board WiKi Contact us

X-Cart PDF catalog module

This module is allow to generate the PDF catalog from your X-Cart data. The code is based on the TCPDF PHP class.

  • You should create the <x-cart>/pdf dir and create the following 3 files inside. In additional the smarty plugin should be uploaded too.
    • pdf/auth.php
    • pdf/func.pdf.php
    • pdf/pdf_catalog.php
    • include/templater/plugins/function.get_categories_list.php
  • At the old X-Cart (4.x), you should move your images to the file system. That's default in the newest X-Cart versions.

Please see the file content under this article.

  • Now you should add 2 new language var: lbl_pdf_catalog_title and lbl_index. It's possible to make in the patch section of the admin area with the following sql:
INSERT INTO `xcart_languages` VALUES ('US','lbl_index','Index','Labels','Y');
INSERT INTO `xcart_languages` VALUES ('US','lbl_pdf_catalog','PDF catalog','Labels','Y');
  • Please check the permission for the x-cart/catalog dir. It's should be writable. You can change the permissions by the “chmod 777 catalog” command.
  • Now you are able to run the catalog generation by typing <you site url>/pdf/pdf_catalog.php, the pdf files will be generated to the “catalog” dir for each language.

Multi-language support

For the multi-language support it is required to have the appropriate font(s) for the TCPDF. If you are using the UTF-8 codepage, as suggested, you can simply download the following archive and unpack it to the include/tcpdf/fonts (it should be created already since you have the tcpdf uploaded). The file is pretty big - because the exotic languages are included to these fonts. Download

PDF catalog PHP files

x-cart/pdf/auth.php

<?php
define('AREA_TYPE', 'C');

@include_once "../top.inc.php";
if (!defined('DIR_CUSTOMER')) die("ERROR: Can not initiate application! Please check configuration.");

require_once $xcart_dir."/init.php";

x_load("backoffice");

$current_area="C";

include $xcart_dir."/include/get_language.php";

require $xcart_dir."/include/admin_security.php";

@include $xcart_dir."/modules/gold_auth.php";
include $xcart_dir."/include/check_useraccount.php";

#
# Single Mode always active for root account
#
if($user_account["flag"]=="RP") $single_mode=true;

x_session_save();
?>

You should be careful with the file below. We are selecting the data here for the catalogue and unfortunately the X-Cart API is changed from version to version. As the result the file is suitable for the last X-Cart version only. All of the difference with the old versions are marked by the ”# version” comment. You should review the code and uncomment the appropriate lines if it's required for your version.

x-cart/pdf/func.pdf.php

<?php
if (!defined('XCART_START')) die();

set_time_limit(86400);
func_set_memory_limit('512M');
error_reporting (E_ALL ^ E_NOTICE);

include_once $xcart_dir.'/include/tcpdf/config/lang/eng.php';
include_once $xcart_dir.'/include/tcpdf/tcpdf.php';

function func_get_pdf_data($categoryid) {
    global $xcart_dir, $smarty, $sql_tbl, $shop_language;

    $categories = func_get_categories_list($categoryid, true, 'level');
# kornev, for the old x-cart 
    if (is_array($categories['subcategories'])) $categories = $categories['subcategories'];

    $current_area = 'C';
    if ($categories)
    foreach($categories as $ink=>$v) {
        $search_data["products"] = array();
        $search_data["products"]["categoryid"] = $v['categoryid'];
        $search_data["products"]["search_in_subcategories"] = "";
        $search_data["products"]["category_main"] = "Y";
        $search_data["products"]["category_extra"] = "Y";
        $search_data["products"]["forsale"] = "Y";

        $do_not_use_navigation = 1;
        $sort = 'productcode';
        $sort_direction = $config['Appearance']['products_order_asc'] == 'Y';
        $mode = "search";
        include $xcart_dir."/include/search.php";

        $categories[$ink]['products'] = $products;
    }

    return $categories;
}

class MYPDF extends TCPDF {
        var $__products_links_counter = 1;

# kornev, removed in new pdf
        public function convertHTMLColorToDec($str) {
            return TCPDF_COLORS::convertHTMLColorToDec($str, $this->spot_colors);
        }

        public function H($text, $level = 1, $break = false) {
            $this->Bookmark($text, $level-1);

            $this->SetTextColorArray($this->convertHTMLColorToDec('#ffffff'));
            $current_font = $this->FontSizePt;
            $this->SetFontSize(16-$level);

            $this->y += 2;
            if ($level == 1) {
                $this->SetFillColorArray($this->convertHTMLColorToDec('#5f94c0'));
                $this->MultiCell(0, 0, $text, 0, 'L', 1);
            }
            else {
                $this->SetFillColorArray($this->convertHTMLColorToDec('#5f94c0'));
                $this->MultiCell($this->GetStringWidth($text)+3, 0, $text, 0, 'L', 1);
            }
            $this->y += 2;

            $this->SetFontSize($current_font);
            $this->SetTextColorArray($this->convertHTMLColorToDec('#000000'));
            $this->SetFillColorArray($this->convertHTMLColorToDec('#ffffff'));
        }

        public function Footer() {
            global $config;

            $cur_y = $this->GetY();
            $ormargins = $this->getOriginalMargins();
            $this->SetTextColor(0, 0, 0);

            $this->SetX($ormargins['right']);
            $this->Cell(0, 0, func_get_langvar_by_name('lbl_pdf_catalog', null, false, true), 0, 0, 'L');

            $this->SetX($ormargins['right']);
            $this->Cell(0, 0, strftime($config["Appearance"]["date_format"], time()), 0, 0, 'R');

            $pagenumtxt = " - ".$this->PageNo().' / '.$this->getAliasNbPages()." - ";
            $this->SetY($cur_y);
            //Print page number
            $this->SetX($ormargins['center']);
            $this->Cell(0, 0, $pagenumtxt, 0, 0, 'C');
        }

        function product($product) {
            global $xcart_dir, $shop_language, $http_location;

            $current_y = $this->y;
            $current_x = $this->x;

            $link = $this->AddLink();
            $this->SetLink($link, $current_y, $this->getPage());
            $this->__products_links[$product['productid']] = $link;

# in some of the versions the tmbn_image is not available
# trying to find the image by the url
            if ($product['tmbn_image'])
                $file = $xcart_dir.'/'.$product['tmbn_image'];
            elseif ($product['tmbn_url'])
                $file = $xcart_dir.'/'.trim(preg_replace('/'.preg_quote($http_location, '/').'/', '', $product['tmbn_url']), '/');
            else
                $file = '';

            $width_image = $width = $this->pixelsToUnits(116);
            $height_image = $height = $width*193/145;

            if (is_file($file)) {
                $image_params = @func_get_image_size($file);
                $height_image = $image_params[2] * $width_image / $image_params[1];
                if ($height_image > $height) {
                    $height_image = $height;
                    $width_image = $image_params[1] * $height_image / $image_params[2];
                }
            }
            $px1 = $this->pixelsToUnits(1);
            $this->Rect($this->x, $this->y, $width+2*$px1, $height+2*$px1, 'D', 0, $this->convertHTMLColorToDec('#acb7c7'));
            if (is_file($file))
                $this->Image($file, $this->x+$px1, $this->y+$px1, $width_image, $height_image);

            $this->y +=  $height+4*$px1;

            $this->Cell($this->pixelsToUnits(30), 0, $product['productcode']);
            $this->y = $current_y + $height + $this->pixelsToUnits(15);
            $this->x = $current_x;
            $this->MultiCell($width, 0, $product['product'], 0, 'L');

            $this->y = $current_y;
            $this->x += $current_x + $width-$this->pixelsToUnits(5);
        }

        function end_product_row() {
            $this->y +=  $this->pixelsToUnits(230);
            $current_y = $this->y;
            $this->x = $this->lMargin;
            $this->checkPageBreak($this->pixelsToUnits(210));
            if ($current_y > $this->y) $this->y = $this->tMargin;
        }

        function add_product_link($product) {
            $this->SetTextColorArray($this->convertHTMLColorToDec('#0000ff'));

            $link = $this->__products_links[$product['productid']];
            if (!$link) return;

            $this->Cell($this->pixelsToUnits(50), 0, $product['productcode'], 0, 0, 'L', 0, $link);
            if ($this->__products_links_counter % 10 == 0) {
                $this->Cell(0, 0, '', 0, 1);
                $this->y += 1;
            }
            $this->__products_links_counter++;
        }
}

function func_pdf_draw_categories(&$pdf, $categoryid) {
    global $app_die;

    $categories = func_get_pdf_data($categoryid);
    $counter = 0;
    if (is_array($categories))
    foreach($categories as $ind=>$cat) {
        $pdf->H($cat['category'], $cat['level'], ($counter != 0 && $cat['products']));
        $counter++;
        if ($cat['products']) {
            $products = array_chunk($cat['products'], 4, true);
            $size = count($products) - 1;
            foreach($products as $k => $row) {
                foreach($row as $product)
                    $pdf->product($product);
                if ($size != $k)
                    $pdf->end_product_row();
                else
                    $pdf->AddPage();
            }
        }
        func_pdf_draw_categories($pdf, $cat['categoryid']);
        unset($categories[$ind]);
    }
}

function func_pdf_generate($template, $save_to_file) {
    global $config, $smarty, $xcart_dir, $sql_tbl;
    global $shop_language;

    //$html = func_display($template, $smarty, false);
    include_once $xcart_dir.'/include/templater/plugins/function.get_categories_list.php';
    $font = null;//'arial';

    $pdf = new MYPDF('A4', 'mm', 'P', true);

    $pdf->setJPEGQuality(100);
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor($config['Company']['company_name']);

    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(true);
    $pdf->setFooterFont(Array($font, '', '10'));
    $pdf->SetMargins('10', '10', '10');
    $pdf->SetFooterMargin(10);

    $pdf->setImageScale(1);

    $pdf->setLanguageArray($l);

//    $pdf->AliasNbPages();

    $pdf->SetFont($font, '', 8);
    $pdf->AddPage();
    $pdf->SetAutoPageBreak(true, 10);

    func_pdf_draw_categories($pdf, 0);

    $search_data["products"] = array();
    $search_data["products"]["forsale"] = "Y";

    $do_not_use_navigation = 1;
    $sort = 'productcode';
    $sort_direction = $config['Appearance']['products_order_asc'] == 'Y';
    $mode = "search";
    include $xcart_dir."/include/search.php";
    if ($products) {
        $pdf->AddPage();
        $pdf->H(func_get_langvar_by_name('lbl_index', null, false, true));
        foreach($products as $product)
            $pdf->add_product_link($product);
    }

    $pdf->Output($save_to_file, 'F');
}
?>

x-cart/pdf/pdf_catalog.php

<?php
include_once './auth.php';
if (is_file($xcart_dir."/include/common.php"))
    include_once $xcart_dir."/include/common.php";
# for the 3.x branch - the categories should be used
else 
    include_once $xcart_dir."/include/categories.php";
include_once $xcart_dir."/pdf/func.pdf.php";

$saved_shop_language = $shop_language;
$smarty->assign('xcart_dir', $xcart_dir);
foreach($all_languages as $lng) {
    $shop_language = $lng['code'];
    $smarty->assign('shop_language', $shop_language);
    $smarty->assign("content_language", $config["language_content"][$shop_language]);

    func_pdf_generate('main/pdf_catalog/catalog.tpl', $xcart_dir.'/catalog/pdf_catalog_'.$lng['code'].'.pdf');
}
$shop_language = $saved_shop_language;
?>

include/templater/plugins/function.get_categories_list.php

<?php
function smarty_function_get_categories_list($params, &$smarty) {
    global $app_dir, $sql_tbl, $config;

    extract($params);

    if (!isset($categoryid)) {
        $smarty->trigger_error("assign: missing 'categoryid' parameter");
        return;
    }

    $tmp = func_get_categories_list($categoryid, true, 'level', true);
    $categories = $tmp['all_categories'];
    $_inner_search = 1;
    if ($categories)
    foreach($categories as $ink=>$v) {
        $search_data["products"] = array();
        $search_data["products"]["categoryid"] = $v['categoryid'];
        $search_data["products"]["search_in_subcategories"] = "";
        $search_data["products"]["category_main"] = "Y";
        $search_data["products"]["category_extra"] = "Y";
        $search_data["products"]["forsale"] = "Y";

        $view_all = 'Y';
        $sort = $config["Appearance"]["products_order"];
        $sort_direction = $config['Appearance']['products_order_asc'] == 'Y';
        $mode = "search";
        include $app_dir."/include/search.php";

        $categories[$ink]['products'] = $products;
    }
    $smarty->assign('categories', $categories);
}
?>

Changelog

27 Dec 2013 - Adaptation for the new php versions 28 Jun 2011 - Adaptation for X-Cart v4.4.x and tcpdf v5.9

 
Home About us Privacy statement Terms & Conditions Refund policy © 2007–2019 ArsCommunity