Skip to content

Instantly share code, notes, and snippets.

@ginsterbusch
Last active December 18, 2015 21:49
Show Gist options
  • Save ginsterbusch/5850115 to your computer and use it in GitHub Desktop.
Save ginsterbusch/5850115 to your computer and use it in GitHub Desktop.
PDF Download test
<?php
/**
* PDF download test
*
* @author Fabian Wolf
* @link http://usability-idealist.de/
*
* NOTE: Copy + paste from @link http://www.php.net/manual/de/function.header.php#refsect1-function.header-examples
*/
// init
// full path to source file. if none given, PHP is trying to find it in the current directory (where THIS php script is sitting in)
$strSourceFile = 'Trambahner.pdf';
// name of the destination file aka what will be downloaded as $name
$strDestFile = 'my-downloaded.pdf';
// main
if( !file_exists( $strSourceFile ) ) {
exit('Error: Could not find ' . $strSourceFile . '!');
}
// disable caching
header('Expires: 0');
header('Cache-Control: must-revalidate');
// indicates PDF
header('Content-type: application/pdf');
// file size
header('Content-Length: ' .(string) (filesize( $strSourceFile ) ) );
// initiate download
header('Content-Disposition: attachment; filename="'.$strDestFile.'"');
/**
* Avoid fun with invalid BINARY code (so, do NOT use this with regular PDF files!)
* @see http://www.php.net/manual/de/function.header.php#81045
*/
//ob_start();
// read source file
readfile( $strSourceFile );
// flush buffer
ob_end_clean();
// done
exit;
// closing tag intentionally left out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment