Ad

Open Pdf View On Click Of Link Of A Html Page In Laravel

- 1 answer

PDF generated but it just downloaded, not opened in a tab.
I want to open a random generated pdf page in a browser from where the user can download it.

I have tried pdf generating code? This is my controller.
Please suggest some code here in show function.

<?php

namespace App\Http\Controllers;

use App\Item;
use DB;
use Excel;
use PDF;
use Illuminate\Http\Request;
//use App\Mail\DemoEmail;
use Illuminate\Support\Facades\Mail;

class MaatwebsiteDemoController extends Controller
{

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function importExport()
    {
        return view('importExport');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function downloadExcel($type)
    {
        $data = Item::get()->toArray();

        return Excel::create('itsolutionstuff_example', function ($excel) use ($data) {
            $excel->sheet('mySheet', function ($sheet) use ($data) {
                $sheet->fromArray($data);
            });
        })->download($type);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function importExcel(Request $request)
    {
        $request->validate([
            'import_file' => 'required'
        ]);

        $path = $request->file('import_file')->getRealPath();
        $data = Excel::load($path)->get();
        //echo "<pre>";
        //print_r($data);exit;

        if ($data->count()) {
            foreach ($data as $key => $value) {
                foreach ($value as $key1 => $x1) {
                    $arr[] = ['email' => $x1->email, 'description' => $x1->description, 'content' => $x1->content];
                }
            }
            if (!empty($arr)) {
                Item::insert($arr);
            }
            //$data1 = Item::get()->toArray();
            $users = Item::get()->toArray();

            foreach ($users as $keycc => $user) {
                $data2[] = $user['email'];
                Mail::send('mails.demo', $user, function ($message) use ($user) {
                    $message->to($user['email'], 'Tutorials Point')->subject('Laravel HTML Testing Mail');
                    $message->from('[email protected]', 'Smita');
                });
            }


            // Mail::send('emails.contact', $data, function($message) use ($data){
            // return back()->with('success', 'Insert Record successfully.');

        }
    }
    //Here i am loading view as pdf bt it not open in a new tab as pdf it just dowloaded.
    public function show($id)
    {
        $users = DB::select('select * from items where id = ?', [$id]);
        //view()->share('users',$users);

        $pdf = PDF::loadView('pdfview', array('users' => $users));

        return $pdf->download('demo.pdf');
    }

    public function downloadPDF()

    {
        $pdf = PDF::loadView('pdfview');
        return $pdf->download('invoice.pdf');
    }
}

This is my view having a link.when we click on this link page open as pdf in browser.

<a target="_blank" rel="nofollow noreferrer" href="{{ route('pdfview', $id) }}">Content Detail Displayed here</a>
Ad

Answer

From the readme of barryvdh/laravel-dompdf

 Using

You can create a new DOMPDF instance and load a HTML string, file or view name. You can save it to a file, or stream (show in browser) or download.

$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML('<h1>Test</h1>');
return $pdf->stream();

The download function naturally downloads the file, use stream() instead

public function show($id)
{
  $users = DB::select('select * from items where id = ?', [$id]);
  $pdf = PDF::loadView('pdfview', array('users' => $users));
  return $pdf->stream();
}

I hope this helps

Ad
source: stackoverflow.com
Ad