Upload PDF file to Laravel DB?

  • 0
    Hello coders, please tell me, I have a form, after pressing the button in the controller, a PDF file is generated (so to speak, an invoice). and this file immediately opens in a new tab. Used this technology
    If you find that the library is not suitable then you can use this
    What I need, I need to implement so that this file is saved in the database, and after that I could, on a separate page, display the file id, username, and download buttons in the column, and view the file in the browser (as it happens in this case when I click on the button on the form).

    Here's the controller I'm handling:
    <?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();
        }
    }

    Here is the migration.
    <?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');
        }
    }

    Here is the invoice file
    <!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>
    Laravel Iris Craig, May 17, 2019

  • 1 Answers
  • 0
    You don't need to save the pdf to the DB. It does not make sense. It is enough to store the data from which it is generated and then do it on the fly. As a last resort, if 100500 downloads are expected, you can save it to storage. Surely your library allows it.
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!