Jump to content

PDF Invoice: How to add images and SKU to pdf?


Recommended Posts

Hello,

 

To do this we have to override a class named OrderInvoice which you will get in YOUR_ROOT_DIR/classes/order/OrderInvoice.php

Open it in a text editor. Now replace the following method to put the product image path

public function getProducts($products = false, $selectedProducts = false, $selectedQty = false)
    {
        if (!$products)
            $products = $this->getProductsDetail();

        $order = new Order($this->id_order);
        $customized_datas = Product::getAllCustomizedDatas($order->id_cart);

        $resultArray = array();
        foreach ($products as $row)
        {
            // Change qty if selected
            if ($selectedQty)
            {
                $row['product_quantity'] = 0;
                foreach ($selectedProducts as $key => $id_product)
                    if ($row['id_order_detail'] == $id_product)
                        $row['product_quantity'] = (int)($selectedQty[$key]);
                if (!$row['product_quantity'])
                    continue;
            }

            $this->setProductImageInformations($row);
            $this->setProductCurrentStock($row);
            $this->setProductCustomizedDatas($row, $customized_datas);

            // Add information for virtual product
            if ($row['download_hash'] && !empty($row['download_hash']))
            {
                $row['filename'] = ProductDownload::getFilenameFromIdProduct((int)$row['product_id']);
                // Get the display filename
                $row['display_filename'] = ProductDownload::getFilenameFromFilename($row['filename']);
            }
            
            $row['id_address_delivery'] = $order->id_address_delivery;
            
            /* Stock product */
            $resultArray[(int)$row['id_order_detail']] = $row;
        }

        if ($customized_datas)
            Product::addCustomizationPrice($resultArray, $customized_datas);

        return $resultArray;
    }

 

with

 

public function getProducts($products = false, $selectedProducts = false, $selectedQty = false)
    {
        if (!$products)
            $products = $this->getProductsDetail();

        $order = new Order($this->id_order);
        $customized_datas = Product::getAllCustomizedDatas($order->id_cart);

        /*ICode*/
        global $cookie;
        $link=new Link();
        /*ICode*/
        $resultArray = array();
        foreach ($products as $row)
        {
            // Change qty if selected
            if ($selectedQty)
            {
                $row['product_quantity'] = 0;
                foreach ($selectedProducts as $key => $id_product)
                    if ($row['id_order_detail'] == $id_product)
                        $row['product_quantity'] = (int)($selectedQty[$key]);
                if (!$row['product_quantity'])
                    continue;
            }

            $this->setProductImageInformations($row);
            $this->setProductCurrentStock($row);
            $this->setProductCustomizedDatas($row, $customized_datas);

            //ICode
            $product = new Product($row['id_product']);
            //echo "<pre>";print_r($row);echo "</pre>";
            $imageUrl= $link->getImageLink($product->link_rewrite[$cookie->id_lang], $row['image']->id, 'small_default');
            $tempArray=explode("/img/",$imageUrl);
            $row['image_path']=_PS_ROOT_DIR_."/img/".$tempArray[1];
            //ICdoe
            // Add information for virtual product
            if ($row['download_hash'] && !empty($row['download_hash']))
            {
                $row['filename'] = ProductDownload::getFilenameFromIdProduct((int)$row['product_id']);
                // Get the display filename
                $row['display_filename'] = ProductDownload::getFilenameFromFilename($row['filename']);
            }
            
            $row['id_address_delivery'] = $order->id_address_delivery;
            /* Stock product */
            $resultArray[(int)$row['id_order_detail']] = $row;
        }

        if ($customized_datas)
            Product::addCustomizationPrice($resultArray, $customized_datas);

        //echo "<pre>";print_r($resultArray);echo "</pre>";
        return $resultArray;
    }

 

Now you need to show it in the pdf. To do this open the invoice tpl file which you will find in YOUR_ROOT_DIR/pdf/invoice.tpl if you not change your default settings.

Now replace the following code

 

<td style="text-align: left; background-color: #4D4D4D; color: #FFF; padding-left: 10px; font-weight: bold; width: {if !$tax_excluded_display}35%{else}45%{/if}">{l s='Product / Reference' pdf='true'}</td>

 

with

 

<td style="text-align: left; background-color: #4D4D4D; color: #FFF; padding-left: 10px; font-weight: bold; width: {if !$tax_excluded_display}25%{else}35%{/if}">{l s='Product / Reference' pdf='true'}</td>
<td style="background-color: #4D4D4D; color: #FFF; text-align: right; font-weight: bold; width: 10%">{l s='Image' pdf='true'}</td>

 

 

and

 

 

<td style="text-align: left; width: {if !$tax_excluded_display}35%{else}45%{/if}">{$order_detail.product_name}</td>

 

with

 

<td style="text-align: left; width: {if !$tax_excluded_display}25%{else}35%{/if}">{$order_detail.product_name}</td>
<td style="text-align: left; width:10%"><img src="{$order_detail.image_path}" /></td>

 

Now if you download an invoice you will see the product image.

 

You will get the total tutorial here http://www.learnprestashop.com/?p=391

 

Thanks,

:)

  • Like 1

Share this post


Link to post
Share on other sites

Thanks. But it does not work.

 

I only checked quickly. But for example

$imageUrl

has the value: "http://bestbuy-china.com/pr/1458-small_default/High-Quality-Leather-Bracelet.jpg". So 

$tempArray[1]

is empty, since the '/img' string does not appear in $imageUrl.

 

Could that be part of the problem? I did everything like you suggested. Everything works, just no images.

Share this post


Link to post
Share on other sites

  • 2 weeks later...

Hello,

 

Hmm I know that might cause problem. Actually You need to work with the following code.

 

 $row['image_path']=_PS_ROOT_DIR_."/img/".$tempArray[1];

 

Here if you know the image path, you need to put the path,

 $row['image_path']=YOUR_PRODUCT_IMAGE_PATH;

 

In my system I can get the product image by the way which I have shown in the code. If you wish you can send me your url Then I may told you what to do.

 

Thanks,

Share this post


Link to post
Share on other sites

  • 1 month later...

Put 

/img/p/-small_default.jpg

 

in the dir and all working fine!

 

Thanks 

Hello, I read well this comment and it works trés well on 1.5.6.2

On the other hand I do not see or making your transformations :/img/p/-small_default.jpg ????

I do not understand either transformation(processing) of the comment 085 :$row['image_path']=_PS_ROOT_DIR_."/img/".$tempArray[1];

Can you tell me how to make. Thank you

Share this post


Link to post
Share on other sites

  • 2 weeks later...
  • 2 months later...

Hi,

 

The image printed in PDF, is the cover image. Is any possibility to get the second image, instead of the cover image ?

 

Regards,

 

Mario

Share this post


Link to post
Share on other sites

Hello,

 

To do this we have to override a class named OrderInvoice which you will get in YOUR_ROOT_DIR/classes/order/OrderInvoice.php

Open it in a text editor. Now replace the following method to put the product image path

public function getProducts($products = false, $selectedProducts = false, $selectedQty = false)

    {

        if (!$products)

            $products = $this->getProductsDetail();

 

        $order = new Order($this->id_order);

        $customized_datas = Product::getAllCustomizedDatas($order->id_cart);

 

        $resultArray = array();

        foreach ($products as $row)

        {

            // Change qty if selected

            if ($selectedQty)

            {

                $row['product_quantity'] = 0;

                foreach ($selectedProducts as $key => $id_product)

                    if ($row['id_order_detail'] == $id_product)

                        $row['product_quantity'] = (int)($selectedQty[$key]);

                if (!$row['product_quantity'])

                    continue;

            }

 

            $this->setProductImageInformations($row);

            $this->setProductCurrentStock($row);

            $this->setProductCustomizedDatas($row, $customized_datas);

 

            // Add information for virtual product

            if ($row['download_hash'] && !empty($row['download_hash']))

            {

                $row['filename'] = ProductDownload::getFilenameFromIdProduct((int)$row['product_id']);

                // Get the display filename

                $row['display_filename'] = ProductDownload::getFilenameFromFilename($row['filename']);

            }

            

            $row['id_address_delivery'] = $order->id_address_delivery;

            

            /* Stock product */

            $resultArray[(int)$row['id_order_detail']] = $row;

        }

 

        if ($customized_datas)

            Product::addCustomizationPrice($resultArray, $customized_datas);

 

        return $resultArray;

    }

 

with

 

public function getProducts($products = false, $selectedProducts = false, $selectedQty = false)

    {

        if (!$products)

            $products = $this->getProductsDetail();

 

        $order = new Order($this->id_order);

        $customized_datas = Product::getAllCustomizedDatas($order->id_cart);

 

        /*ICode*/

        global $cookie;

        $link=new Link();

        /*ICode*/

        $resultArray = array();

        foreach ($products as $row)

        {

            // Change qty if selected

            if ($selectedQty)

            {

                $row['product_quantity'] = 0;

                foreach ($selectedProducts as $key => $id_product)

                    if ($row['id_order_detail'] == $id_product)

                        $row['product_quantity'] = (int)($selectedQty[$key]);

                if (!$row['product_quantity'])

                    continue;

            }

 

            $this->setProductImageInformations($row);

            $this->setProductCurrentStock($row);

            $this->setProductCustomizedDatas($row, $customized_datas);

 

            //ICode

            $product = new Product($row['id_product']);

            //echo "<pre>";print_r($row);echo "</pre>";

            $imageUrl= $link->getImageLink($product->link_rewrite[$cookie->id_lang], $row['image']->id, 'small_default');

            $tempArray=explode("/img/",$imageUrl);

            $row['image_path']=_PS_ROOT_DIR_."/img/".$tempArray[1];

            //ICdoe

            // Add information for virtual product

            if ($row['download_hash'] && !empty($row['download_hash']))

            {

                $row['filename'] = ProductDownload::getFilenameFromIdProduct((int)$row['product_id']);

                // Get the display filename

                $row['display_filename'] = ProductDownload::getFilenameFromFilename($row['filename']);

            }

            

            $row['id_address_delivery'] = $order->id_address_delivery;

            /* Stock product */

            $resultArray[(int)$row['id_order_detail']] = $row;

        }

 

        if ($customized_datas)

            Product::addCustomizationPrice($resultArray, $customized_datas);

 

        //echo "<pre>";print_r($resultArray);echo "</pre>";

        return $resultArray;

    }

 

Now you need to show it in the pdf. To do this open the invoice tpl file which you will find in YOUR_ROOT_DIR/pdf/invoice.tpl if you not change your default settings.

Now replace the following code

 

<td style="text-align: left; background-color: #4D4D4D; color: #FFF; padding-left: 10px; font-weight: bold; width: {if !$tax_excluded_display}35%{else}45%{/if}">{l s='Product / Reference' pdf='true'}</td>

 

with

 

<td style="text-align: left; background-color: #4D4D4D; color: #FFF; padding-left: 10px; font-weight: bold; width: {if !$tax_excluded_display}25%{else}35%{/if}">{l s='Product / Reference' pdf='true'}</td>

<td style="background-color: #4D4D4D; color: #FFF; text-align: right; font-weight: bold; width: 10%">{l s='Image' pdf='true'}</td>

 

 

and

 

 

<td style="text-align: left; width: {if !$tax_excluded_display}35%{else}45%{/if}">{$order_detail.product_name}</td>

 

with

 

<td style="text-align: left; width: {if !$tax_excluded_display}25%{else}35%{/if}">{$order_detail.product_name}</td>

<td style="text-align: left; width:10%"><img src="{$order_detail.image_path}" /></td>

 

Now if you download an invoice you will see the product image.

 

You will get the total tutorial here http://www.learnprestashop.com/?p=391

 

Thanks,

:)

Hi,

 

The image printed in PDF, is the cover image. Is any possibility to get the second image, instead of the cover image ? :(

 

Regards,

 

Mario

Share this post


Link to post
Share on other sites

  • 4 weeks later...
  • 1 month later...

hello i have this error :

 

TCPDF ERROR: [image] Unable to get image: /home/etkslo/public_html/img/

 

 

i use ps 1.5.6.1 

 

I followed this tutorial 

 

On a 1.5.4.1 version i got the same error,

 

try to do this, it should work :

//ICode
 $product = new Product($row['id_product']);
 //echo "<pre>";print_r($row);echo "</pre>";
 $imageUrl= $link->getImageLink($product->link_rewrite[$cookie->id_lang], $row['image']->id, 'medium_default');
 $tempArray=explode("/img/",$imageUrl);
 $row['image_path']=$tempArray[0];
 //ICdoe

As you can see, the $tempArray[1] is modified to $tempArray[0]

i also changed "small-default" image type to "medium-default", because the small image has a really too low definition to be printed nicely.

 

Enjoy !

Edited by ZenGraph (see edit history)

Share this post


Link to post
Share on other sites

  • 4 weeks later...

Damn I wish I scrolled to the bottom of this and read zengraph's post. spent like an hour debugging the code at the top to end up with the same thing you have....

 

anyways also notice in zengraph's code it removes the _PS_ROOT_DIR_  from $row['image_path']

  • Like 1

Share this post


Link to post
Share on other sites

  • 4 weeks later...
  • 4 months later...

In Prestashop 1.6, in order to add product image to invoice, i used 

$row['image_path']=$imageUrl;

instead of 

 $row['image_path']=_PS_ROOT_DIR_."/img/".$tempArray[1];

and it worked fine!

yes,thank you ,

 

 

 

//add product image  2/
                        $product = new Product($row['id_product']);
                        $imageUrl= $link->getImageLink($product->link_rewrite[$cookie->id_lang], $row['image']->id, 'small_default');

Share this post


Link to post
Share on other sites

  • 3 months later...
  • 3 months later...

Hi,

 

 After follow the code posted here by limon994, I not get the second image to my PDF. PrestaShop version: 1.5.6.2 

 

when I play with array position, only the cover image appear. I have 2 images associated with product.

 

 $row['image_path']=_PS_ROOT_DIR_."/img/".$tempArray[0]; gives an error:

CBVFNWVZXTCPDF ERROR: [image] Unable to get image: /home/agriloja/public_html/agricor.es/procurement/img/agricor.es/procurement

 

 $row['image_path']=_PS_ROOT_DIR_."/img/".$tempArray[1]; gives me the cover image

 

$row['image_path']=_PS_ROOT_DIR_."/img/".$tempArray[2]; gives an error:

GZOJBSSMBTCPDF ERROR: [image] Unable to get image: /home/agriloja/public_html/agricor.es/procurement/img/

 

 

Anyone help me?

 

regards,

Mario

Share this post


Link to post
Share on other sites

Hi,

 

 After follow the code posted here by limon994, I not get the second image to my PDF. PrestaShop version: 1.5.6.2 

 

when I play with array position, only the cover image appear. I have 2 images associated with product.

 

 $row['image_path']=_PS_ROOT_DIR_."/img/".$tempArray[0]; gives an error:

CBVFNWVZXTCPDF ERROR: [image] Unable to get image: /home/agriloja/public_html/agricor.es/procurement/img/agricor.es/procurement

 

 $row['image_path']=_PS_ROOT_DIR_."/img/".$tempArray[1]; gives me the cover image

 

$row['image_path']=_PS_ROOT_DIR_."/img/".$tempArray[2]; gives an error:

GZOJBSSMBTCPDF ERROR: [image] Unable to get image: /home/agriloja/public_html/agricor.es/procurement/img/

 

 

Anyone help me?

 

regards,

Mario

 

Hi,

Did you look here?

Share this post


Link to post
Share on other sites

 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More