Created
August 30, 2016 08:09
-
-
Save MikSDigital/54b46bdb98d9d83d7067036e9761d678 to your computer and use it in GitHub Desktop.
pdf creation snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL); | |
ini_set('display_errors', 1); | |
$domain = 'http://' . $_SERVER['HTTP_HOST']; | |
$cache_path = ($_SERVER['DOCUMENT_ROOT']. '/cache/'); | |
$url = $domain . strtok($_SERVER["REQUEST_URI"],'?'); // requested URL w/o params | |
$full_url = $domain . $_SERVER['REQUEST_URI']; // for example http://devamcomau.int.fxservice.com/trading-platforms/mt4-se/indicator-innovations | |
$cultureKey = ('_' . $modx->getOption('cultureKey')); | |
$pdf_type = (isset($_GET['pdf_type']) ? (strip_tags($_GET['pdf_type'])) : false); | |
$fileName = ($modx->resource->get('alias')) . $pdf_type . $cultureKey . '.pdf'; | |
$dir = ($_SERVER['DOCUMENT_ROOT']. '/cache/pdf/'); | |
$filePath = ($dir . $fileName); | |
/* | |
$pdf_download_name = '_download'; | |
$pdf_print_name = '_print'; | |
$pdf_download_dir = ($dir . $modx->resource->get('alias') . $pdf_download_name) . $cultureKey . '.pdf'; | |
$pdf_print_dir = ($dir . $modx->resource->get('alias') . $pdf_print_name) . $cultureKey . '.pdf'; | |
*/ | |
if($pdf_type) { | |
$curl = curl_init(); | |
curl_setopt_array($curl, array( | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_URL => $url | |
)); | |
$result = curl_exec($curl); | |
curl_close($curl); | |
// parse target page, get content for pdf | |
$xml = new DOMDocument(); | |
libxml_use_internal_errors(true); // to prevent possible warnings show | |
$xml->loadHtml($result); | |
libxml_use_internal_errors(false); | |
$xpath = new DOMXPath($xml); | |
$html = ''; | |
foreach ($xpath->query('//div[@id="content"]/node()') as $node) | |
{ | |
$html .= $xml->saveHTML($node); | |
} | |
$html = str_replace("http://dce5jani6jm7e.cloudfront.net/", "/assets/common/", $html); // result html to convert to pdf, switch to local image paths | |
$mode = 'utf-8'; | |
//$format = ($cultureKey === '_de') ? ('A4-L') : 'A4'; | |
$format = 'A4'; | |
$font_size =''; | |
$font = ''; | |
$margin_left = 20; | |
$margin_right = 12; | |
$margin_top = 35; | |
$margin_bottom = ''; | |
$margin_header = 15; | |
$margin_footer = ''; | |
$orientation = ''; | |
$mpdf = new mPDF($mode, $format, $font_size, $font, $margin_left, $margin_right, $margin_top, $margin_bottom, $margin_header, $margin_footer, $orientation); | |
//$mpdf->showImageErrors = true; /* Show custom images in PDF file */ | |
//$mpdf->autoScriptToLang = true; | |
//$mpdf->autoLangToFont = true; | |
$htmlTpl = array( | |
'header' => '<div> | |
<table width="100%"> | |
<tr width="100%"> | |
<td width="50%" style="border: 0;"><img src="{{logo}}" alt="" style="width: 150px;" /></td> | |
<td width="50%" style="border: 0;" align="right"> | |
<p style="font-size: 11px;line-height: 9px;">{{legalName}}</p> | |
<p style="font-size: 11px;line-height: 9px;">{{address}}</p> | |
<p style="font-size: 11px;line-height: 9px;">{{country}}</p> | |
<p style="font-size: 11px;line-height: 9px;">{{main_phone}}</p> | |
<p style="font-size: 11px;line-height: 9px;">{{compliance_email}}</p> | |
</td> | |
</tr> | |
</table> | |
</div>', | |
'footer' => '<div class="text-center"> | |
<p style="font-size: 11px;line-height: 15px;">{{legalName}}</p> | |
<p style="font-size: 11px;line-height: 15px;">Registered Office: As Above 2/2</p> | |
<p style="font-size: 11px;line-height: 15px;">Registered Number: 08171762</p> | |
<p style="font-size: 11px;line-height: 15px;">Authorised and Regulated by the Financial Conduct Authority FRN: 595450</p> | |
</div>' | |
); | |
function replaceString($string, $array) { | |
return strtr($string, $array); | |
}; | |
$mpdf->SetHTMLHeader(replaceString($htmlTpl['header'], | |
array( | |
'{{logo}}' => $domain . '/assets/common/images/elements/logo.png', | |
'{{legalName}}' => $modx->getOption('legalName'), | |
'{{address}}' => $modx->getOption('address'), | |
'{{country}}' => $modx->getOption('country'), | |
'{{main_phone}}' => ($cultureKey === '_de') ? $modx->getOption('local_phone1') : $modx->getOption('main_phone'), | |
'{{compliance_email}}' => ($cultureKey === '_de') ? $modx->getOption('support_email') : $modx->getOption('compliance_email') | |
) | |
), 'O', true); | |
//$html=utf8_encode($html); | |
$mpdf->WriteHTML($html); | |
if($pdf_type == 'download') { | |
$mpdf->Output($fileName,'D'); | |
} | |
if($pdf_type == 'print') { | |
//header('Content-type: application/pdf'); | |
//readfile($fileName); | |
$mpdf->SetJS('this.print();'); | |
$mpdf->Output($fileName,'I'); | |
} | |
exit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment