fix: Use View::file() to render sticker template directly, add missing test route, and allow GET for regenerate

- Changed from View::make() to View::file() with full path resolution
- Added file existence check with detailed error logging
- Allow GET/POST for regenerate sticker endpoint for easier testing
- Add design name and customer surname to A6 PDF stickers
- Add regenerate sticker endpoint POST /test/sticker/{orderNumber}/regenerate
- Add test sticker preview page at /test/sticker/{orderNumber}
This commit is contained in:
twotalesanimation
2026-01-02 20:25:02 +02:00
parent e503cd3479
commit 12f42d5efc
4 changed files with 201 additions and 12 deletions
+52 -11
View File
@@ -8,6 +8,7 @@ use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\View;
class QrStickerService
{
@@ -99,28 +100,68 @@ class QrStickerService
? 'STOCK ORDER'
: 'CUSTOM ORDER';
// Extract design name from first order item
$designName = '';
if ($order->items && $order->items->count() > 0) {
$firstItem = $order->items->first();
$designName = $firstItem->product?->name ?? $firstItem->name ?? 'Custom Design';
}
// Extract customer surname (last word of name)
$customerName = $order->user?->name ?? '';
$nameParts = explode(' ', trim($customerName));
$customerSurname = end($nameParts);
// Prepare data for the view
$data = [
'order' => $order,
'qrSvg' => $qrSvg,
'orderType' => $orderType,
'designName' => $designName,
'customerSurname' => $customerSurname,
'qrUrl' => $qrUrl,
'generatedAt' => now()->format('Y-m-d H:i'),
];
// Generate PDF using the sticker template
$pdf = Pdf::loadView('stickers.qr-sticker', $data);
// Render the view to HTML first
try {
$viewPath = resource_path('views/stickers/qr-sticker.blade.php');
if (!file_exists($viewPath)) {
throw new \Exception("View file not found at: {$viewPath}");
}
// Compile and render the view
$html = View::file($viewPath, $data)->render();
} catch (\Exception $e) {
Log::error('Failed to render sticker view', [
'order_uuid' => $order->uuid,
'error' => $e->getMessage(),
'view_path' => $viewPath ?? 'unknown',
]);
throw $e;
}
// Configure for A6 landscape
// A6 is 105mm × 148mm (landscape: 148mm × 105mm)
$pdf->setPaper([0, 0, 420.94, 297.64], 'portrait'); // 148mm × 105mm in points (1mm ≈ 2.834645669 points)
// Generate PDF from HTML using DomPDF
try {
$pdf = Pdf::loadHTML($html);
// Configure for A6 landscape
// A6 is 105mm × 148mm (landscape: 148mm × 105mm)
$pdf->setPaper([0, 0, 420.94, 297.64], 'portrait'); // 148mm × 105mm in points (1mm ≈ 2.834645669 points)
// Store PDF
$filename = "{$order->uuid}.pdf";
$path = "qr-stickers/{$filename}";
$pdfContent = $pdf->output();
Storage::disk('public')->put($path, $pdfContent);
// Store PDF
$filename = "{$order->uuid}.pdf";
$path = "qr-stickers/{$filename}";
$pdfContent = $pdf->output();
Storage::disk('public')->put($path, $pdfContent);
return $path;
return $path;
} catch (\Exception $e) {
Log::error('DomPDF generation failed', [
'order_uuid' => $order->uuid,
'error' => $e->getMessage(),
]);
throw $e;
}
}
}