Image quality on pdf output

Topics: Developer Forum, User Forum
Nov 4, 2010 at 12:45 AM

I am having image quality issues with my pdf output!  

 

Here is the code I am using:  (image section is at line 99! 

 

<?php
ini_set('display_errors',1);
define('XML_FOLDER_PATH', 'D:/wamp/www/phpexcel/xml_folder/'); //have slasher at end
define('PDF_FOLDER_PATH', 'D:/wamp/www/phpexcel/pdf_folder/'); //have slasher at end
define('TOP_IMAGE','newtop.jpg');
define('BOTTOM_IMAGE','new bottom.jpg');

//change these params fit with your config
//or you can replace with include config.php of your
$connection = mysql_connect('localhost', 'root', '') or die('Can not connect mysql server');
mysql_select_db('ordernew') or die('Can not select database');

//parsing XML
$dir = opendir(XML_FOLDER_PATH);
if(!$dir)
{
    die('Can not open '.XML_FOLDER_PATH.'. Please check it');
}

while($filename = readdir($dir))
{
    if(is_file(XML_FOLDER_PATH.$filename))
    {
        if(processFile(XML_FOLDER_PATH.$filename))
			echo '<b>Process '.$filename.' success.</b><br/>';
		else
			echo 'Can not process '.$filename.'.<br/>';
    }
}
closedir($dir);

function processFile($filenameFullPath)
{
    $xmlDoc     = new DOMDocument();
    $xmlLoad    = $xmlDoc->load($filenameFullPath);
    if(!$xmlLoad) return false;
    
    $patient_id     = $xmlDoc->getElementsByTagName('patient_id')->item(0)->nodeValue;
    $patient_last   = $xmlDoc->getElementsByTagName('patient_last')->item(0)->nodeValue;
    $patient_first  = $xmlDoc->getElementsByTagName('patient_first')->item(0)->nodeValue;
    $patient_dob    = $xmlDoc->getElementsByTagName('patient_dob')->item(0)->nodeValue;
    $patient_gender = $xmlDoc->getElementsByTagName('patient_gender')->item(0)->nodeValue;

    $xrex_case_number   = $xmlDoc->getElementsByTagName('xrex_case_number')->item(0)->nodeValue;
    $accession_number   = $xmlDoc->getElementsByTagName('accession_number')->item(0)->nodeValue;
    $date_of_exam       = $xmlDoc->getElementsByTagName('date_of_exam')->item(0)->nodeValue;
    $exam_type          = $xmlDoc->getElementsByTagName('exam_type')->item(0)->nodeValue;
    $exam_reason        = $xmlDoc->getElementsByTagName('exam_reason')->item(0)->nodeValue;
    $physician          = $xmlDoc->getElementsByTagName('physician')->item(0)->nodeValue;
    $modality           = $xmlDoc->getElementsByTagName('modality')->item(0)->nodeValue;
    $priority           = $xmlDoc->getElementsByTagName('priority')->item(0)->nodeValue;
    $facility           = $xmlDoc->getElementsByTagName('facility')->item(0)->nodeValue;

    $findings           = $xmlDoc->getElementsByTagName('findings')->item(0)->nodeValue;
    $impression         = $xmlDoc->getElementsByTagName('impression')->item(0)->nodeValue;
    $diagnosis          = $xmlDoc->getElementsByTagName('diagnosis')->item(0)->nodeValue;
    $last               = $xmlDoc->getElementsByTagName('last')->item(0)->nodeValue;
    $first              = $xmlDoc->getElementsByTagName('first')->item(0)->nodeValue;
    $npi                = $xmlDoc->getElementsByTagName('npi')->item(0)->nodeValue;
    $signature_timestamp = $xmlDoc->getElementsByTagName('signature_timestamp')->item(0)->nodeValue;

    //not process if no accession number
    if($accession_number == '') return false;
    
    //not process if not match accession number
    $accession_number   = mysql_real_escape_string($accession_number);
    $result             = mysql_query("SELECT * FROM study WHERE accessionnum = '$accession_number'");

    if(!$result || mysql_num_rows($result) == 0) return false;

    //get file name
    $row            = mysql_fetch_assoc($result);
    $pdfFileName    = $row['uuid'].'.pdf'; //which field ?
    


    //create pdf ----------------------------------

    require_once 'PHPExcel/Classes/PHPExcel.php';

    // Create new PHPExcel object
    $objPHPExcel = new PHPExcel();

    // Set properties
    $objPHPExcel->getProperties()->setCreator("SonIT")
                                 ->setLastModifiedBy("SonIT")
                                 ->setTitle("X-RAY");
    $objPHPExcel->createSheet();
    $objPHPExcel->setActiveSheetIndex(0);

    $sheet = $objPHPExcel->getActiveSheet();
    $sheet->setTitle('X-RAY');
    $sheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_DEFAULT)
                            ->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
    
    $sheet->setShowGridlines(false);

    $rowSub = 1;
    $objDrawing = new PHPExcel_Worksheet_Drawing();
    $objDrawing->setPath(TOP_IMAGE);
    $objDrawing->setCoordinates('A'.($rowSub+1));
    $objDrawing->setWorksheet($sheet);

    $sheet->getColumnDimension('A')->setWidth(25);
    $sheet->getColumnDimension('B')->setWidth(30);
    $sheet->getColumnDimension('C')->setWidth(25);
    $sheet->getColumnDimension('D')->setWidth(34);

    $rowSub +=3;
    $sheet->getStyle('A'.$rowSub)->getFont()->setBold(true);
    $sheet->getStyle('A'.$rowSub)->getAlignment()->setWrapText(true);
    $sheet->setCellValue('A'.$rowSub, 'PATIENT:');
    $sheet->getStyle('B'.$rowSub)->getAlignment()->setWrapText(true);
    $sheet->setCellValue('B'.$rowSub, $patient_first.' '.$patient_last);

    $sheet->getStyle('C'.$rowSub)->getFont()->setBold(true);
    $sheet->getStyle('C'.$rowSub)->getAlignment()->setWrapText(true);
    $sheet->setCellValue('C'.$rowSub, 'REFERRING MD:');
    $sheet->getStyle('D'.$rowSub)->getAlignment()->setWrapText(true);
    $sheet->setCellValue('D'.$rowSub, $physician);

    $rowSub ++;
    $sheet->getStyle('A'.$rowSub)->getFont()->setBold(true);
    $sheet->getStyle('A'.$rowSub)->getAlignment()->setWrapText(true);
    $sheet->setCellValue('A'.$rowSub, 'DOB:');
    $sheet->getStyle('B'.$rowSub)->getAlignment()->setWrapText(true);
    $sheet->setCellValue('B'.$rowSub, $patient_dob);

    $sheet->getStyle('C'.$rowSub)->getFont()->setBold(true);
    $sheet->getStyle('C'.$rowSub)->getAlignment()->setWrapText(true);
    $sheet->setCellValue('C'.$rowSub, 'INTERPRETING MD:');
    $sheet->getStyle('D'.$rowSub)->getAlignment()->setWrapText(true);
    $sheet->setCellValue('D'.$rowSub, $first.' '.$last); //do we need NPI ? timestamp ?

    $rowSub ++;
    $sheet->getStyle('A'.$rowSub)->getFont()->setBold(true);
    $sheet->getStyle('A'.$rowSub)->getAlignment()->setWrapText(true);
    $sheet->setCellValue('A'.$rowSub, 'DOS:');
    $sheet->getStyle('B'.$rowSub)->getAlignment()->setWrapText(true);
    $sheet->setCellValue('B'.$rowSub, $date_of_exam);

    $sheet->getStyle('C'.$rowSub)->getFont()->setBold(true);
    $sheet->getStyle('C'.$rowSub)->getAlignment()->setWrapText(true);
    $sheet->setCellValue('C'.$rowSub, 'FACILITY:');
    $sheet->getStyle('D'.$rowSub)->getAlignment()->setWrapText(true);
    $sheet->setCellValue('D'.$rowSub, $facility);

    $rowSub ++;
    $sheet->getStyle('A'.$rowSub)->getFont()->setBold(true);
    $sheet->getStyle('A'.$rowSub)->getAlignment()->setWrapText(true);
    $sheet->setCellValue('A'.$rowSub, 'STUDY:');
    $sheet->getStyle('B'.$rowSub)->getAlignment()->setWrapText(true);
	$sheet->getStyle('B'.$rowSub)->getFont()->setBold(true);
	$sheet->getStyle('B'.$rowSub)->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
    $sheet->setCellValue('B'.$rowSub, $exam_type);

    $alignArray = array('alignment' => array(
								'horizontal'    => PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY,
								'vertical'      => PHPExcel_Style_Alignment::VERTICAL_TOP,
								'wrap'          => true
							 ));

    $rowSub += 2;
    $sheet->getStyle('A'.$rowSub.':D'.$rowSub)->applyFromArray($alignArray);
    $sheet->mergeCells('A'.$rowSub.':D'.$rowSub);
    $sheet->setCellValue('A'.$rowSub, 'DEAR:    '.$physician.'  MD');
	
    $rowSub +=2;
	$sheet->mergeCells('A'.$rowSub.':D'.$rowSub);

	$objRichText = new PHPExcel_RichText();
	$objStudy = $objRichText->createTextRun("REASON FOR STUDY:    ");
	$objStudy->getFont()->setBold(true);
//	$objStudy->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN ) );
	$objRichText->createText($exam_reason);
	
	$sheet->getCell('A'.$rowSub)->setValue($objRichText);


    $rowSub += 2;
    $sheet->mergeCells('A'.$rowSub.':D'.$rowSub);
	$sheet->getStyle('A'.$rowSub)->getFont()->setBold(true);
    $sheet->setCellValue('A'.$rowSub, "FINDINGS:");

	$rowSub ++;
    $sheet->getStyle('A'.$rowSub.':D'.$rowSub)->applyFromArray($alignArray);
    $sheet->mergeCells('A'.$rowSub.':D'.$rowSub);
    $sheet->setCellValue('A'.$rowSub, $findings);

    $rowSub += 2;
    $sheet->mergeCells('A'.$rowSub.':D'.$rowSub);
    $sheet->getStyle('A'.$rowSub)->getFont()->setBold(true);
    $sheet->setCellValue('A'.$rowSub, "IMPRESSION:");
    $rowSub ++;
    $sheet->getStyle('A'.$rowSub.':D'.$rowSub)->applyFromArray($alignArray);
    $sheet->mergeCells('A'.$rowSub.':D'.$rowSub);
	$sheet->getStyle('A'.$rowSub)->getFont()->setBold(true);
    $sheet->setCellValue('A'.$rowSub, $impression);

    $rowSub += 4;
    $sheet->getStyle('A'.$rowSub.':D'.$rowSub)->applyFromArray($alignArray);
    $sheet->mergeCells('A'.$rowSub.':D'.$rowSub);
    $sheet->setCellValue('A'.$rowSub, "Thank you for allowing BUFFALO ULTRASOUND to participate in the care of this patient.\nShould you have any questions concerning this report or the study performed,\nPlease do not hesitate to contact us.");

    $rowSub +=2;
    $sheet->getStyle('A'.$rowSub.':D'.$rowSub)->applyFromArray($alignArray);
    $sheet->mergeCells('A'.$rowSub.':D'.$rowSub);
    $sheet->setCellValue('A'.$rowSub,"Very Truly Yours,");
    $rowSub ++;
//    $sheet->getStyle('A'.$rowSub.':D'.$rowSub)->applyFromArray($alignArray);
//    $sheet->mergeCells('A'.$rowSub.':D'.$rowSub);
//    $sheet->setCellValue('A'.$rowSub,"Buffalo Ultrasound, IDTF");

    $rowSub ++;
    $sheet->getStyle('A'.$rowSub.':D'.$rowSub)->applyFromArray($alignArray);
    $sheet->setCellValue('A'.$rowSub,"E-SIGNED BY:   ");
    $sheet->getStyle('A'.$rowSub)->getFont()->setBold(true);
    $sheet->getStyle('A'.$rowSub)->getFont()->setSize(14);
    $sheet->setCellValue('B'.$rowSub, $first.' '.$last.",  MD\nBoard Certified Radiologist");

    $sheet->setCellValue('C'.$rowSub,"ON:");
    $sheet->getStyle('C'.$rowSub)->getFont()->setBold(true);
    $sheet->getStyle('C'.$rowSub)->getFont()->setSize(14);
	$sheet->getStyle('C'.$rowSub)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
    $sheet->setCellValue('D'.$rowSub, "   ".$signature_timestamp);

    $rowSub +=6;
    $objDrawing = new PHPExcel_Worksheet_Drawing();
    $objDrawing->setPath(BOTTOM_IMAGE);
    $objDrawing->setCoordinates('A'.($rowSub+1));
    $objDrawing->setWorksheet($sheet);

    $rowSub +=2;
    $sheet->mergeCells('A'.$rowSub.':D'.$rowSub);
    $sheet->getStyle('A'.$rowSub)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $sheet->getStyle('A'.$rowSub)->getFont()->setItalic(true);
    $sheet->setCellValue('A'.$rowSub,"Patient Name: $patient_first $patient_last"."                 DOB: $patient_dob            DOS: $date_of_exam                         Page 1 of 1");

	$sheet->setBreak( 'A' . ($rowSub+1), PHPExcel_Worksheet::BREAK_ROW );
	
    try
    {
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
        $objWriter->save(PDF_FOLDER_PATH.$pdfFileName);
    }
    catch (Exception $e)
    {
        return false;
    }
    //end create pdf -------------------------------

    //update db
	mysql_query("UPDATE study SET reviewed = 'xml' WHERE accessionnum = '$accession_number'");

    //remove xml file
    unlink($filenameFullPath);

    //done
    return true;
}

the quality of the image is dramatically reduced compared to the original images)  Image is a jpeg! 

 

suggestions?