<?php
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
  die;
}
/**
 *  function for generate pdf for single product. 
 *
 * @since    2.0.0
 */
 function rtwwpcp_get_all_setting_opt(){
 	$rtwwpcp_basic_stng = get_option('rtwwpcp_basic_setting_opt');
	if( !$rtwwpcp_basic_stng )
	{
		$rtwwpcp_basic_stng = array();
	}
	$rtwwpcp_css_stng = get_option('rtwwpcp_css_setting_opt');
	if( !$rtwwpcp_css_stng )
	{
		$rtwwpcp_css_stng = array();
	}
	$rtwwpcp_header_stng = get_option('rtwwpcp_header_setting_opt');
	if( !$rtwwpcp_header_stng )
	{
		$rtwwpcp_header_stng = array();
	}
	$rtwwpcp_footer_stng = get_option('rtwwpcp_footer_setting_opt');
	if( !$rtwwpcp_footer_stng )
	{
		$rtwwpcp_footer_stng = array();
	}
	$rtwwpcp_watermark_stng = get_option('rtwwpcp_watermark_setting_opt');
	if( !$rtwwpcp_watermark_stng )
	{
		$rtwwpcp_watermark_stng = array();
	}
	$rtwwpcp_format_stng = get_option('rtwwpcp_format_setting_opt');
	if( !$rtwwpcp_format_stng )
	{
		$rtwwpcp_format_stng = array();
	}
	$rtwwpcp_stng = array_merge($rtwwpcp_basic_stng,$rtwwpcp_css_stng,$rtwwpcp_header_stng,$rtwwpcp_footer_stng,$rtwwpcp_watermark_stng,$rtwwpcp_format_stng);
	return $rtwwpcp_stng;
 }
/**
 *  function for generate pdf for single product. 
 *
 * @since    2.0.0
 */
