Last active
February 7, 2020 06:55
-
-
Save brifiction/4b95f3ddc6e28743ae106fac145ce7f0 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 DocumentController 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"; | |
} | |
// catch empty or null | |
// TODO provide a better return statement, below is definitely not fun | |
if (empty($file) || is_null($file)) { | |
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