/** * Returns the contents of specific url passed by parameter using cURLs * * @param $url * @return mixed */ if( ! function_exists('phptopdf_curl_get') ) { function phptopdf_curl_get($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); $data = curl_exec($ch); curl_close($ch); return $data; } } /** * Returns the contents of specific url passed by parameter. * * @param $url * @return mixed|string * @throws Exception */ if( ! function_exists('phptopdf_get_contents') ) { function phptopdf_get_contents($url) { if (ini_get('allow_url_fopen') === '1') { // is allow_url_fopen = On ? $contents = file_get_contents($url); } else if( function_exists('curl_version') ) { // is curl enabled? $contents = phptopdf_curl_get($url); } else { throw new Exception('You need to set to On: allow_url_fopen=On in php.ini OR enable php cURL.'); } return $contents; } } /** * Reads file into output buffer * * @param $pdf * @return int|mixed */ if( ! function_exists('phptopdf_readfile') ) { function phptopdf_readfile( $pdf ) { if (filter_var($pdf, FILTER_VALIDATE_URL) === FALSE) { // If not url echo $pdf; } else { if (ini_get('allow_url_fopen') === '1') { readfile( $pdf ); } else { echo phptopdf_curl_get( $pdf ); } } } } /** * Writes PDF File to disk * @param $pdf * @param null $pdf_name * @param null $save_directory */ function savePDF($pdf, $pdf_name = NULL, $save_directory = NULL) { if ($pdf_name == NULL) { $pdf_name = API_KEY . substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", 9)), 0, 9) . ".pdf"; } if ($save_directory == NULL || $save_directory === '') { $save_directory = getcwd(); } if (DIRECTORY_SEPARATOR == '/') { $save_directory = preg_replace('~/+~', '/', $save_directory); } if(is_dir($save_directory) && is_writable($save_directory)) { $full_dir = $save_directory . DIRECTORY_SEPARATOR . $pdf_name; if (DIRECTORY_SEPARATOR == '/') { $full_dir = preg_replace('~/+~', '/', $full_dir); } $fp = fopen($full_dir, "w"); fwrite($fp, $pdf); fclose($fp); } else { header('Content-type: application/pdf'); echo $pdf; } } /** * Outputs PDF file in the browser * @param $pdf * @param null $pdf_name */ function downloadPDF($pdf, $pdf_name = NULL) { if ($pdf_name == NULL) { $pdf_name = API_KEY . substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", 9)), 0, 9) . ".pdf"; } header('Content-type: application/pdf'); header("Content-disposition: attachment; filename=" . $pdf_name); phptopdf_readfile( $pdf ); }