function rtwwpcp_creat_sngl_prdct_pdf( $rtwwpcp_product_id )
{
	$rtwwpcp_all_opt = rtwwpcp_get_all_setting_opt();
	$rtwwpcp_product_obj = wc_get_product($rtwwpcp_product_id);
	$args = array(
	    'post_type' => 'product',
	    'posts_per_page' => -1
	);
	$rtwwpcp_format_stng = get_option('rtwwpcp_format_setting_opt');
	$rtwwpcp_get_setting = get_option('rtwwpcp_basic_setting_opt');
	if(isset($rtwwpcp_all_opt['pdf_format']) && $rtwwpcp_all_opt['pdf_format'] != ''){
		if ($rtwwpcp_all_opt['pdf_format'] == 1 ) {
		$rtwwpcp_pdf_format = $rtwwpcp_all_opt['pdf_format_1'];
		}else if($rtwwpcp_all_opt['pdf_format'] == 2){
			$rtwwpcp_pdf_format = $rtwwpcp_all_opt['pdf_format_2'];
		}else{
			$rtwwpcp_pdf_format = $rtwwpcp_all_opt['pdf_format_3'];
		}
	}else{
		$rtwwpcp_pdf_format = get_option( 'default_catalog_format', array());
	}
	$rtwwpcp_file      = RTWWPCP_PDF_DIR.$rtwwpcp_product_id.'.pdf';
	$rtwwcfp_file_name = $rtwwpcp_product_id.'.pdf';
	if ( isset( $rtwwpcp_all_opt ['pdf_name'] ) && $rtwwpcp_all_opt ['pdf_name'] == 'rtwwpcp_post_name' ) {
		$rtwwpcp_product   = new WC_Product($rtwwpcp_product_id);
		$rtwwpcp_file      = RTWWPCP_PDF_DIR.$rtwwpcp_product->get_name().'.pdf';
		$rtwwcfp_file_name = $rtwwpcp_product->get_name().'.pdf';
	}
	if ( file_exists($rtwwpcp_file) ) {
		header('Content-Type: application/octet-stream');
		header("Content-Transfer-Encoding: Binary"); 
		header("Content-disposition: attachment; filename=\"".$rtwwcfp_file_name."\""); 
		readfile( $rtwwpcp_file );
		exit;
	}
	if (! class_exists ( 'simple_html_dom_node' )) 
	{
		require_once (RTWWPCP_DIR .'/includes/simplehtmldom/simple_html_dom.php');
	}
	$rtwwpcp_attr_data = array();
	$rtwwpcp_color = array();
	$rtwwpcp_relate_img = array();
	$rtwwpcp_atchmnt_id = array();
	$rtwwpcp_sngl_product_format = '';
	$rtwwpcp_sngl_product_format = $rtwwpcp_pdf_format;
	$rtwwpcp_data = array();
	$rtwwpcp_data['product_id']  = $rtwwpcp_product_obj->get_id();
	$attchmnt_id = $rtwwpcp_product_obj->get_gallery_image_ids();
	foreach ($attchmnt_id as $key => $value) {
		array_push($rtwwpcp_atchmnt_id, wp_get_attachment_url( $value ));
	}
	$rtwwpcp_data['product_name']  = $rtwwpcp_product_obj->get_name();
	$attr = $rtwwpcp_product_obj->get_attributes();
	if (!empty($attr)) {
		$product_attribute_slugs = array_keys($attr);
		foreach ($attr as $key => $value) {
			if ( strpos($value->get_name(), 'pa_') !== false ) {
				$strng = str_replace('pa_', '', $value->get_name());
			}
			else{
				$strng = $value->get_name();
			}
            $rtwwpcp_attr_data[$strng] = $value->get_options($value->get_name());
		} 
	}
	$rtwwpcp_data['related_products'] = wc_get_related_products( $rtwwpcp_product_obj->get_id(), 4 );
	$cnt = 1;
	foreach ($rtwwpcp_data['related_products'] as $relat_key => $relat_val) {
		$rtwwpcp_relate_img[(wc_get_product( $relat_val ))->get_name()] = wp_get_attachment_image_src( get_post_thumbnail_id( $relat_val ), 'single-post-thumbnail' );
		$rtwwpcp_data['add_to_cart_'.$cnt] =  add_query_arg('add-to-cart', $relat_val, wc_get_cart_url()) ;
		$cnt++;
	}
	$count = 1;
	foreach ($rtwwpcp_relate_img as $relat_k => $relate_v) {
		$rtwwpcp_data['relate_img_'.$count]  = $relate_v[0];
		$rtwwpcp_data['relate_prod_'.$count]  = $relat_k;
		$count++;
	}
	$image = wp_get_attachment_image_src( get_post_thumbnail_id( $rtwwpcp_product_obj->get_id() ), 'single-post-thumbnail' );
	$rtwwpcp_data['add_to_cart']  = add_query_arg('add-to-cart', $rtwwpcp_product_obj->get_id(), wc_get_cart_url()) ;
	$rtwwpcp_data['thumb_img_1']  = isset($image[0])?$image[0]:'';
	$rtwwpcp_data['thumb_img_2']  = isset($rtwwpcp_atchmnt_id[0])?$rtwwpcp_atchmnt_id[0]:'';
	$rtwwpcp_data['thumb_img_3']  = isset($rtwwpcp_atchmnt_id[1])?$rtwwpcp_atchmnt_id[1]:'';
	$rtwwpcp_data['thumb_img_4']  = isset($rtwwpcp_atchmnt_id[2])?$rtwwpcp_atchmnt_id[2]:'';
	$rtwwpcp_data['product_slug']  = $rtwwpcp_product_obj->get_slug();
	$rtwwpcp_data['product_type']  = $rtwwpcp_product_obj->get_type();
	if ( $rtwwpcp_pdf_format == 2 ) 
	{
		if ( $rtwwpcp_product_obj->is_on_sale() ) 
		{
			if( $rtwwpcp_product_obj->has_child() ) {
				$available_variations = $rtwwpcp_product_obj->get_available_variations();
				$percent_off = ( ( 100 * ( $available_variations[0]['display_regular_price'] - $available_variations[0]['display_price'] ) ) / $available_variations[0]['display_regular_price'] );
				$percent_off = wc_format_decimal( $percent_off, 2 );
				$rtwwpcp_data['product_price'] = '<td style="font-size:16px;padding-top: 10px;font-weight: bold;color: #212121;">'.wc_price(floor($available_variations[0]['display_price'])).' <del style="font-weight: normal;"> '.wc_price(floor($available_variations[0]['display_regular_price'])).'</del> <span style="color:#FF905A;">( '.$percent_off.'% OFF)</span></td>';
			}
			else {
				$product_sale_price  = wc_price( $rtwwpcp_product_obj->get_sale_price() );
				$product_regular_price  = wc_price( $rtwwpcp_product_obj->get_regular_price() );
				$percent_off = ( ( 100 * ( $rtwwpcp_product_obj->get_regular_price() - $rtwwpcp_product_obj->get_sale_price() ) ) / $rtwwpcp_product_obj->get_regular_price() );
				$percent_off = wc_format_decimal( $percent_off, 2 );
				$rtwwpcp_data['product_price'] = '<td style="font-size:16px;padding-top: 10px;font-weight: bold;color: #212121;">'.$product_sale_price.' <del style="font-weight: normal;"> '.$product_regular_price.'</del> <span style="color:#FF905A;">( '.$percent_off.'% OFF)</span></td>';
			}
		}
		else
		{
			$rtwwpcp_data['product_price']  = wc_price( $rtwwpcp_product_obj->get_price() );
		}
	}
	else
	{
		$rtwwpcp_data['product_price']  = wc_price( $rtwwpcp_product_obj->get_price() );
	}
	$rtwwpcp_data['product_sku']  = $rtwwpcp_product_obj->get_sku();
	$rtwwpcp_data['permalinks']  = get_permalink( $rtwwpcp_product_obj->get_id() );
	$rtwwpcp_cat = $rtwwpcp_product_obj->get_category_ids();
	foreach ($rtwwpcp_cat as $cat_key => $cat_val) {
		$rtwwpcp_data['product_category'][]  = get_term_by( 'id', $cat_val, 'product_cat' )->name;
	}
    $rtwwpcp_data['status']  = $rtwwpcp_product_obj->get_status();
	$rtwwpcp_data['product_desc']  = $rtwwpcp_product_obj->get_description();
	$rtwwpcp_data['product_short_desc']  = $rtwwpcp_product_obj->get_short_description();
    $args = array ('post_type' => 'product', 'post_id' => $rtwwpcp_product_obj->get_id() );
	$comments = get_comments( $args );
    $rtwwpcp_data['product_img'] = $image[0];
    $rtwwpcp_data['product_rating'] = wc_get_rating_html( $rtwwpcp_product_obj->get_review_count() );
	if( $rtwwpcp_data['product_rating'] == '' )
	{
		$rtwwpcp_data['product_rating'] = 'No Rating Is Available';
	}
	$rtwwpcp_data['product_review'] = array();
	if( !empty( $comments ) && is_array( $comments ) )
	{
		$count = 0;
		foreach ($comments as $key => $value) 
		{
		 	$rtwwpcp_data['product_review'][$count] = $value->comment_content;
		 	$count = ++$count;
		}
	}
	$rtwwpcp_data['avrg_rating'] = $rtwwpcp_product_obj->get_average_rating();
	if ( $rtwwpcp_sngl_product_format != '' ) 
	{
		foreach ($rtwwpcp_data as $key => $value) 
		{	
			if ( strpos($rtwwpcp_sngl_product_format, $key)!== false ) 
			{
				if ( $key == 'product_review' ) 
				{
					$strng = '';
					if( is_array($rtwwpcp_data['product_review']) && !empty($rtwwpcp_data['product_review']) )
					{
						$cont = 1;
						foreach ($rtwwpcp_data['product_review'] as $k => $val) 
						{
							$strng .= '<p>'.$cont.' : '.$val.'</p>';
							$cont = ++$cont;
						}
					}
					else
					{
						$strng = 'No Review Available';
					}
				    $rtwwpcp_sngl_product_format = str_replace('['.$key.']', $strng, $rtwwpcp_sngl_product_format);
				}
				if ( $key == 'product_category' ) 
				{
					$rtwwpcp_category_name = '';
					if ( is_array($rtwwpcp_data['product_category']) && !empty($rtwwpcp_data['product_category'] )) 
					{
						if ( $rtwwpcp_pdf_format == 1 ) 
						{
							$rtwwpcp_sngl_product_format = htmlspecialchars_decode ( htmlentities ( $rtwwpcp_sngl_product_format, ENT_NOQUOTES, 'UTF-8', false ), ENT_NOQUOTES );
							$rtwwpcp_sngl_pro_cat_str = '';
							$rtwwpcp_dom = new simple_html_dom ();
							$rtwwpcp_dom->load ( $rtwwpcp_sngl_product_format );
							foreach ($rtwwpcp_dom->find('#rtwwpcp_cat_list') as $val) 
							{
								$rtwwpcp_sngl_pro_cat_str = $val->outertext;
							}
							$rtwwpcp_category_name .= '<tr><td><div style="width: 100%;"><table><tr>';
							foreach ($rtwwpcp_data['product_category'] as $catgry_k => $catgry_val) 
							{
								if ( $catgry_k > 0 ) 
								{
									$rtwwpcp_category_name .= '<td style="width: 4px;"></td>';
								}
								$rtwwpcp_category_name .= '<td style="width: 90px; text-align: center; padding: 4px; background-color: #009688; border-radius: 2px; color: white;font-size: 12px;">'.$catgry_val.'</td>';
							}
							$rtwwpcp_category_name .= '</tr></table></div></td></tr>';
							$rtwwpcp_dom = new simple_html_dom ();
							$rtwwpcp_dom->load ( $rtwwpcp_sngl_product_format );
							foreach ($rtwwpcp_dom->find('#rtwwpcp_cat_list') as $val) 
							{
								$val->outertext = $rtwwpcp_category_name;
							}
							$rtwwpcp_sngl_product_format = $rtwwpcp_dom->save();
						}
						else
						{
							foreach ($rtwwpcp_data['product_category'] as $cat_k => $cat_value) 
							{
								if ($cat_key == 0) 
								{
									$rtwwpcp_category_name .= '<div style="float:left; width: 25%; text-align: center; padding: 4px; background-color: #009688; border-radius: 2px; color: white;font-size: 12px;">'.$cat_value.'</div>';
								}
								else
								{
									$rtwwpcp_category_name .= '<div style="float:left; width: 25%; text-align: center; padding: 4px; background-color: #009688; border-radius: 2px; color: white;margin-left: 5px;font-size: 12px;">'.$cat_value.'</div>';
								}
							}
							$rtwwpcp_sngl_product_format = str_replace('['.$key.']', $rtwwpcp_category_name, $rtwwpcp_sngl_product_format);
						}
					}
				}
				else
				{
					$rtwwpcp_sngl_product_format = str_replace('['.$key.']', $value, $rtwwpcp_sngl_product_format);
				}
			}
			else
			{
				$rtwwpcp_sngl_product_format = str_replace($key, ' ', $rtwwpcp_sngl_product_format);
			}
		}
		if ( strpos($rtwwpcp_sngl_product_format, '[variation]')!== false ) 
		{
			$rtwwpcp_sngl_product_format = htmlspecialchars_decode ( htmlentities ( $rtwwpcp_sngl_product_format, ENT_NOQUOTES, 'UTF-8', false ), ENT_NOQUOTES );
			$rtwwpcp_string2 = '';
			$rtwwpcp_dom = new simple_html_dom ();
			$rtwwpcp_dom->load ( $rtwwpcp_sngl_product_format );
			$rtwwpcp_prod_tr = '';
			foreach ($rtwwpcp_dom->find('#rtwwpcp_variation_list') as $val) 
			{
				$rtwwpcp_prod_tr = $val->outertext;
			}
			$attr_final_text = '';
			foreach ($rtwwpcp_attr_data as $key_attr => $value_attr) 
			{ 
				if ( $rtwwpcp_pdf_format == 1 ) 
				{
					$attr_final_text .= '<tr style="font-size: 16px; font-weight: bold; margin-top: 25px; color: #000000;">';
					$attr_final_text .= '<td style="font-size: 16px; font-weight: bold; margin-top: 25px; color: #000000;">'.ucfirst($key_attr);
					$attr_final_text .= '</td>';
					$attr_final_text .= '</tr>';
					if ( is_array($value_attr) ) 
					{
						$attr_string  = '';
						$attr_string .= '<tr>';
						$attr_string .= '<td style="padding-top: 10px;">';
						$attr_string .= '<table style="300px;" cellspacing="0" cellpadding="0">';
						$attr_string .= '<tr>';
						foreach ($value_attr as $k_attr => $val_attr) 
						{
							if ( $key_attr == 'color' ) 
							{
								$attr_string .= '<td style="padding-right: 15px;"><table style="border-spacing: 0;" cellspacing="0" cellpadding="0"><tr><td style="width: 40px;border: 2px solid #ddd; height: 40px;text-align: center; font-size: 14px; background-color: '.get_term( $val_attr)->slug.';"></td></tr></table></td>'; 
							}
							else if($key_attr == 'Size')
							{
								$val = substr($val_attr, 0,1);
								$attr_string .= '<td style="padding-right: 15px;"><table style="border-spacing: 0;" cellspacing="0" cellpadding="0"><tr><td style="width: 40px;border: 2px solid #2874f0; height: 40px;text-align: center; font-size: 14px; background-color: #efefefe;">'.$val.'</td></tr></table></td>'; 
							}
							else
							{
								$attr_string .= '<td style="padding-right: 15px;"><table style="border-spacing: 0;" cellspacing="0" cellpadding="0"><tr><td style="width: 40px;border: 2px solid #2874f0; height: 40px;text-align: center; font-size: 14px; background-color: #efefefe;">'.get_term( $val_attr)->name.'</td></tr></table></td>'; 
							}
						}
						$attr_string .= '</tr>';
						$attr_string .= '</table>';
						$attr_string .= '</td>';
						$attr_string .= '</tr>';
					}
					$attr_final_text .= $attr_string;
				}
				else
				{
					$attr_final_text .= '<div>';
					$attr_final_text .= '<div style="font-size: 16px;padding-top: 15px; color: #878787;">'.ucfirst($key_attr).'</div>';
					$attr_final_text .= '</div>';
					if (  is_array($value_attr) ) 
					{
						$attr_string = '';
						$attr_string .= '<div style="width: 100%;">';
						foreach ($value_attr as $k_attr => $val_attr) 
						{
							if ( $key_attr == 'color' ) 
							{
								if($k_attr == 0)
								{
									$attr_string .= '<div style="float: left;width:30px; height: 30px;text-align: center; font-size: 14px;border: 1px solid #ddd;background-color: '.get_term( $val_attr)->slug.';">
										</div>';
								}
								else
								{
									$attr_string .= '<div style="float: left; margin-left: 10px;width:30px; height: 30px;text-align: center; font-size: 14px;border: 1px solid #ddd;background-color: '.get_term( $val_attr)->slug.';">
										</div>';
								}
							}
							else if ( $key_attr == 'Size' ) 
							{
								$val = substr($val_attr, 0,1);
								$attr_string .= '<div style="float: left; padding-right: 10px;width:55px;"><div style="border: 2px solid #2874f0; border-style: dotted;width: 45px; height: 27px;text-align: center; font-size: 14px;line-height: 27px;">'.$val.'</div></div>';
							}
							else
							{
								$attr_string .= '<div style="float: left; padding-right: 10px;width:55px;"><div style="border: 2px solid #2874f0;border-style: dotted;width: 50px; height: 27px;text-align: center; font-size: 14px;line-height: 27px;">'.get_term( $val_attr)->name.'</div></div>';
							}
						}
						$attr_string .= '</div>';	
					}
					$attr_final_text .= $attr_string;
				}
			} 
		}
		$rtwwpcp_dom = new simple_html_dom ();
		$rtwwpcp_dom->load ( $rtwwpcp_sngl_product_format );
		foreach ($rtwwpcp_dom->find('#rtwwpcp_variation_list') as $val) 
		{
			$val->outertext = $attr_final_text;
		}
		$rtwwpcp_sngl_product_format = $rtwwpcp_dom->save();
		
		if ( empty($rtwwpcp_relate_img) ) 
		{
			$rtwwpcp_sngl_product_format = htmlspecialchars_decode ( htmlentities ( $rtwwpcp_sngl_product_format, ENT_NOQUOTES, 'UTF-8', false ), ENT_NOQUOTES );
			$rtwwpcp_string2 = '';
			$rtwwpcp_string = '';
			$rtwwpcp_dom = new simple_html_dom ();
			$rtwwpcp_dom->load ( $rtwwpcp_sngl_product_format );
			$rtwwpcp_relat_prod = '';
			foreach ($rtwwpcp_dom->find('#rtwwpcp_relat_prod tbody') as $val) 
			{
				$rtwwpcp_string2 = $val->outertext;
			}
			foreach ($rtwwpcp_dom->find('#rtwwpcp_similar_prod td') as $val) 
			{
				$rtwwpcp_string = $val->outertext;
			}
			$rtwwpcp_dom = new simple_html_dom ();
			$rtwwpcp_dom->load ( $rtwwpcp_sngl_product_format );
			foreach ($rtwwpcp_dom->find('#rtwwpcp_relat_prod tbody') as $val) 
			{
				$val->outertext ='';
			}
			foreach ($rtwwpcp_dom->find('#rtwwpcp_similar_prod td') as $val) 
			{
				$val->outertext ='';
			}
			$rtwwpcp_sngl_product_format = $rtwwpcp_dom->save();
		}
		$rtwwpcp_sngl_product_format = htmlspecialchars_decode ( htmlentities ( $rtwwpcp_sngl_product_format, ENT_NOQUOTES, 'UTF-8', false ), ENT_NOQUOTES );
		$img_string2 = '';
		$img_string = '';
		$rtwwpcp_dom = new simple_html_dom ();
		$rtwwpcp_dom->load ( $rtwwpcp_sngl_product_format );
		$relate_img = '';
		foreach ($rtwwpcp_dom->find('#rtwwcpcp_img img') as $val) 
		{
			$img_string2 = $val->getAttribute('src');
			if ( $img_string2 == '' ) 
			{
				$val->outertext ='';
			}
		}
		$rtwwpcp_sngl_product_format = $rtwwpcp_dom->save();
		$rtwwpcp_sngl_product_format = htmlspecialchars_decode ( htmlentities ( $rtwwpcp_sngl_product_format, ENT_NOQUOTES, 'UTF-8', false ), ENT_NOQUOTES );
		$img_str = '';
		$str = '';
		$rtwwpcp_dom = new simple_html_dom ();
		$rtwwpcp_dom->load ( $rtwwpcp_sngl_product_format );
		foreach ($rtwwpcp_dom->find('#rtwwpcp_relat_prod tbody tr img') as $val) 
		{
			$img_str = $val->getAttribute('src');
			if ( strpos($img_str, 'img') || $img_str == '' ) 
			{
				$val->outertext ='';
			}
		}
		foreach ($rtwwpcp_dom->find('#rtwwpcp_relat_prod tr[id=rtwwpcp_relat_prod_tr]') as $val) 
		{
			$str = $val->outertext;
			if ( strpos($str, 'prod') ) 
			{
				$val->outertext ='';
			}
		}
		$rtwwpcp_sngl_product_format = $rtwwpcp_dom->save();
		if ( strpos($rtwwpcp_sngl_product_format, '[add_to_cart') ) 
		{
			$rtwwpcp_sngl_product_format = str_replace('<td id="rtwwpcp_add_to_cart" style="font-size: 16px; font-weight: normal; background-color: #d93c50; height: 30px;"><a style="color: #ffffff; text-decoration: none;" href="[add_to_cart_2]">Buy Now</a></td>', '', $rtwwpcp_sngl_product_format);
			$rtwwpcp_sngl_product_format = str_replace('<td id="rtwwpcp_add_to_cart" style="font-size: 16px; font-weight: normal; background-color: #d93c50; height: 30px;"><a style="color: #ffffff; text-decoration: none;" href="[add_to_cart_3]">Buy Now</a></td>', '', $rtwwpcp_sngl_product_format);
			$rtwwpcp_sngl_product_format = str_replace('<td id="rtwwpcp_add_to_cart" style="font-size: 16px; font-weight: normal; background-color: #d93c50; height: 30px;"><a style="color: #ffffff; text-decoration: none;" href="[add_to_cart_4]">Buy Now</a></td>', '', $rtwwpcp_sngl_product_format);
		}
		if ( isset($rtwwpcp_get_setting['hide_add_to_cart_button']) && $rtwwpcp_get_setting['hide_add_to_cart_button'] == '1') 
		{
			$rtwwpcp_sngl_product_format = htmlspecialchars_decode ( htmlentities ( $rtwwpcp_sngl_product_format, ENT_NOQUOTES, 'UTF-8', false ), ENT_NOQUOTES );
			$rtwwpcp_dom = new simple_html_dom ();
			$rtwwpcp_dom->load ( $rtwwpcp_sngl_product_format );
			foreach( $rtwwpcp_dom->find('#add_to_cart_btn') as $val )
			{
				$val->outertext ='';
			}
			$rtwwpcp_sngl_product_format = $rtwwpcp_dom->save();
		}
	}
	$pdf_data = rtwwpcp_convert_in_pdf( $rtwwpcp_sngl_product_format,$rtwwpcp_product_obj->get_id());
	$rtwwcfp_file_name 	= $rtwwpcp_product_obj->get_id().'.pdf';
	if ( isset( $rtwwpcp_all_opt['pdf_name'] ) && $rtwwpcp_all_opt['pdf_name'] == 'rtwwpcp_post_name' ) 
	{
		$rtwwcfp_file_name 	= $rtwwpcp_product_obj->get_name().'.pdf';
	}
	$rtwwcfp_file_url 	= $pdf_data['dir_path'];
	header('Content-Type: application/octet-stream');
	header("Content-Transfer-Encoding: Binary"); 
	header("Content-disposition: attachment; filename=\"".$rtwwcfp_file_name."\""); 
	readfile( $rtwwcfp_file_url );
	exit;		
}
/**
* function for convert html into PDF.
*
* @since    1.0.0
*/
function rtwwpcp_convert_in_pdf( $rtwwpcp_html , $rtwwpcp_id )
{
	$rtwwpcp_all_opt = rtwwpcp_get_all_setting_opt();
	error_reporting(0);
	ini_set('display_errors', 0);
	if ( $rtwwpcp_id == 'Category-catalog' ) 
	{
		$rtwwpcp_file_path = RTWWPCP_PDF_DIR.'Category-catalog.pdf';
		$rtwwpcp_file_name = 'Category-catalog.pdf';
	}
	else if ( $rtwwpcp_id == 'Catalog' ) 
	{
		$rtwwpcp_file_path = RTWWPCP_PDF_DIR.'Catalog.pdf';
		$rtwwpcp_file_name = 'Catalog.pdf';
	}
	else
	{
		$rtwwpcp_file_path = RTWWPCP_PDF_DIR.$rtwwpcp_id.'.pdf';
		$rtwwpcp_file_name = $rtwwpcp_id.'.pdf';
		if ( isset( $rtwwpcp_all_opt['pdf_name'] ) && $rtwwpcp_all_opt['pdf_name'] == 'rtwwpcp_post_name' ) 
		{
			$rtwwpcp_product = new WC_Product($rtwwpcp_id);
			$rtwwpcp_file_path = RTWWPCP_PDF_DIR.$rtwwpcp_product->get_name().'.pdf';
			$rtwwpcp_file_name = $rtwwpcp_product->get_name().'.pdf';
		}
	}

	$rtwwpcp_pdf_css = '<style>';
	if ( is_array( $rtwwpcp_html ) && !empty( $rtwwpcp_html ) ) 
	{
		foreach ($rtwwpcp_html as $key => $value) 
		{
			if ( isset($rtwwpcp_all_opt['rtw_pdf_css']) && !empty($rtwwpcp_all_opt['rtw_pdf_css']) )
			{
				$rtwwpcp_pdf_css .= $rtwwpcp_all_opt['rtw_pdf_css'].$value;
			}
			$rtwwpcp_pdf_css .= '</style>';
		}
	}
	if( isset( $rtwwpcp_all_opt['pdf_page_size'] ) && $rtwwpcp_all_opt['pdf_page_size'] != 'Select' ) 
	{
		$rtwwpcp_page_size = $rtwwpcp_all_opt['pdf_page_size'];
	} else {
		$rtwwpcp_page_size = serialize(array(210,297));
	}
	if( isset( $rtwwpcp_all_opt['page_orien'] ) ) {
		$rtwwpcp_page_orientation = $rtwwpcp_all_opt['page_orien'];
	} else {
		$rtwwpcp_page_orientation = 'P';
	}
	if( isset( $rtwwpcp_all_opt['body_left_margin'] ) ) {
		$rtwwpcp_lft_marg = $rtwwpcp_all_opt['body_left_margin'];	
	} else {
		$rtwwpcp_lft_marg = 15;
	}
	if( isset( $rtwwpcp_all_opt['body_right_margin'] ) ) {
		$rtwwpcp_rgt_marg = $rtwwpcp_all_opt['body_right_margin'];	
	} else {
		$rtwwpcp_rgt_marg = 15;
	}
	if( isset( $rtwwpcp_all_opt['body_top_margin'] ) ) {
		$rtwwpcp_top_marg = $rtwwpcp_all_opt['body_top_margin'];	
	} else {
		$rtwwpcp_top_marg = 15;
	}
	if( isset( $rtwwpcp_all_opt['header_top_margin'] ) ) {
		$rtwwpcp_hdr_top_marg = $rtwwpcp_all_opt['header_top_margin'];	
	} else {
		$rtwwpcp_hdr_top_marg = 7;
	}
	/*PDF footer top margin*/
	if( isset( $rtwwpcp_all_opt['footer_top_margin'] ) ) {
		$rtwwpcp_foo_top_marg = $rtwwpcp_all_opt['footer_top_margin'];	
	} else {
		$rtwwpcp_foo_top_marg = 15;
	}
	if( isset($rtwwpcp_all_opt['body_font_family']) ) {
		$rtwwpcp_body_font_family = $rtwwpcp_all_opt['body_font_family'];
	} else{
		$rtwwpcp_body_font_family = "dejavusanscondensed";
	}
	if(isset($rtwwpcp_all_opt['body_font_size']) ) {
		$rtwwpcp_body_font_size = $rtwwpcp_all_opt['body_font_size'];
	} else{
		$rtwwpcp_body_font_size = 10;
	}
	include(RTWWPCP_DIR ."includes/mpdf/autoload.php");
	$rtwwpcp_mpdf = new \Mpdf\Mpdf( ['mode' => 'utf-8', 'format' => unserialize( $rtwwpcp_page_size ), 'default_font_size' => $rtwwpcp_body_font_size, 'default_font' => $rtwwpcp_body_font_family, 'margin_left' => $rtwwpcp_lft_marg, 'margin_right' => $rtwwpcp_rgt_marg, 'margin_top' => $rtwwpcp_top_marg, 'margin_bottom' => '20', 'margin_header' => $rtwwpcp_hdr_top_marg, 'margin_footer' => $rtwwpcp_foo_top_marg, 'orientation' => $rtwwpcp_page_orientation ]);
	if( isset( $rtwwpcp_all_opt['header_font_size'] ) ) {
		$rtwwpcp_hdr_font_size = $rtwwpcp_all_opt['header_font_size'];
	} else {
		$rtwwpcp_hdr_font_size = 20;
	}
	if( isset( $rtwwpcp_all_opt['header_font_family'] ) ) {
		$rtwwpcp_hdr_font_family = $rtwwpcp_all_opt['header_font_family'];
	} else{
		$rtwwpcp_hdr_font_family = 'sans-serif';
	}
	$rtwwpcp_mpdf->defaultheaderfontsize = $rtwwpcp_hdr_font_size;
	$rtwwpcp_mpdf->defaultheaderfontstyle = $rtwwpcp_hdr_font_family;
	$rtwwpcp_mpdf->defaultheaderline = 1;
	$rtwwpcp_site_name=get_bloginfo ( 'name' );
	$rtwwpcp_site_desc=get_bloginfo ( 'description' );
	$rtwwpcp_site_url=home_url();
	if( isset( $rtwwpcp_all_opt['footer_font_size'] ) ) {
		$rtwwpcp_foo_font_size = $rtwwpcp_all_opt['footer_font_size'];
	} else {
		$rtwwpcp_foo_font_size = 10;
	}
	if( isset( $rtwwpcp_all_opt['footer_font_family'] ) ) {
		$rtwwpcp_foo_family = $rtwwpcp_all_opt['footer_font_family'];
	} else {
		$rtwwpcp_foo_family = 'sans-serif';
	}
	$rtwwpcp_mpdf->defaultfooterfontsize = $rtwwpcp_foo_font_size;	/* in pts */
	$rtwwpcp_mpdf->defaultfooterfontstyle = $rtwwpcp_foo_family;	/* blank, B, I, or BI */
	$rtwwpcp_mpdf->defaultfooterline = 1; 	/* 1 to include line below header/above footer */
	if( isset($rtwwpcp_all_opt['rtw_footer_html']) ) {
		$rtwwpcp_footer_txt = $rtwwpcp_all_opt['rtw_footer_html'];
	} else {
		$rtwwpcp_footer_txt = '';
	}
	if (isset($rtwwpcp_all_opt['page_no']) && !empty( $rtwwpcp_all_opt['page_no'] )) 
	{
		$rtwwpcp_page_no = '' ;
	}
	else
	{
		$rtwwpcp_page_no = '{PAGENO}/{nbpg}';
	}
	if(!isset($rtwwpcp_all_opt['remove_footer'] ))
	{
		//***  $rtwwpcp_footer_txt this variable is used for HTML ***//
		$rtwwpcp_mpdf->SetHTMLFooter( '<div style="width:100%;margin:0px;padding:0px;"><div style="width: 92%;margin-top:'.esc_attr($rtwwpcp_foo_top_marg).';font-size:'.esc_attr($rtwwpcp_foo_font_size).';font-family:'.esc_attr($rtwwpcp_foo_family).'">'.$rtwwpcp_footer_txt.'</div><div style="width: ;margin:0px;padding:0px;float:;text-align:;">'.$rtwwpcp_page_no.'</div>', 'O' );
		$rtwwpcp_mpdf->SetHTMLFooter( '<div style="width:100%;margin:0px;padding:0px;margin-top:2px;padding-top:10px"><div style="width: 92%;margin-top:'.esc_attr($rtwwpcp_foo_top_marg).';font-size:'.esc_attr($rtwwpcp_foo_font_size).'">'.$rtwwpcp_footer_txt.'</div><div style="width: 8%;margin:0px;padding:0px;float:;text-align:;">'.$rtwwpcp_page_no.'</div>', 'E' );
	}
	if( isset($rtwwpcp_all_opt['enable_text_watermark'] ) )
	{
		if( isset($rtwwpcp_all_opt['watermark_text'] ) && !empty( $rtwwpcp_all_opt['watermark_text'] ) )
		{  
			$rtwwpcp_alpha=0.2;
			if( isset( $rtwwpcp_all_opt['watermark_text_trans'] ) && !empty($rtwwpcp_all_opt['watermark_text_trans'] ) )
			{
				$rtwwpcp_alpha = $rtwwpcp_all_opt['watermark_text_trans'];
			}
			if( isset($rtwwpcp_all_opt['watermark_rotation']) && !empty($rtwwpcp_all_opt['watermark_rotation']))
			{
				$GLOBALS['rotate']= $rtwwpcp_all_opt['watermark_rotation'];
			}
			$rtwwpcp_mpdf->SetWatermarkText( trim($rtwwpcp_all_opt['watermark_text']),$rtwwpcp_alpha );
			$rtwwpcp_mpdf->showWatermarkText = true;
			if( isset( $rtwwpcp_all_opt['watermark_font'] ) && !empty( $rtwwpcp_all_opt['watermark_font'] ) )
			{
				$rtwwpcp_mpdf->watermark_font = $rtwwpcp_all_opt['watermark_font'];
			}
		}	
	}
	if( isset( $rtwwpcp_all_opt['enable_image_watermark'] ) )
	{
		if( isset( $rtwwpcp_all_opt['rtwwpcp_watermark_img_url'] ) && !empty($rtwwpcp_all_opt['rtwwpcp_watermark_img_url']) )
		{
			if( isset( $rtwwpcp_all_opt['watermark_image_trans'] ) && !empty($rtwwpcp_all_opt['watermark_image_trans']) )
			{ 
				$rtwwpcp_alpha_img = $rtwwpcp_all_opt['watermark_image_trans'];
			}
			else
			{
				$rtwwpcp_alpha_img = 0.2;
			}
			$rtwwpcp_watermark_img_dim ='D';
			if( isset($rtwwpcp_all_opt['watermark_img_dim']) && $rtwwpcp_all_opt['watermark_img_dim'] == 'P') 
			{
				$rtwwpcp_watermark_img_dim = 'P';
			}
			if(isset($rtwwpcp_all_opt['watermark_img_dim']) && $rtwwpcp_all_opt['watermark_img_dim']=='F')
			{
				$rtwwpcp_watermark_img_dim = 'F';
			}
			if(isset($rtwwpcp_all_opt['watermark_img_dim']) && $rtwwpcp_all_opt['watermark_img_dim']=='INT')
			{
				$rtwwpcp_watermark_img_dim = $rtwwpcp_all_opt['water_img_dim_int'];
				$rtwwpcp_watermark_img_dim = (int)$rtwwpcp_watermark_img_dim ; 
			}
			if(isset($rtwwpcp_all_opt['watermark_img_dim']) && $rtwwpcp_all_opt['watermark_img_dim']=='array')
			{
				$rtwwpcp_watermark_img_dim = array($rtwwpcp_all_opt['water_img_dim_width'],$rtwwpcp_all_opt['water_img_dim_height']);
			}
			$rtwwpcp_watermark_pos = 'P';
			if(isset($rtwwpcp_all_opt['watermark_img_pos']) && $rtwwpcp_all_opt['watermark_img_pos']=='F')
			{
				$rtwwpcp_watermark_pos='F';	
			}
			if(isset($rtwwpcp_all_opt['watermark_img_pos']) && $rtwwpcp_all_opt['watermark_img_pos']=='arrays')
			{
				$rtwwpcp_watermark_pos=array($rtwwpcp_all_opt['watermark_img_pos_x'],$rtwwpcp_all_opt['watermark_img_pos_y']);
			}
			$rtwwpcp_mpdf->SetWatermarkImage($rtwwpcp_all_opt['rtwwpcp_watermark_img_url'],$rtwwpcp_alpha_img,$rtwwpcp_watermark_img_dim , $rtwwpcp_watermark_pos);
			$rtwwpcp_mpdf->showWatermarkImage = true;
		}	
	}
	if( isset($rtwwpcp_all_opt['rtl']) && $rtwwpcp_all_opt['rtl'] == 1)
	{
		$rtwwpcp_mpdf->SetDirectionality('rtl');
	}
	else
	{
		$rtwwpcp_mpdf->SetDirectionality('ltr');
	}
	if ( !is_dir ( RTWWPCP_PDF_DIR ) ) 
	{
		mkdir ( RTWWPCP_PDF_DIR, 0755, true );
	}
	//$rtwwpcp_mpdf->showImageErrors = true;
	try
	{ 
		$rtwwpcp_mpdf->SetDefaultBodyCSS('background', "url(".$rtwwpcp_all_opt['bck_img_url'].")");
		$rtwwpcp_mpdf->SetDefaultBodyCSS('background-image-resize', 6);
		$rtwwpcp_mpdf->SetDefaultBodyCSS('background-color', $rtwwpcp_all_opt['back_color']);
		if(isset( $rtwwpcp_all_opt['enable_paswrd'] ) && ($rtwwpcp_all_opt['enable_paswrd'] != '' ))
		{
			if( $rtwwpcp_id != 'Catalog' )
			{
				$rtwwpcp_mpdf->SetProtection(array(), $rtwwpcp_all_opt['catalog_pswrd'] , $rtwwpcp_all_opt['catalog_pswrd'] );
			}
		}
		if ( is_array($rtwwpcp_html) ) 
		{
			$rtwwpcp_page_count = count($rtwwpcp_html);
			foreach ($rtwwpcp_html as $key => $value) 
			{
				if($key == 0 && $rtwwpcp_all_opt['remove_header_frst_last'] )
				{
					$rtwwpcp_mpdf->rtwwpcp_custom_hdr_mrg = true;
					$rtwwpcp_mpdf->setAutoTopMargin = false;
					$rtwwpcp_mpdf->SetHTMLHeader('');
				}
				else if ( $key == ($rtwwpcp_page_count - 1) && $rtwwpcp_all_opt['remove_header_frst_last'] ) 
				{
					$rtwwpcp_mpdf->setAutoTopMargin = false;
					$rtwwpcp_mpdf->SetHTMLHeader('');
				}
				else
				{
					if( !isset($rtwwpcp_all_opt['remove_header'] ) )
					{
						$rtwwpcp_mpdf->setAutoTopMargin = 'stretch';
						if( isset( $rtwwpcp_all_opt['rtw_header_html'] ) && !empty( $rtwwpcp_all_opt['rtw_header_html'] ) )
						{
							$rtwwpcp_mpdf->SetHTMLHeader('<div><h2 style="margin-top:'.esc_attr($rtwwpcp_hdr_top_marg).';font-size:'.esc_attr($rtwwpcp_hdr_font_size).'px;font-family:'.esc_attr($rtwwpcp_all_opt['header_font_family']).';">'.$rtwwpcp_all_opt['rtw_header_html'].'</h2></div>', 'O' );
							$rtwwpcp_mpdf->SetHTMLHeader('<div><h2 style="margin-top:'.esc_attr($rtwwpcp_hdr_top_marg).';padding:0px;font-size:'.esc_attr($rtwwpcp_hdr_font_size).'px;font-family:'.esc_attr($rtwwpcp_all_opt['header_font_family']).';">'.$rtwwpcp_all_opt['rtw_header_html'].'</h2></div>', 'E');
						}
						else
						{
							$rtwwpcp_mpdf->SetHTMLHeader('<div style="width:100%;height:70px;border-bottom: 2px solid #000000;"><div style="float:left;"><h2 style="margin:0px;padding:0px;font-size:'.esc_attr($rtwwpcp_hdr_font_size).'px;font-family:'.esc_attr($rtwwpcp_all_opt['header_font_family']).';">'.$rtwwpcp_site_name.'</h2><p style="margin:0px;padding:0px;font-size:'.esc_attr($rtwwpcp_hdr_font_size).'px;font-family:'.esc_attr($rtwwpcp_all_opt['header_font_family']).';">'.esc_html__($rtwwpcp_site_desc, 'rtwwpcp-woocommerce-pdf-flipbook').'</p><p style="margin:0px;padding:0px;font-size:'.esc_attr($rtwwpcp_hdr_font_size).'px;font-family:'.esc_attr($rtwwpcp_all_opt['header_font_family']).';">'.$rtwwpcp_site_url.'</p></div></div>','O');
							$rtwwpcp_mpdf->SetHTMLHeader('<div style="width:100%;height:70px;border-bottom: 2px solid #000000;"><div style="float:left;"><h2 style="margin:0px;padding:0px;font-size:'.esc_attr($rtwwpcp_hdr_font_size).'px;font-family:'.esc_attr($rtwwpcp_all_opt['header_font_family']).';">'.$rtwwpcp_site_name.'</h2><p style="margin:0px;padding:0px;font-size:'.esc_attr($rtwwpcp_hdr_font_size).'px;font-family:'.esc_attr($rtwwpcp_all_opt['header_font_family']).';">'.esc_html__($rtwwpcp_site_desc, 'rtwwpcp-woocommerce-pdf-flipbook').'</p><p style="margin:0px;padding:0px;font-size:'.esc_attr($rtwwpcp_hdr_font_size).'px;font-family:'.esc_attr($rtwwpcp_all_opt['header_font_family']).';">'.$rtwwpcp_site_url.'</p></div></div>','E');
						}
					}
				}
				$rtwwpcp_mpdf->AddPage('P','','','','',0,0,0,0,0,0);
				$value = do_shortcode( $value );
				$rtwwpcp_mpdf->WriteHTML($value);
			}
		}
		else
		{
			if( !isset($rtwwpcp_all_opt['remove_header'] ) )
			{
				$rtwwpcp_mpdf->setAutoTopMargin = 'stretch';
				if( isset( $rtwwpcp_all_opt['rtw_header_html'] ) && !empty( $rtwwpcp_all_opt['rtw_header_html'] ) )
				{
					$rtwwpcp_mpdf->SetHTMLHeader('<div><h2 style="font-size:'.esc_attr($rtwwpcp_hdr_font_size).'px;font-family:'.esc_attr($rtwwpcp_all_opt['header_font_family']).';">'.$rtwwpcp_all_opt['rtw_header_html'].'</h2></div>', 'O' );
					$rtwwpcp_mpdf->SetHTMLHeader('<div><h2 style="padding:0px;font-size:'.esc_attr($rtwwpcp_hdr_font_size).'px;font-family:'.esc_attr($rtwwpcp_all_opt['header_font_family']).';">'.$rtwwpcp_all_opt['rtw_header_html'].'</h2></div>', 'E');
				}
				else
				{
					$rtwwpcp_mpdf->SetHTMLHeader('<div style="width:100%;height:70px;border-bottom: 2px solid #000000;"><div style="float:left;"><h2 style="margin:0px;padding:0px;font-size:'.esc_attr($rtwwpcp_hdr_font_size).'px;font-family:'.esc_attr($rtwwpcp_all_opt['header_font_family']).';">'.$rtwwpcp_site_name.'</h2><p style="margin:0px;padding:0px;font-size:'.esc_attr($rtwwpcp_hdr_font_size).'px;font-family:'.esc_attr($rtwwpcp_all_opt['header_font_family']).';">'.esc_html__($rtwwpcp_site_desc, 'rtwwpcp-woocommerce-pdf-flipbook').'</p><p style="margin:0px;padding:0px;font-size:'.esc_attr($rtwwpcp_hdr_font_size).'px;font-family:'.esc_attr($rtwwpcp_all_opt['header_font_family']).';">'.$rtwwpcp_site_url.'</p></div></div>','O');
					$rtwwpcp_mpdf->SetHTMLHeader('<div style="width:100%;height:70px;border-bottom: 2px solid #000000;"><div style="float:left;"><h2 style="margin:0px;padding:0px;font-size:'.esc_attr($rtwwpcp_hdr_font_size).'px;font-family:'.esc_attr($rtwwpcp_all_opt['header_font_family']).';">'.$rtwwpcp_site_name.'</h2><p style="margin:0px;padding:0px;font-size:'.esc_attr($rtwwpcp_hdr_font_size).'px;font-family:'.esc_attr($rtwwpcp_all_opt['header_font_family']).';">'.esc_html__($rtwwpcp_site_desc, 'rtwwpcp-woocommerce-pdf-flipbook').'</p><p style="margin:0px;padding:0px;font-size:'.esc_attr($rtwwpcp_hdr_font_size).'px;font-family:'.esc_attr($rtwwpcp_all_opt['header_font_family']).';">'.$rtwwpcp_site_url.'</p></div></div>','E');
				}
			}
			$rtwwpcp_mpdf->WriteHTML( $rtwwpcp_html );
		}
		$rtwwpcp_mpdf->Output($rtwwpcp_file_path,'F');	
	}
	catch( Exception $rtwwpcp_excptn)
	{
		print_r($rtwwpcp_excptn);
	}
	$rtwwpcp_Dir_path = RTWWPCP_PDF_DIR.$rtwwpcp_file_name;
	$rtwwpcp_file_url = RTWWPCP_PDF_URL.$rtwwpcp_file_name;
	$rtwwpcp_pdf_1 = array('dir_path' => $rtwwpcp_Dir_path, 'file_url' => $rtwwpcp_file_url);
	return $rtwwpcp_pdf_1;
}