Answer the question
In order to leave comments, you need to log in
Upload PDF file to DB on Laravel?
Hello coders, please tell me, I have a form, after clicking on the button in the controller, a PDF file is generated (so to speak, an invoice). and this file immediately opens in a new tab. I used this technology
If you think that the library is not suitable, then you can use this one.
What I need, I need to implement so that this file is saved in the database, and after that I can display the file ID, user name, and buttons on a separate page in a column download the file, and view the file in the browser (as it happens in this case when I click on the button in the form).
Here is the controller I'm processing:
<?php
namespace App\Http\Controllers;
use PDF;
use Illuminate\Http\Request;
class InvoiceController extends Controller
{
public function index(){
return view('invoice');
}
public function download(Request $request)
{
$name = $request->name;
$products = [
[
'title' => 'Product 1',
'price' => 10.99,
'quantity' => 1,
'totals' => 10.99
],
[
'title' => 'Product 2',
'price' => 14.99,
'quantity' => 2,
'totals' => 29.98
],
[
'title' => 'Product 3',
'price' => 500.00,
'quantity' => 1,
'totals' => 500.00
],
[
'title' => 'Product 4',
'price' => 6.99,
'quantity' => 3,
'totals' => 20.97
],
];
$total = collect($products)->sum('totals');
$pdf = PDF::loadView('pdf.invoices', compact('products', 'total', 'name'));
return $pdf->stream('invoice.pdf');//данный файл открывает в браузере
//return $pdf->stream('invoice.pdf');//Сохраняет файл на Пк
$pdf->save();
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateInvoiceUsers extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('invoice_users', function (Blueprint $table) {
$table->id();
$table->string('name_user');
$table->string('name_file');
//data
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('invoice_users');
}
}
<!DOCTYPE html>
<html>
<head>
<title>Invoice Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div style="width: 100%; max-width: 960px; margin: auto">
<table width="100%">
<tr style="border-bottom: 1px solid #000000">
<td><h2>Invoice</h2></td>
<td style="text-align: right"><h3>Order # 12345</h3></td>
</tr>
<tr>
<td style="padding-bottom: 16px;">
<strong>Billed To:</strong><br>
{{ $name }}<br>
1234 Victory Avenue<br>
Apt. 5D<br>
Sunfield, ST 54321
</td>
<td style="text-align: right; padding-bottom: 16px;">
<strong>Shipped To:</strong><br>
John Smith<br>
1234 Victory Avenue<br>
Apt. 5D<br>
Sunfield, ST 54321
</td>
</tr>
<tr>
<td>
<strong>Payment Method:</strong><br>
Visa ending **** 4242<br>
[email protected]
</td>
<td style="text-align: right">
<strong>Order Date:</strong><br>
March 7, 2014<br><br>
</td>
</tr>
<tr>
<td colspan="2">
<h3>Order summary</h3>
</td>
</tr>
<tr>
<td colspan="2">
<table width="100%" cellpadding="0" cellspacing="0" border="1">
<thead>
<tr style="background-color: #eee">
<th style="text-align: left; padding: 5px 10px;">Item</th>
<th style="text-align: center; padding: 5px 10px;">Price</strong></th>
<th style="text-align: center; padding: 5px 10px;">Quantity</th>
<th style="text-align: right; padding: 5px 10px;">Totals</th>
</tr>
</thead>
<tbody>
@foreach ($products as $product)
<tr>
<td style="text-align: left; padding: 5px 10px;">{{ $product['title'] }}</td>
<td style="text-align: center; padding: 5px 10px;">{{ $product['price'] }}</td>
<td style="text-align: center; padding: 5px 10px;">{{ $product['quantity'] }}</td>
<td style="text-align: right; padding: 5px 10px;">{{ $product['totals'] }}</td>
</tr>
@endforeach
<tr>
<td colspan="2"></td>
<td style="text-align: center; padding: 5px 10px;"><strong>Totals</strong></td>
<td style="text-align: right; padding: 5px 10px;">{{ $total }}</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</div>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question