Export pdf file from Laravel Vapor through S3 bucket

This commit is contained in:
Dillon Ngo
2024-06-18 11:38:43 +08:00
parent 38aa670528
commit fcaee29be3
8 changed files with 106 additions and 31 deletions
+23 -5
View File
@@ -2,19 +2,18 @@
namespace App\Classes\General;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Carbon\Carbon;
use Maatwebsite\Excel\Concerns\Exportable;
class AWSS3Helper
{
/**
* @param string $methodName
* @param string $exportFileName
* @param Exportable $exportableObject
* @return string
*/
static function S3($exportFileName, $exportableObject){
static function S3Exportable($exportFileName, $exportableObject){
//Step 1: Upload to S3
$filePathForS3 = 'temp/' . $exportFileName;
$exportableObject->store($filePathForS3, 's3');
@@ -28,4 +27,23 @@ class AWSS3Helper
return $temporaryUrl;
}
/**
* @param string $exportFileName
* @param string|resource $contents
* @return string
*/
static function S3PDF($exportFileName, $contents){
//Step 1: Upload to S3
$filePathForS3 = 'temp/' . $exportFileName;
Storage::disk('s3')->put($filePathForS3, $contents);
//Step 2: Return temporary URL from S3
$temporaryUrl = Storage::disk('s3')->temporaryUrl(
$filePathForS3,
Carbon::now()->addMinutes(10)
);
return $temporaryUrl;
}
}
@@ -10,6 +10,8 @@ use Mccarlosen\LaravelMpdf\Facades\LaravelMpdf;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Classes\General\AWSS3Helper;
use Illuminate\Support\Facades\Storage;
class GenerateCreditNotePdfLogic
{
@@ -46,6 +48,14 @@ class GenerateCreditNotePdfLogic
$pdf = LaravelMpdf::loadView('pages.pdfs.credit_note', ['transaction' => $transaction, 'booking' => $booking, 'supplier' => $supplier]);
return $pdf->stream('CreditNote.pdf');
$exportFileName = 'CreditNote.pdf';
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
$pdfContent = $pdf->output();
AWSS3Helper::S3PDF($exportFileName, $pdfContent);
}
else{
return $pdf->stream($exportFileName);
}
}
}
@@ -29,7 +29,7 @@ class ExportAnalyticToExcelController
$exportFileName = 'bookingData.csv';
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsAnalyticBookingTransactions) ]);
return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsAnalyticBookingTransactions) ]);
}
else{
$response = $exportsAnalyticBookingTransactions->download($exportFileName, Excel::CSV, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
@@ -42,7 +42,7 @@ class ExportAnalyticToExcelController
$exportFileName = 'billingData.csv';
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsAnalyticBillingTransactions) ]);
return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsAnalyticBillingTransactions) ]);
}
else{
$response = $exportsAnalyticBillingTransactions->download($exportFileName, Excel::CSV, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
@@ -41,7 +41,7 @@ class ExportCustomersToExcelController
$exportFileName = 'customers.csv';
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsCustomers) ]);
return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsCustomers) ]);
}
else{
return $exportsCustomers->download($exportFileName, Excel::CSV, ['Content-Type' => 'text/csv']);
@@ -52,7 +52,7 @@ class ExportCustomersToExcelController
$exportFileName = 'CurrencyVendorOrder.xls';
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportCurrencyVendorOrder) ]);
return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportCurrencyVendorOrder) ]);
}
else{
$response = $exportCurrencyVendorOrder->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
@@ -65,7 +65,7 @@ class ExportCustomersToExcelController
$exportFileName = 'transactions.csv';
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsTransactions) ]);
return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsTransactions) ]);
}
else{
return $exportsTransactions->download($exportFileName, Excel::CSV, ['Content-Type' => 'text/csv']);
@@ -76,7 +76,7 @@ class ExportCustomersToExcelController
$exportFileName = 'nullDebtor.xls';
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsNullDebtors) ]);
return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsNullDebtors) ]);
}
else{
$response = $exportsNullDebtors->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
@@ -90,7 +90,7 @@ class ExportCustomersToExcelController
$exportFileName = 'payment-transactions.xls';
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsTransactions) ]);
return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsTransactions) ]);
}
else{
$response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
@@ -104,7 +104,7 @@ class ExportCustomersToExcelController
$exportFileName = 'white-form-transactions.xls';
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsTransactions) ]);
return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsTransactions) ]);
}
else{
$response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
@@ -118,7 +118,7 @@ class ExportCustomersToExcelController
$exportFileName = 'wallet-transactions.xls';
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsTransactions) ]);
return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsTransactions) ]);
}
else{
$response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
@@ -132,7 +132,7 @@ class ExportCustomersToExcelController
$exportFileName = 'invoice-transactions.xls';
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsTransactions) ]);
return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsTransactions) ]);
}
else{
$response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
@@ -146,7 +146,7 @@ class ExportCustomersToExcelController
$exportFileName = 'receipt-transactions.xls';
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsTransactions) ]);
return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsTransactions) ]);
}
else{
$response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
@@ -159,7 +159,7 @@ class ExportCustomersToExcelController
$exportFileName = 'bookingTransactions.xls';
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsBookingTransactions) ]);
return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsBookingTransactions) ]);
}
else{
$response = $exportsBookingTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
@@ -172,7 +172,7 @@ class ExportCustomersToExcelController
$exportFileName = 'leadsData.xls';
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsLeadsTransactions) ]);
return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsLeadsTransactions) ]);
}
else{
$response = $exportsLeadsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
@@ -185,7 +185,7 @@ class ExportCustomersToExcelController
$exportFileName = $request->input('fileName').'.xls';
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsImportedInvoiceMappeds) ]);
return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsImportedInvoiceMappeds) ]);
}
else{
$response = $exportsImportedInvoiceMappeds->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
@@ -198,7 +198,7 @@ class ExportCustomersToExcelController
$exportFileName = $request->input('fileName').'.xls';
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsImportedReceiptMappeds) ]);
return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsImportedReceiptMappeds) ]);
}
else{
$response = $exportsImportedReceiptMappeds->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
@@ -29,7 +29,7 @@ class ExportCustomersWalletTransactionToExcelController
$exportFileName = $request->route('marking') . '-wallet-' . ($request->route('is_precise') == 'true' ? 'precise-' : '') . 'transaction-history.xls';
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsTransactions) ]);
return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsTransactions) ]);
}
else{
$response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
@@ -22,13 +22,13 @@
<div class="row bg-white padding-10 m-b-10 rounded" v-for="(item, index) in transaction" v-bind:key="item.id" :data="item">
<div class="col-2 fs-12">{{item.created_at}}</div>
<div class="col-3 fs-12">{{ convertTransactionType(item.type) }} {{ item.payment_reference ? ' - ' + item.payment_reference : '' }} <a target=_blank v-if="[9,11].includes(item.type) " :href="route('transaction.credit_note.download', item.id)"><i class="fa fa-download fs-11 m-l-5 text-secondary hover-primary"></i></a></div>
<div class="col-3 fs-12">{{ convertTransactionType(item.type) }} {{ item.payment_reference ? ' - ' + item.payment_reference : '' }} <a target=_blank v-if="[9,11].includes(item.type) " @click="downloadTransaction(item)"><i class="fa fa-download fs-11 m-l-5 text-secondary hover-primary"></i></a></div>
<div class="col fs-12"><a :href="route('booking.details', item.booking_marking)">{{item.booking_marking}}</a></div>
<div class="col-2 text-success text-center" v-if="parseFloat(item.type) !== 2">{{[5, 9].includes(parseFloat(item.type)) ? (Math.round((parseFloat(item.amount) + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") : ''}}</div>
<div class="col-2 text-success text-center" v-if="parseFloat(item.type) === 2">{{(Math.round(((parseFloat(item.amount) / parseFloat(item.currency_rate) + parseFloat(item.service_charge)) + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}}</div>
<div class="col-2 text-danger text-center">{{[1, 11].includes(parseFloat(item.type)) ? '- ' + (Math.round((parseFloat(item.amount) + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") : ''}}</div>
<div class="col-2 text-right">{{remainingBalance(index)}}</div>
</div>
</div>
</div>
</div>
</div>
@@ -119,7 +119,23 @@ export default {
successHandler(response){
this.isLoading = false;
this.transaction = response.payload.data;
}
},
downloadTransaction(paramItem) {
let url = this.route('transaction.credit_note.download', paramItem.id);
if(window.LARAVEL_VAPOR_ENABLED){
return fetch(url, {
method: 'GET',
responseType: 'json',
}).then(response => {
if (response.src) {
window.location.href = response.src;
}
});
}
else{
window.open(url, '_blank');
}
},
}
}
</script>
@@ -1,11 +1,11 @@
<template>
<div class="row bg-white padding-10 m-b-10 rounded">
<div class="col-3 fs-12">{{data.created_at}}</div>
<div class="col fs-12"><span v-html="data.description"></span> <a target=”_blank” v-if="[9,11].includes(data.type) " :href="route('transaction.credit_note.download', data.id)"><i class="fa fa-download fs-11 m-l-5 text-secondary hover-primary"></i></a></div>
<div class="col fs-12"><span v-html="data.description"></span> <a target=”_blank” v-if="[9,11].includes(data.type) " @click="downloadTransaction(data)"><i class="fa fa-download fs-11 m-l-5 text-secondary hover-primary"></i></a></div>
<div class="col-2 text-success text-center">{{[5, 9].includes(parseFloat(data.type)) ? formatValue(data.amount) : ''}}</div>
<div class="col-2 text-danger text-center">{{[1, 11].includes(parseFloat(data.type)) ? '- ' + (formatValue(data.amount)) : ''}}</div>
<div class="col-2 text-right">{{formatValue(data.running_balance)}}</div>
</div>
</div>
</template>
<script>
import componentHandler from '../../../general/mixins/componentHandler';
@@ -28,8 +28,24 @@
}
return (Math.round((parseFloat(value) + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")
}
},
downloadTransaction(paramItem) {
let url = this.route('transaction.credit_note.download', paramItem.id);
if(window.LARAVEL_VAPOR_ENABLED){
return fetch(url, {
method: 'GET',
responseType: 'json',
}).then(response => {
if (response.src) {
window.location.href = response.src;
}
});
}
else{
window.open(url, '_blank');
}
},
},
mixins: [componentHandler],
}
</script>
</script>
+17 -2
View File
@@ -226,7 +226,14 @@ Route::post('/support', function (Request $request) {
Route::get('/online_payment/redirect', 'Billplz\CallbackBillplzController@callback')->name('online_payment.redirect');
Route::get('/products', function (\App\Classes\Modules\Exports\Services\ExportsProducts $exportsProducts) {
return $exportsProducts->download('products.csv', Excel::CSV, ['Content-Type' => 'text/csv']);
$exportFileName = 'products.csv';
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
return response([ 'src' => App\Classes\General\AWSS3Helper::S3Exportable($exportFileName, $exportsProducts) ]);
}
else{
return $exportsProducts->download($exportFileName, Excel::CSV, ['Content-Type' => 'text/csv']);
}
})->name('products.random.1'); //duplicate name
Route::get('/fix_bills', function () {
@@ -306,7 +313,15 @@ Route::get('/products', function (\App\Classes\Modules\Exports\Services\ExportsP
$q->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED]);
})->get();
dd($bookings->count());
return $exportsProducts->download('products.csv', Excel::CSV, ['Content-Type' => 'text/csv']);
$exportFileName = 'products.csv';
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
return response([ 'src' => App\Classes\General\AWSS3Helper::S3Exportable($exportFileName, $exportsProducts) ]);
}
else{
return $exportsProducts->download('products.csv', Excel::CSV, ['Content-Type' => 'text/csv']);
}
})->name('products.random.3');
Route::get('/auto-purchase-order-fill', 'Bookings\AutoPurchaseOrderFillController@auto')->name('assign');