/ Published in: Other
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php //name the snippet as download_pdf, and then reference to it as [[!download_pdf]] if(isset($_REQUEST["pid"])){ ini_set('zlib.output_compression','0'); function generate_name($resname){ $rndname = chr(rand(65,90)).chr(rand(97,122)).chr(rand(48,57)).chr(rand(65,90)).chr(rand(97,122)).chr(rand(48,57)); $pdffilename = "/home/berlinp/public_html/beta/html2pdf/pdftmp/".$resname.$rndname.".pdf"; if(file_exists($pdffilename)) return generate_name($resname); else return $pdffilename; } $resId = mysql_escape_string($_REQUEST["pid"]); $url = "http://".$_SERVER['HTTP_HOST']."/beta/html2pdf/".$modx->makeUrl($resId); $name = $modx->getObject('modResource', $resId)->get('alias'); $pdffilename = generate_name($name); $cmd = "/home/berlinp/public_html/beta/cgi-bin/wkhtmltopdf -q --footer-center '[page]/[topage]' --page-size Letter $url $pdffilename"; shell_exec($cmd); //WAIT UP TO 20 SEC IF FILE IS NOT CREATED for($i=1;$i<20;$i++){ sleep(1); //wait to finish if(file_exists($pdffilename) && filesize($pdffilename)>10) break; } //WAIT UP TO 20 SEC IF FILE IS NOT CREATED if(!file_exists($pdffilename)) die("Sorry, an error ocours while downloading your pdf file. Please try again later."); header('Content-Type: application/pdf'); header('Content-Disposition: inline; filename="'.$name.'.pdf"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: '.filesize($pdffilename)); ob_clean(); flush(); readfile($pdffilename); unlink($pdffilename); exit(); }