Skip to content

Instantly share code, notes, and snippets.

@BHWD
Created July 20, 2018 10:19
Show Gist options
  • Save BHWD/b7ce33269dcc588e06526d33b4eddb6b to your computer and use it in GitHub Desktop.
Save BHWD/b7ce33269dcc588e06526d33b4eddb6b to your computer and use it in GitHub Desktop.
<?php
function new_us_simple_pdf_generate_cover_sheet( ) {
$postID = $_POST['postID'];
$siteRef1 = get_field('site_ref_1', $postID);
if (! class_exists ( 'TCPDF' )) {
require_once WP_PLUGIN_DIR . '/utility-search/pdf-gen/tcpdf_min/tcpdf.php';
}
if (! class_exists ( 'pdfheader' )) {
require_once WP_PLUGIN_DIR . '/utility-search/pdf-gen/pdfheader.php';
}
if (! class_exists ( 'simple_html_dom_node' )) {
require_once WP_PLUGIN_DIR . '/utility-search/pdf-gen/simplehtmldom/simple_html_dom.php';
}
$fileName = '/US'.$postID.'-'.$siteRef1.'.pdf';
$filePath = US_CACHE_DIR . $fileName;
$pagesize = 'A4';
$unit = 'mm';
$orientation = 'P';
$pdf = new CUSTOMPDF ( $orientation, $unit, $pagesize, true, 'UTF-8', false );
// information about doc
$pdf->SetCreator ( 'Utility Search' );
$pdf->SetAuthor ( 'Utility Search' );
$logoImage_url = get_field('site_search_settings_pdf_settings_pdf_cover_sheet_logo', 'site_search_settings');
if ( !empty( $logoImage_url ) ) {
$infologo = getimagesize ( $logoImage_url );
try {
$logo_width = @ ( int ) ((14 * $infologo [0]) / $infologo [1]);
}
catch(Exception $e){
throw new Exception("Invalid Size Image..");
echo "Exception:".$e->getMessage();
}
}
$pdf->SetHeaderData ( $logoImage_url, $logo_width , '','',array(0,0,0), array(255,255,255) );
$header_font_size = 10;
$footer_font_size = 10;
$pdf->setHeaderFont ( array (
'helvetica',
'',
$header_font_size
) );
$pdf->setFooterFont ( array (
'helvetica',
'',
$footer_font_size
) );
$pdf->SetMargins(15, 15, 15);
$pdf->SetLeftMargin (15);
$pdf->SetRightMargin (15);
$pdf->SetTopMargin (25);
$pdf->SetHeaderMargin (10);
$pdf->SetFooterMargin (10);
$pdf->SetAutoPageBreak ( TRUE, 10);
$pdf->setImageScale ( 1.25 );
// set default font subsetting mode
$pdf->setFontSubsetting ( true );
$pdf->setFontStretching(100);
$page_format = array();
$page_format['Rotate'] = 0;
$pdf->AddPage($orientation, $page_format, false, false);
//Search Submission Date
$pdf->Ln(5);
$pdf->SetFont ( 'helvetica', 'B', 10 );
$report_date = get_the_date('dS F Y', $postID);
$pdf->Cell(0, 0, $report_date, 0, 1, 'C', 0, '', 0);
// Cover Sheet Title
$pdf->Ln(1);
$pdf->SetFont ( 'helvetica', 'B', 10 );
$pdf->Cell(0, 0, 'Utilities Search Report for', 0, 1, 'C', 0, '', 0);
// Site Name
$pdf->Ln(1);
$pdf->SetFont ( 'helvetica', 'B', 10 );
$site_name = get_field('title', $postID);
$pdf->Cell(0, 0, 'Site Name: '.$site_name, 0, 1, 'C', 0, '', 0);
// Site Ref 1
$site_ref_1 = get_field('site_ref_1', $postID);
$pdf->Ln(1);
$pdf->SetFont ( 'helvetica', 'B', 10 );
$pdf->Cell(0, 0, 'Ref. No. 1: '.$site_ref_1, 0, 1, 'C', 0, '', 0);
// Site Ref 2
if( get_field('site_ref_2', $postID) ):
$site_ref_2 = get_field('site_ref_2', $postID);
$pdf->Ln(1);
$pdf->SetFont ( 'helvetica', 'B', 10 );
$pdf->Cell(0, 0, 'Ref. No. 2: '.$site_ref_2, 0, 1, 'C', 0, '', 0);
endif;
// Search Table
$pdf->Ln(5);
$tbl = '
<table border="1" cellpadding="2" cellspacing="1" align="left">
<tr>
<td style="font-weight:bold;">Utility / Service</td>
<td style="font-weight:bold;">Status</td>
<td style="font-weight:bold;">Comments</td>
</tr>';
// Basic Search
$basic_search = get_field('basic_search', $postID);
// Gas Row
if( $basic_search && in_array('gas', $basic_search) ) { $gas_status = 'Yes'; } else { $gas_status = 'No'; }
$tbl .= '
<tr>
<td>Gas</td>
<td>'.$gas_status.'</td>
<td></td>
</tr>';
// Electricity Row
if( $basic_search && in_array('electricity', $basic_search) ) { $electricity_status = 'Yes'; } else { $electricity_status = 'No'; }
$tbl .= '
<tr>
<td>Electricity</td>
<td>'.$electricity_status.'</td>
<td></td>
</tr>';
// Water Row
if( $basic_search && in_array('water', $basic_search) ) { $water_status = 'Yes'; } else { $water_status = 'No'; }
$tbl .= '
<tr>
<td>Water</td>
<td>'.$water_status.'</td>
<td></td>
</tr>';
// Sewer Row
if( $basic_search && in_array('sewer', $basic_search) ) { $sewer_status = 'Yes'; } else { $sewer_status = 'No'; }
$tbl .= '
<tr>
<td>Sewer</td>
<td>'.$sewer_status.'</td>
<td></td>
</tr>';
// BT Row
if( $basic_search && in_array('bt', $basic_search) ) { $bt_status = 'Yes'; } else { $bt_status = 'No'; }
$tbl .= '
<tr>
<td>BT</td>
<td>'.$bt_status.'</td>
<td></td>
</tr>';
// Cable Search
if ( have_rows( 'cable_search', $postID ) ) :
while ( have_rows( 'cable_search', $postID ) ) : the_row();
$free_cable_array = get_sub_field( 'free_cable', $postID);
if( $free_cable_array && in_array('free-cable', $free_cable_array) ) { $free_cable_status = 'Yes'; } else { $free_cable_status = 'No'; }
$tbl .= '
<tr>
<td>Free Cable</td>
<td>'.$free_cable_status.'</td>
<td></td>
</tr>';
$premium_cable_array = get_sub_field( 'premium_cable', $postID );
if( $premium_cable_array && in_array('virgin-media', $premium_cable_array) ) { $virgin_media_status = 'Yes'; } else { $virgin_media_status = 'No'; }
$tbl .= '
<tr>
<td>Virgin Media</td>
<td>'.$virgin_media_status.'</td>
<td></td>
</tr>';
if( $premium_cable_array && in_array('vodafone', $premium_cable_array) ) { $vodafone_status = 'Yes'; } else { $vodafone_status = 'No'; }
$tbl .= '
<tr>
<td>Vodafone</td>
<td>'.$vodafone_status.'</td>
<td></td>
</tr>';
if( $premium_cable_array && in_array('vtesse', $premium_cable_array) ) { $vtesse_status = 'Yes'; } else { $vtesse_status = 'No'; }
$tbl .= '
<tr>
<td>Vtesse</td>
<td>'.$vtesse_status.'</td>
<td></td>
</tr>';
endwhile;
endif;
// Independant Utilities
if ( have_rows( 'independent_utilities', $postID ) ) :
while ( have_rows( 'independent_utilities', $postID ) ) : the_row();
$independent_utilities_array = get_sub_field( 'independent_utilities', $postID);
if( $independent_utilities_array && in_array('free-iu', $independent_utilities_array) ) { $free_iu_status = 'Yes'; } else { $free_iu_status = 'No'; }
$tbl .= '
<tr>
<td>Free IU\'s</td>
<td>'.$free_iu_status.'</td>
<td></td>
</tr>';
$additional_independent_utilities_array = get_sub_field( 'additional_independent_utilities', $postID );
if( $additional_independent_utilities_array && in_array('uk-power-distribution', $additional_independent_utilities_array) ) { $uk_power_distribution_status = 'Yes'; } else { $uk_power_distribution_status = 'No'; }
$tbl .= '
<tr>
<td>UK Power Distribution</td>
<td>'.$uk_power_distribution_status.'</td>
<td></td>
</tr>';
if( $additional_independent_utilities_array && in_array('harlaxton', $additional_independent_utilities_array) ) { $uk_power_distribution_status = 'Yes'; } else { $uk_power_distribution_status = 'No'; }
$tbl .= '
<tr>
<td>Harlaxton</td>
<td>'.$uk_power_distribution_status.'</td>
<td></td>
</tr>';
endwhile;
endif;
// Transport Search
if ( have_rows( 'transport_search', $postID ) ) :
while ( have_rows( 'transport_search', $postID ) ) : the_row();
$transport_search_array = get_sub_field( 'transport_search', $postID);
if( $transport_search_array && in_array('transport', $transport_search_array) ) { $transport_status = 'Yes'; } else { $transport_status = 'No'; }
$tbl .= '
<tr>
<td>Transport</td>
<td>'.$transport_status.'</td>
<td></td>
</tr>';
endwhile;
endif;
// CAD Search
if ( have_rows( 'cad', $postID ) ) :
while ( have_rows( 'cad', $postID ) ) : the_row();
$cad_array = get_sub_field( 'cad', $postID);
if( $cad_array && in_array('cad-all-in-one', $cad_array) ) { $cad_status = 'Yes'; } else { $cad_status = 'No'; }
$tbl .= '
<tr>
<td>CAD All-in-One</td>
<td>'.$cad_status.'</td>
<td></td>
</tr>';
$background_mapping_array = get_sub_field( 'background_mapping', $postID);
if( $background_mapping_array && in_array('background-mapping', $background_mapping_array) ) { $background_mapping_status = 'Yes'; } else { $background_mapping_status = 'No'; }
$tbl .= '
<tr>
<td>Background Mapping</td>
<td>'.$background_mapping_status.'</td>
<td></td>
</tr>';
endwhile;
endif;
$tbl .= '</table>';
$pdf->writeHTML($tbl, true, false, false, false, '');
if (! is_dir ( CPM_CACHE_DIR )) {
mkdir ( CPM_CACHE_DIR, 0755, true );
}
//ob_end_clean();
$pdf->Output ( $filePath, 'F' );
//$pdf->Output ( $filePath, 'I' );
//$upload_dir = wp_upload_dir();
echo $fileName;
exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment