Jump to content

[Solved] Add Image To PDF Invoice


Recommended Posts

Hi

I am trying to retreieve an image from the /img folder related to the product by product_id & id_image

I get this far but the id_image is 0 and should be 215 which is the records id_image number.

The code I am using is:-

$this->Image(_PS_IMG_DIR_.'p/'.(int)($product['product_id']).'-'.(int)($image['id_image']).'-small.jpg', 15);

and I am getting this error:-

FPDF error: Missing or incorrect image file: /xxx/xxx/xxx/screentekdesigns.com/img/p/52-0-small.jpg

Just need to get the id_image number relating to the product id within the ps_image table.

Anyone can help much appreciated.

Thanks


BETTER SOLUTION AT POST #4

Link to comment
Share on other sites

Hi

Have browsed my img/p/ directory and the makup of the image files are as so:-

eg. 52-215-small.jpg

The 52 is the id_product, the 215 is the id_image in ps_image table and selecting the small version of image created there are others for the product like home,large,medium,thickbox and watermark

Thanks

Link to comment
Share on other sites

Hi Everyone

After the above was long and tiresome, I found a better way and used the product_mini_??.jpg in the temp img/tmp directory

using the following code in the PDF file.

$this->Image(_PS_IMG_DIR_.'tmp/product_mini_'.(int)($product['product_id']).'.jpg', 25);

instant solution. it works better

Hope this helps.

Link to comment
Share on other sites

UPDATE

Added a file exist senario incase no file exists as below to be put in PDF.php file in the classes folder under your admin folder

if (file_exists(_PS_IMG_DIR_.'tmp/product_mini_'.(int)($product['product_id']).'.jpg'))
$this->Image(_PS_IMG_DIR_.'tmp/product_mini_'.(int)($product['product_id']).'.jpg', 25);
else
$this->Image(_PS_IMG_DIR_.'tmp/noimage.jpg', 25);

Link to comment
Share on other sites

Hello dear, thanks a lot for your real smart work. I understood the logic and I think I can try embedding the code, however I am very sure where I should embed this code.!

if (file_exists(PS_IMG_DIR.‘tmp/product_mini_’.(int)($product[‘product_id’]).’.jpg’))
$this->Image(PS_IMG_DIR.‘tmp/product_mini_’.(int)($product[‘product_id’]).’.jpg’, 25); else
$this->Image(PS_IMG_DIR.‘tmp/noimage.jpg’, 25);


I found file called pdf.php just under the admin folder and also AdminPDF.php in Tabs in Admin. Can you please let me know where I should insert this code?

Your answer would be so much appreciated.

Thanks a lot.

Link to comment
Share on other sites

Hi

Glad you can you use it, works a treat for us.

Ok, you need the pdf.php file in the classes folder. Open the file and search for

displayPrice($final_price

we inserted the script just below that statement, we had to rearrange the columns here and there to align the other fields

columns we have is product picture, product name, reference, weight and quantity.

Screenshot below

45957_9746Pf6yc1s5lmhj6yZs_t

Link to comment
Share on other sites

  • 2 weeks later...

From this topic, I built a working solution to display the product "cover" image, in place of the product reference that I don't use. (To move the image, just change 110 to another x value)

Here is the modified code of my /classes/PDF.php : line #776 (Prestashop 1.4)

               if ($productQuantity)
               {
                   $before = $this->GetY();
                   $this->MultiCell($w[++$i], 5, Tools::iconv('utf-8', self::encoding(), $product['product_name']), 'B');
                   $lineSize = $this->GetY() - $before;
                   $this->SetXY($this->GetX() + $w[0] + ($delivery ? 15 : 0), $this->GetY() - $lineSize);
                   $this->Cell($w[++$i], $lineSize, ($product['product_reference'] ? $product['product_reference'] : ''), 'B');

                    if (!$delivery)
                       $this->Cell($w[++$i], $lineSize, (self::$orderSlip ? '-' : '').self::convertSign(Tools::displayPrice($unit_price, self::$currency, true, false)), 'B', 0, 'R');
                   $this->Cell($w[++$i], $lineSize, $productQuantity, 'B', 0, 'C');
                   if (!$delivery)
                       $this->Cell($w[++$i], $lineSize, (self::$orderSlip ? '-' : '').self::convertSign(Tools::displayPrice($final_price, self::$currency, true, false)), 'B', 0, 'R');

                   //Modif : Ajout photo sur pdf
                   $cover = Product::getCover((int)($product['product_id']));
                   if(is_array($cover)) $product = array_merge($cover, $product);
                   $limage = _PS_IMG_DIR_.'p/'.(int)($product['product_id']).'-'.(int)($product['id_image']).'-medium.jpg';
                   if (file_exists($limage)){
                       $this->Image($limage,110,null,13);
                   } else{
                       $this->Image(_PS_IMG_DIR_.'/p/noimage.jpg',110,null,13);
                   }

                   $this->Ln();
               }



Hope this will help.

Link to comment
Share on other sites

  • 2 weeks later...
  • 1 month later...

Hi,How do you get this space?Look at the pics plz.I don't have the same result, all the line is tightened and the image overtakes of the line.

<br/>

<br/>EDIT : SOLVED

 

 

How did you solve the issue? i have the same issue?

 

Kindest regards

Link to comment
Share on other sites

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

Hi

 

Sorry for the hijack - but you folk seem to know all about the PDF file. I am looking for a way to change the font of the reference number to display as a barcode. I have barcode ttf files. Any idea how I can incorporate this?

Link to comment
Share on other sites

  • 3 months later...
  • 5 months later...
  • 3 weeks later...

UPDATE

 

Added a file exist senario incase no file exists as below to be put in PDF.php file in the classes folder under your admin folder

 

if (file_exists(_PS_IMG_DIR_.'tmp/product_mini_'.(int)($product['product_id']).'.jpg'))

$this->Image(_PS_IMG_DIR_.'tmp/product_mini_'.(int)($product['product_id']).'.jpg', 25);

else

$this->Image(_PS_IMG_DIR_.'tmp/noimage.jpg', 25);

 

Need help with this code. I have already put the code and it is work. But, how to move the image, move the line, etc. If somebody can post the pdf.php i'm so appreciate it.

Link to comment
Share on other sites

if ($productQuantity)
{
//add photo products on pdf invoice -- start
				$cover = Product::getCover((int)($product['product_id']));
				if(is_array($cover)) $product = array_merge($cover, $product);
				$limage = _PS_IMG_DIR_.‘tmp/product_mini_’.(int)($product[‘product_id’]).’.jpg’;
				$withcover = file_exists($limage) ;
				if ($withcover) {
					$this->SetX($this->GetX()+13);
				}
				$before = $this->GetY();
				$this->MultiCell($w[++$i]-($withcover?8:0), 13, Tools::iconv('utf-8', self::encoding(), $product['product_name']), 'B');
				$lineSize = $this->GetY() - $before;
				$this->SetXY($this->GetX() + $w[0] + ($delivery ? 15 : 0), $this->GetY() - $lineSize);
				$this->Cell($w[++$i], $lineSize, ($product['product_reference'] ? Tools::iconv('utf-8', self::encoding(), $product['product_reference']) : '--'), 'B');
				if (!$delivery)
					$this->Cell($w[++$i], $lineSize,(self::$orderSlip ? '-' : '').self::convertSign(Tools::displayPrice($unit_price, self::$currency, true)), 'B', 0, 'R');
				$this->Cell($w[++$i], $lineSize, $productQuantity, 'B', 0, 'C');
				if (!$delivery)
					$this->Cell($w[++$i], $lineSize,(self::$orderSlip ? '-' : '').self::convertSign(Tools::displayPrice($final_price, self::$currency, true)), 'B', 0, 'R');
				//add photo products on pdf invoice -- start
				if ($withcover){
					$this->Image($limage,10,null,13);					  
				} else{
					$this->Image(_PS_IMG_DIR_.'tmp/noimage.jpg',110,null,13);
				}
				//add photo products on pdf invoice -- end

 

I'm using this code but the problem is the picture always said no image.

 

the file name example in img/tmp/product_mini_4176.jpg

 

But why the invoice can't show the product image. Anyone can help?

 

If i force the code with this :

 

				if ($withcover){
					$this->Image($limage,10,null,13);					
				} else{
					$this->Image($limage,10,null,13);
				}
				//add photo products on pdf invoice -- end

 

FPDF error: Image file has no extension and no type was specified: /home/delovisn/public_html/po/img/4176’jpg’

 

Solved : ‘tmp/product_mini_’ different with 'tmp/product_mini_' (the right one)

 

The problem now there are two type temporary file.

_0.jpg and .jpg

post-277630-0-32380100-1349492641_thumb.jpg

Edited by De Angel Net (see edit history)
Link to comment
Share on other sites

PROBLEM : Two type temporary files in img/tmp/ product_mini_XXXX.jpg and product_mini_XXXX_0.jpg

 

SOLUTION : Using double if function for the .jpg and _0.jpg.

 

if ($productQuantity)
{
//add photo products on pdf invoice -- start
				$cover = Product::getCover((int)($product['product_id']));
				if(is_array($cover)) $product = array_merge($cover, $product);
				$limage = _PS_IMG_DIR_.'tmp/product_mini_'.(int)($product['product_id']).'.jpg';
				$limage0 = _PS_IMG_DIR_.'tmp/product_mini_'.(int)($product['product_id']).'_0.jpg';
				$withcover = file_exists($limage) ;
				$withcover0 = file_exists($limage0) ;
				if ($withcover) {
					$this->SetX($this->GetX()+13);
				}
				$before = $this->GetY();
				$this->MultiCell($w[++$i]-($withcover?8:0), 13, Tools::iconv('utf-8', self::encoding(), $product['product_name']), 'B');
				$lineSize = $this->GetY() - $before;
				$this->SetXY($this->GetX() + $w[0] + ($delivery ? 15 : 0), $this->GetY() - $lineSize);
				$this->Cell($w[++$i], $lineSize, ($product['product_reference'] ? Tools::iconv('utf-8', self::encoding(), $product['product_reference']) : '--'), 'B');
				if (!$delivery)
					$this->Cell($w[++$i], $lineSize,(self::$orderSlip ? '-' : '').self::convertSign(Tools::displayPrice($unit_price, self::$currency, true)), 'B', 0, 'R');
				$this->Cell($w[++$i], $lineSize, $productQuantity, 'B', 0, 'C');
				if (!$delivery)
					$this->Cell($w[++$i], $lineSize,(self::$orderSlip ? '-' : '').self::convertSign(Tools::displayPrice($final_price, self::$currency, true)), 'B', 0, 'R');
				//add photo products on pdf invoice -- start
				if ($withcover){
					$this->Image($limage,110,null,13);
				} if ($withcover0) {						
  $this->Image($limage0,110,null,13);

				  } else{
					$this->Image(_PS_IMG_DIR_.'tmp/noimage.jpg',110,null,13);
				}
				//add photo products on pdf invoice -- end
 $this->Ln(1);
}
  }

 

 

 

Another problem : Need more help for setting the line so the image not above the line.

post-277630-0-91345800-1349499393_thumb.jpg

Edited by De Angel Net (see edit history)
Link to comment
Share on other sites

  • 2 weeks later...
  • 4 months later...
  • 1 month later...

Good day

 

I am new to prestashop and using prestashop 1.5.4

 

I am a little bit lost about the code in adding it to PDF.php if (file_exists(_PS_IMG_DIR_.'tmp/product_mini_'.(int)($product['product_id']).'.jpg'))

$this->Image(_PS_IMG_DIR_.'tmp/product_mini_'.(int)($product['product_id']).'.jpg', 25);

else

$this->Image(_PS_IMG_DIR_.'tmp/noimage.jpg', 25);

 

where do I exactly add this code? and which line?

 

Help is really appreciated. Thank you.

Edited by jahyax (see edit history)
Link to comment
Share on other sites

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

Hello everyone. This is the solution for version 1.5.X. It's very simple. Modify the file : \pdf\invoice.tpl

<!-- PRODUCTS TAB -->

<table style="width: 100%">

<tr>

<td style="width: 85%; text-align: right">

<table style="width: 100%; font-size: 8pt;">

<tr style="line-height:4px;">

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

.

.

.

<!-- PRODUCTS -->

{foreach $order_details as $order_detail}

{cycle values='#FFF,#DDD' assign=bgcolor}

<tr style="line-height:6px;background-color:{$bgcolor};">

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

 

Next, modify the file: \classes\pdf\HTMLTemplateInvoice.php

/**

* Returns the template's HTML content

* @return string HTML content

*/

public function getContent()

{

$prod_dtl_id = $this->order->getProducts();

foreach($prod_dtl_id as &$product)

{

if ($product['image'] != null)

{

$name = 'product_mini_'.(int)$product['product_id'].(isset($product['product_attribute_id']) ? '_'.(int)$product['product_attribute_id'] : '').'.jpg';

// generate image cache, only for back office

$product['image_tag'] = ImageManager::thumbnail(_PS_IMG_DIR_.'p/'.$product['image']->getExistingImgPath().'.jpg', $name, 45, 'jpg');

if (file_exists(_PS_TMP_IMG_DIR_.$name))

$product['image_size'] = getimagesize(_PS_TMP_IMG_DIR_.$name);

else

$product['image_size'] = false;

}

$product['pro_img'] = _PS_TMP_IMG_DIR_.$name;

}

$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((int)$this->order->id_customer);

 

$this->smarty->assign(array(

'order' => $this->order,

'order_details' => $this->order_invoice->getProducts(),

'cart_rules' => $this->order->getCartRules($this->order_invoice->id),

'delivery_address' => $formatted_delivery_address,

'invoice_address' => $formatted_invoice_address,

'tax_excluded_display' => Group::getPriceDisplayMethod($customer->id_default_group),

'tax_tab' => $this->getTaxTabContent(),

'customer' => $customer

));

$this->smarty->assign('order_details', $prod_dtl_id);

return $this->smarty->fetch($this->getTemplateByCountry($country->iso_code));

}

 

/**

* Returns the tax tab content

*/

That's all. works 100%. How to configure User Interface, I think the problems will not make.

Link to comment
Share on other sites

  • 1 month later...
  • 3 weeks later...

Hi There,

 

For newer version of PrestaShop (time of writing 05 November 2013) you can get away with editing two files:

 

In pdf/invoice.tpl file follow the instructions above. I added:

<td style="background-color: #4D4D4D; color: #FFF; text-align: right; font-weight: bold; width: 10%"> </td>

on line 100 to expand the header row. You also need to amend the product detail column (line 101) to width: 35% to allow for the new column.

 

To add the main row amend/add the lines 129 & 130 to the product table

 

amend line 129:

<tr{if isset($order_detail.image) && $order_detail.image->id && isset($order_detail.image_size)} height="{$order_detail['image_size'][1] + 7}"{/if} style="line-height:6px;background-color:{$bgcolor};">

add line 130:

<td style="text-align: center; width: 10%" align="center">{if isset($order_detail.image) && $order_detail.image->id}<img  src="{$order_detail.pro_img}"/>{/if}</td>

 

Then in the file /classes/order/OrderLine.php insert the following at line 229 (inside the if() {} block)

$name = 'product_mini_'.(int)$product['product_id'].(isset($product['product_attribute_id']) ? '_'.(int)$product['product_attribute_id'] : '').'.jpg';
            $product['image_tag'] = ImageManager::thumbnail(_PS_IMG_DIR_.'p/'.$product['image']->getExistingImgPath().'.jpg', $name, 100, 'jpg');
                        
            if (file_exists(_PS_TMP_IMG_DIR_.$name))
                $product['image_size'] = getimagesize(_PS_TMP_IMG_DIR_.$name);
            else
                $product['image_size'] = false;       
            
            $product['pro_img'] =  _PS_TMP_IMG_DIR_.$name;  

Hope this helps

Link to comment
Share on other sites

If you want a less pixelated image then you maybe able to get a bigger image by tinkering with these lines:

$name = 'product_mini_'.(int)$product['product_id'].(isset($product['product_attribute_id']) ? '_'.(int)$product['product_attribute_id'] : '').'.jpg';
            $product['image_tag'] = ImageManager::thumbnail(_PS_IMG_DIR_.'p/'.$product['image']->getExistingImgPath().'.jpg', $name, 100, 'jpg');

This is where you are generating the temp thumbnail so you might be able to generate a bigger image. Try looking at the developer documentation or digging around in the ImageManager class.

Link to comment
Share on other sites

  • 6 months later...

Hello,

I used cescodes and I managed to embed the image in the invoice, thank you!

By cons I have not managed to use a better picture ...

someone he succeeded?

I also tried to do the same thing for the delivery but I did not succeed ...
pdf is not calculated ...

Thank you in advance for your answers
cordially
Julian

Link to comment
Share on other sites

  • 1 year later...

Hello,

 

I added this code (prestashop 1.4)

$cover = Product::getCover((int)($product['product_id']));
if(is_array($cover)) $product = array_merge($cover, $product);


$limage = _PS_IMG_DIR_.'p/'.(int)($product['product_id']).'-'.(int)($product['id_image']).'-medium.jpg';

if (file_exists(_PS_IMG_DIR_.'p/'.(int)($product['product_id']).'-'.(int)($product['id_image']).'-medium.jpg'))

$this->Image(_PS_IMG_DIR_.'p/'.(int)($product['product_id']).'-'.(int)($product['id_image']).'-medium.jpg', 110,null,20);
else
$this->Image(_PS_IMG_DIR_.'tmp/product_mini_'.(int)($product['product_id']).'.jpg', 110,null,20);

And it works, but only for products which don't have severals attributes... :(

 

I just would like the main image cover appear, not specificly another... but no one...

 

Any ideas ?

Link to comment
Share on other sites

  • 2 years later...

Hello,

I would like to know if it's possible to add image on the delivery form (HTMLTemplateDeliverySlip.php).

I'm on 1.6.0.11 prestashop version.

I already add a column for the image, but I don't know how can I recover the picture of the product.

This is my function getContent() in HTMLTemplateDeliverySlip.php

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)$this->order->id_address_invoice);
			$formatted_invoice_address = AddressFormat::generateAddress($invoice_address, array(), '<br />', ' ');
		}
		
		$carrier = new Carrier($this->order->id_carrier);
		$carrier->name = ($carrier->name == '0' ? Configuration::get('PS_SHOP_NAME') : $carrier->name);
		$this->smarty->assign(array(
			'order' => $this->order,
			'order_details' => $this->order_invoice->getProducts(),
			'delivery_address' => $formatted_delivery_address,
			'invoice_address' => $formatted_invoice_address,
			'order_invoice' => $this->order_invoice,
			'carrier' => $carrier
		));

		return $this->smarty->fetch($this->getTemplate('delivery-slip'));
	}

This my product tab in delivery-slip.tpl

<tr style="line-height:6px;">
					<td style="background-color: #4D4D4D; color: #FFF; text-align: center; font-weight: bold; width: 10%">{l s='IMAGE' pdf='true'}</td>
					<td style="text-align: left; background-color: #4D4D4D; color: #FFF; padding-left: 10px; font-weight: bold; width: 55%">{l s='ITEMS TO BE DELIVERED' pdf='true'}</td>
					<td style="background-color: #4D4D4D; color: #FFF; text-align: left; font-weight: bold; width: 20%">{l s='REFERENCE' pdf='true'}</td>
					<td style="background-color: #4D4D4D; color: #FFF; text-align: center; font-weight: bold; width: 15%">{l s='QTY' pdf='true'}</td>
				</tr>
				{foreach $order_details as $order_detail}
				{cycle values='#FFF,#DDD' assign=bgcolor}
				<tr style="line-height:6px;background-color:{$bgcolor};">
					<td style="text-align: center; width: 10%">{html_image file='https://www.monsite.fr/191637-thickbox_default/productref-10000419.jpg' height="40px" width="40px"}</td>
					<td style="text-align: left; width: 55%;">{$order_detail.product_name}</td>
					<td style="text-align: left; width: 20%;">
						{if empty($order_detail.product_reference)}
							---
						{else}
							{$order_detail.product_reference}
						{/if}
					</td>
					<td style="text-align: center; width: 15%;">{$order_detail.product_quantity}</td>
				</tr>

With this code I can add directly the image I want but I want it automatic.

Can you help me please ?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...