Created
March 20, 2021 06:29
-
-
Save webdevmatics/17912d8ca21b2a0eb748f409738500be to your computer and use it in GitHub Desktop.
Livewire images to pdf converter
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
<div class="m-5"> | |
<div> | |
@if (session()->has('message')) | |
<div class="alert alert-success"> | |
{{ session('message') }} | |
</div> | |
@endif | |
</div> | |
<div wire:loading> | |
Processing Please Wait... | |
</div> | |
<form wire:submit.prevent="save"> | |
<input type="file" wire:model="photos" multiple> | |
<br> | |
@error('photos.*') <span class="text-red-400">{{ $message }}</span> @enderror | |
<br> | |
@if(count($photos)) | |
<button class="bg-green-500 p-2 mt-2" type="submit">Generate PDF</button> | |
@endif | |
</form> | |
</div> |
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\Livewire; | |
use PDF; | |
use Livewire\Component; | |
use Livewire\WithFileUploads; | |
use Illuminate\Support\Facades\Storage; | |
class ImageUpload extends Component | |
{ | |
use WithFileUploads; | |
public $photos=[]; | |
public function render() | |
{ | |
return view('livewire.image-upload'); | |
} | |
public function save() | |
{ | |
$this->validate([ | |
'photos.*' => 'required|image', // 1MB Max | |
]); | |
$folderName = uniqid(); | |
foreach ($this->photos as $photo) { | |
$photo->storeAs($folderName, $photo->getClientOriginalName(), 'public'); | |
} | |
$images = Storage::disk('public')->files($folderName); | |
$outputFileName = 'storage/'.$folderName.'/final.pdf'; | |
PDF::loadView('final-pdf',['images' => $images])->save($outputFileName); | |
return response()->download($outputFileName); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment