Created
February 7, 2020 04:48
-
-
Save brifiction/bf3f027225cf4eabab8b3df33ffb2f76 to your computer and use it in GitHub Desktop.
Laravel - Simple Print Document Example
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 | |
namespace App\Http\Controllers\External; | |
use Illuminate\Http\Request; | |
use App\Http\Controllers\Controller; | |
class AwardsSubmissionController extends Controller | |
{ | |
/** | |
* Create a new controller instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
$this->middleware(['auth', 'verified']); | |
} | |
/** | |
* Print document, to view or download. | |
* | |
* This function will be used, based on assumption if the | |
* document is stored as a blob entry and with | |
* mimetype, mime and additional file information. | |
* | |
* Of course, this is the 'quick and dirty' approach. | |
* | |
* @param int $key | |
* @param string $file | |
* @param string $fileName | |
* @param string $fileType | |
* @echo $file | |
* @return \Illuminate\Http\RedirectResponse | |
*/ | |
public function printDocument($key, $file, $fileName, $fileType) | |
{ | |
if ($fileType == "PDF") { | |
$fileType = "application/pdf"; | |
} | |
if ($fileType == "DOC") { | |
$fileType = "application/msword"; | |
} | |
if ($fileType == "PNG") { | |
$fileType = "image/png"; | |
} | |
if ($fileType == "JPG" || $fileType == "JPEG") { | |
$fileType = "image/jpeg"; | |
} | |
if ($file == " " || $file == null) { | |
return back(); | |
} | |
$file = base64_encode($file); | |
$file = base64_decode($file); | |
header('Content-Type: ' . $fileType); | |
header('Content-Disposition: attachment; filename=' . $fileName); | |
// used as self::printDocument($key, $file, $fileName, $fileType); | |
echo $file; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment