PDF Report - Inserting Blank Lines when group printing

I am doing control break printing. I am grouping the records. The records are printed based on a condition. But the issue I am facing is quite absurd. The report prints blank lines when the condition is not met.
. Code as follows
/------------------ Page 1 -----------------/
//$batchNo = “”;
$x = 5;//sc_pdf_get_x();
$y =0;// sc_pdf_get_y();

		if ([prevBatchNo] == "START"){
			$lines_printed = 0;
			$print_header = 1;
			[prevBatchNo] = "ST";
		} //if ([prevBatchNo] == "START")

		
		if ($lines_printed >=50){
			$print_header = 1;
			$lines_printed = 0;
			sc_pdf_add_page("Portrait");
		} //if ($total_printed_lines >=50){

		if ($print_header ==1){
			//$total_lines = ($header_printed_count *5) + $lines_printed;
			// Following section is to check if adding header exceeds no_of_lines per page which is 45
			// if so then add blank lines
			$totallines_with_new_header = $lines_printed +2;
			if ($totallines_with_new_header >=50){
					sc_pdf_add_page("Portrait");		
					$lines_printed = 0;
				} //if ($totallines_with_new_header >=45) {
			
			// Detail Header
			$y = sc_pdf_get_y();
			sc_pdf_ln(5); 
			$y = sc_pdf_get_y();
			sc_pdf_set_xy(5,$y,true); 
			sc_pdf_write(0,"ROW ID");
			sc_pdf_set_xy(26,$y,true);  
			//sc_pdf_set_xy(37,$y,true); 
			sc_pdf_write(0,"TEST DATA ");
			sc_pdf_set_xy(70,$y,true); 
			sc_pdf_write(0,"LINES COUNT ");
			sc_pdf_set_xy(100,$y,true);
			sc_pdf_write(0,"ROW ID ");
			sc_pdf_set_xy(140,$y,true);
			sc_pdf_write(0," Even / odd ");
			sc_pdf_ln(5);
			$print_header=0;	
		}  //if ($print_header ==1){				
		// DETAIL PRINT SECTION
	$divResult = {row_id};

//following is to simulate conditional printing
if ($divResult % 2 == 0) //comment / uncomment this to see the issue
{
$y = sc_pdf_get_y();
sc_pdf_set_xy(5,$y,true); //position on your PDF
sc_pdf_write(0,trim({row_id}));
sc_pdf_set_xy(26,$y,true);
sc_pdf_write(0,trim({TestData}));
sc_pdf_set_xy(70,$y,true);
sc_pdf_write(0,$lines_printed);
sc_pdf_set_xy(100,$y,true);
sc_pdf_write(0,{row_id});
sc_pdf_set_xy(140,$y,true);
sc_pdf_write(0,$divResult % 2);
} //if ($divResult % 2 ==0){
$lines_printed = $lines_printed+1;

uploaded : reports , excel file with test data

allrecordsnocondition.pdf (12.9 KB) conditionalprint.pdf (10.8 KB) testreport.csv (3.1 KB)