Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.

Commit

Permalink
Add PHP Backend For Creating PDFs (Frontend Already Done)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkamara committed Oct 18, 2023
1 parent f849a29 commit 9156f04
Show file tree
Hide file tree
Showing 6 changed files with 406 additions and 14 deletions.
26 changes: 15 additions & 11 deletions app/Http/Controllers/Web/V1/PdfController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
use App\Http\Controllers\Controller;
use App\Http\Requests\StorePdfRequest;
use App\Http\Requests\UpdatePdfRequest;
use App\Http\Resources\V1\PdfResource;
use App\Models\V1\Pdf;
use Illuminate\Http\Request;
use Barryvdh\DomPDF\Facade\Pdf as DomPdf;

class PdfController extends Controller
{
public function __construct()
public function __construct(protected Pdf $pdf = new Pdf())
{
$this->middleware('auth:sanctum');
}
Expand All @@ -33,20 +33,24 @@ public function index(Request $request)
return compact("data");
}

/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}

/**
* Store a newly created resource in storage.
*/
public function store(StorePdfRequest $request)
{
//
$name = htmlspecialchars($request->input("name"));
$birthday = htmlspecialchars($request->input("birthday"));
$this->pdf->users_id = $request->user()->id;
$this->pdf->name = $name;
$this->pdf->birthday = $birthday;
// Set PDF Content.
$domPdf = DomPdf::loadHTML(
"<p>Name: {$this->pdf->name}</p><br/><p>Birthday: {$this->pdf->birthday}</p>"
)->setPaper('a4', 'landscape')->setWarnings(false);
$this->pdf->content = base64_encode($domPdf->output());
$this->pdf->save();
$data = $this->pdf;
return compact("data");
}

/**
Expand Down
13 changes: 11 additions & 2 deletions app/Http/Requests/StorePdfRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class StorePdfRequest extends FormRequest
*/
public function authorize(): bool
{
return false;
return true;
}

/**
Expand All @@ -22,7 +22,16 @@ public function authorize(): bool
public function rules(): array
{
return [
//
"name" => [
"required",
"min:3",
"max:30"
],
"birthday" => [
"required",
"min:3",
"max:30"
]
];
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"license": "MIT",
"require": {
"php": "^8.1",
"barryvdh/laravel-dompdf": "^2.0",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^10.10",
"laravel/sanctum": "^3.2",
Expand Down
Loading

0 comments on commit 9156f04

Please sign in to comment.