Skip to content

Instantly share code, notes, and snippets.

@2208Abhinav
Last active December 17, 2018 09:42
Show Gist options
  • Save 2208Abhinav/af958a663e4c3f7cdebf8dec89dfb07f to your computer and use it in GitHub Desktop.
Save 2208Abhinav/af958a663e4c3f7cdebf8dec89dfb07f to your computer and use it in GitHub Desktop.
<?php
// include the TCPDF class
require_once '../../globals.php';
require_once('../../../modules/pdf_generator/TCPDF/tcpdf.php');
require_once "$srcdir/options.inc.php";
require_once "$srcdir/formdata.inc.php";
require_once "$srcdir/formsoptions.inc.php";
require_once "$srcdir/patient.inc";
$encounter_data = sqlQuery('SELECT `date` as date, `case_number` as case_num, `facility` as facility_name FROM `form_encounter` WHERE `encounter` = ?', [$encounter]);
$case_exists = sqlQuery('SELECT `pt_eval_id` as eval_exists FROM `pt_evaluations` WHERE `encounter` = ? AND `pid` = ? AND `case_number` = ?', [$encounter, $pid, $encounter_data['case_num']]);
$injury_info = sqlQuery('SELECT `pci_case_injury_type` as injury_type, `pci_case_injury_date` as injury_date, `pci_surgery_date` as surgery_date,`pci_surgery` as surgery_y_n FROM `pt_case_info` WHERE `pci_case_number` = ?', [$encounter_data['case_num']]);
if (strlen($injury_info['injury_date']) != 0) {
$injury_date = $injury_info['injury_date'];
} else {
$injury_date = 'None';
}
if ($case_exists['eval_exists']) {
#record already exists
$case_eval_id = $case_exists['eval_exists'];
}else{
#insert record into pt_evaluations
$pt_eval_id = sqlInsert("INSERT INTO pt_evaluations (encounter, pid, case_number, created_at, initial_eval) VALUES (?,?,?,NOW(),1)", array($encounter, $pid, $encounter_data['case_num']));
$case_eval_id = $pt_eval_id;
}
$subjective_obj = sqlQuery("SELECT * FROM `pt_evaluation_form_subjective` WHERE `evaluation_id` = ? ", array($case_eval_id));
$result = getPatientData($pid, "*, DATE_FORMAT(DOB,'%Y-%m-%d') as DOB_YMD");
$provider_id = $result['providerID'];
$therapist_id = sqlQuery("SELECT * FROM users WHERE id = ?", array($provider_id));
$therapist_name = $therapist_id['fname'] . " " . $therapist_id['mname'] . " " . $therapist_id['lname'];
$therapist_location = $therapist_id['street'] . " " . $therapist_id['city'] . "/" . $therapist_id['state'];
$therapist_phone = $therapist_id['phone'];
$therapist_fax = $therapist_id['fax'];
$referring_id = $result['referrer'];
$referring_phy = sqlQuery("SELECT * FROM users WHERE id = ?", array($referring_id));
$referring_phy_name = $referring_phy['fname'] . " " . $referring_phy['mname'] . " " . $referring_phy['lname'];
$pcp_id = $result['pcpID'];
$pcp_phy = sqlQuery("SELECT * FROM users WHERE id = ?", array($pcp_id));
$pcp_phy_name = $pcp_phy['fname'] . " " . $pcp_phy['mname'] . " " . $pcp_phy['lname'];
$patient_phone = $result['phone_contact'];
$result3 = getInsuranceDataCase($pid, "primary", "copay, provider, DATE_FORMAT(`date`,'%Y-%m-%d') as effdate", $encounter_data['case_num'] );
if (strlen(getInsuranceProvider($result3['provider'])) != 0) {
$ins_company_1 = getInsuranceProvider($result3['provider']);
} else {
$ins_company_1 = 'None';
}
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Oxford');
$pdf->SetTitle('Initial Evaluation Form');
$pdf->SetSubject('Physical Therapy');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, 'Oxford Physical Therapy', 'Initial Evaluation Form', array(0,64,255), array(0,64,128));
$pdf->setFooterData(array(0,64,0), array(0,64,128));
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$PDF_MARGIN_TOP = 20; // custom value for better pdf appearance
$pdf->SetMargins(PDF_MARGIN_LEFT, $PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
// ---------------------------------------------------------
// set default font subsetting mode
$pdf->setFontSubsetting(true);
// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
$pdf->SetFont('dejavusans', '', 10, '', true);
// Add a page
// This method has several options, check the source code documentation for more information.
$pdf->AddPage();
// set text shadow effect
$pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));
$content = $_POST['content'];
$content = json_encode($content);
header('Content-Type: application/json');
$content = json_decode($content, true);
//error_log("pdf_data 1 : ". $content);
// first block of the PDF which contains logo
$html .= '<table>
<tr>
<td>
<p><strong style="background-color: yellow">Patient Evaluation</strong></p>
<p style="margin-top: -15px">Evaluation ' .
oeFormatShortDate(date("Y-m-d", strtotime($encounter_data["date"]))) . '</p>
</td>
<td></td>
<td>
<img class="img-responsive" src="' . $webroot . '/sites/default/images/oxford_logo.png" width="195px" height="40px">
<p>7567 Central Parke Boulevard</p>
<p style="margin-top: -15px">Mason, OH 45040-6855</p>
</td>
</tr>
</table>
<br>';
$style = array('width' => 0.7, 'color' => array(128, 0, 128)); // store the width and color of line
$lineX1Coordinate = 14.8; // These values are calculated accurately
$lineX2Coordinate = 195.2; // in accordance with the line below header.
$pdf->Line($lineX1Coordinate, 53, $lineX2Coordinate, 53, $style); // 60 are the y1, y2 coordinates of line
$html .= '<table>
<tr>
<td>
<p>Patient:<br> <strong>'. getPatientName($pid) . '</strong></p>
</td>
<td>
<p>Patient ID:<br> <strong>' . $pid . '</strong></p>
</td>
<td>
<p>DOB:<br> <strong>'. oeFormatShortDate(getPatientDOBOnly($pid)) . '</strong></p>
</td>
</tr>
<tr>
<td>
<p>Age:<br> <strong>'. getPatientAgeDisplay($result["DOB_YMD"]) . '</strong></p>
</td>
<td>
<p>Phone:<br> <strong>' . attr($patient_phone) . '</strong></p>
</td>
<td>
<p>Insurance:<br> <strong>' . attr($ins_company_1) . '</strong></p>
</td>
</tr>
<tr>
<td>
<p>Injury Date:<br> <strong id="inj_date">'. oeFormatShortDate($injury_date) . '</strong></p>
</td>
<td>
<p>Diagnosis:<br> <strong>' . $subjective_obj["primary_icd10"] . '</strong></p>
</td>
<td>
<p>Referred by:<br> <strong>' . attr($referring_phy_name) . '</strong></p>
</td>
</tr>
<tr>
<td>
<p>Initial Evaluation Date:<br> <strong>' . oeFormatShortDate(date("Y-m-d", strtotime($encounter_data["date"]))) . '</strong></p>
</td>
<td>
<p>Total Visits/CXL/NS:<br> <strong>' . attr($patient_phone) . '</strong></p>
</td>
<td>
<p>Therapist:<br> <strong>' . attr($therapist_name) . '</strong></p>
</td>
</tr>
<tr>
<td>
<p>Location:<br> <strong>' . attr($therapist_location) . '</strong></p>
</td>
<td>
<p>Phone:<br> <strong>' . attr($therapist_phone) . '</strong></p>
</td>
<td>
<p>Fax:<br> <strong>' . attr($therapist_fax) . '</strong></p>
</td>
</tr>
<tr>
<td>
<p>Primary Care Physician:<br> <strong>' . attr($pcp_phy_name) . '</strong></p>
</td>
<td></td>
<td></td>
</tr>
</table>
<hr style="background-color: purple" />';
$pdf->WriteHTML($html , true, false, true, false, '');
// This function is used to separate "text" type data from other type of data.
function separateTextFromOtherDataType($data) {
$textType = array();
$otherType = array();
foreach ($data as $field_key => $field_value) {
if(in_array("text", $field_value)){
$textType[$field_key] = key($field_value);
} else {
$otherType[$field_key] = key($field_value);
$otherTypeCounter;
}
}
return [$textType, $otherType];
}
foreach($content as $form_title=>$field_name) {
$html = ''; // Clean the $html variable data for second round of loop
$html .= '<table><br><br><br><b><u>' . $form_title . '</u></b><br><br>';
$filteredData = separateTextFromOtherDataType($field_name);
$textType = $filteredData[0];
$otherType = $filteredData[1];
// This block will output "text" type content to PDF with
// width = width of page.
foreach($textType as $field => $value) {
$html .= '<tr style="line-height: 1.6;"><td><b>' . $field . '</b> ' . $value . '</td></tr>';
}
$html .= '</table>';
$pdf->WriteHTML($html , true, false, true, false, '');
$html = ''; // Clean the content to output content of type other than "text".
$i = 1; // This counter helps in closing the <tr> properly.
$html .= "<table>";
// This block outputs the content in two column format.
foreach($otherType as $field => $value) {
if ($i%2 != 0) {
$html .= '<tr style="line-height: 1.6;">
<td><b>' . $field . '</b> ' . $value . '</td>';
} else {
$html .= '<td><b>' . $field . '</b> ' . $value . '</td>
</tr>';
}
$i++;
}
// This block closes the <tr> tag properly in case there are odd number of elements.
if ($i%2 == 0) {
$html .= '<td></td></tr>';
}
$html .= '</table>';
$pdf->WriteHTML($html , true, false, true, false, '');
}
//$pdf->Output($_SERVER["DOCUMENT_ROOT"] . '/' . $webroot . '/sites/default/documents/enc_' . $encounter . '_' . $pid . '_' . /*getPatientName($pid)*/ 'testing' . '.pdf' , 'I'); // save pdf to server
// $pdf->Output($_SERVER["DOCUMENT_ROOT"] . '/' . $webroot . '/sites/default/documents/enc_' . $encounter . '_' . $pid . '_' . getPatientName($pid) . '.pdf' , 'F'); // show print dialog
$pdf->Output("testing.pdf", 'D'); // Allows the user to download the pdf with name "testing"
//============================================================+
// END OF FILE
//============================================================+
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment