Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions app/Conversion/Drivers/Image/PngToPdfDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace App\Conversion\Drivers\Image;

use App\Support\Conversions\Contracts\ConverterDriver;
use App\Support\Conversions\DTO\ConversionContext;
use App\Support\Conversions\DTO\ConversionResult;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Support\Facades\Storage;

final class PngToPdfDriver implements ConverterDriver
{
public function key(): string
{
return 'png_to_pdf';
}

public function convert(ConversionContext $context): ConversionResult
{
$sourceContent = Storage::disk('local')->get($context->sourceFile->stored_path);
$imageDataUri = 'data:image/png;base64,'.base64_encode($sourceContent);

$pageSize = $context->options['page_size'] ?? 'a4';
$orientation = $this->resolveOrientation($context->options['orientation'] ?? 'auto', $sourceContent);
$margin = $this->resolveMargin($context->options['margin'] ?? 'small');
$fitMode = $context->options['fit_mode'] ?? 'contain';

$html = view('pdf.single-image', compact('imageDataUri', 'margin', 'fitMode'))->render();

$pdf = Pdf::loadHTML($html)->setPaper($pageSize, $orientation);

$outputPath = $context->outputDirectory.'/result.pdf';
Storage::disk('local')->put($outputPath, $pdf->output());

return new ConversionResult(
path: $outputPath,
originalName: 'result.pdf',
mimeType: 'application/pdf',
extension: 'pdf',
sizeBytes: Storage::disk('local')->size($outputPath),
);
}

private function resolveOrientation(string $orientation, string $imageContent): string
{
if ($orientation !== 'auto') {
return $orientation;
}

$image = imagecreatefromstring($imageContent);
if ($image === false) {
return 'portrait';
}

$width = imagesx($image);
$height = imagesy($image);
imagedestroy($image);

return $width > $height ? 'landscape' : 'portrait';
}

private function resolveMargin(string $margin): string
{
return match ($margin) {
'none' => '0mm',
'small' => '10mm',
'medium' => '20mm',
'large' => '30mm',
default => '10mm',
};
}
}
37 changes: 37 additions & 0 deletions resources/views/pdf/single-image.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { margin: {{ $margin }}; }
.image-container {
width: 100%;
height: 100%;
@if ($fitMode === 'contain')
display: flex;
align-items: center;
justify-content: center;
@endif
}
img {
@if ($fitMode === 'contain')
max-width: 100%;
max-height: 100%;
object-fit: contain;
@elseif ($fitMode === 'fill')
width: 100%;
height: 100%;
object-fit: fill;
@else
display: block;
@endif
}
</style>
</head>
<body>
<div class="image-container">
<img src="{{ $imageDataUri }}" alt="">
</div>
</body>
</html>