Skip to content

Commit 34ec5c8

Browse files
wip
1 parent c1bfc75 commit 34ec5c8

File tree

2 files changed

+102
-10
lines changed

2 files changed

+102
-10
lines changed

src/Enums/WriterType.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Bernskioldmedia\LaravelPpt\Enums;
4+
5+
enum WriterType: string
6+
{
7+
case PowerPoint2007 = 'PowerPoint2007';
8+
case ODPresentation = 'ODPresentation';
9+
case PDF = 'PDF';
10+
case HTML = 'HTML';
11+
case Serialized = 'Serialized';
12+
13+
/**
14+
* Get the file extension for this writer type.
15+
*/
16+
public function extension(): string
17+
{
18+
return match ($this) {
19+
self::PowerPoint2007 => 'pptx',
20+
self::ODPresentation => 'odp',
21+
self::PDF => 'pdf',
22+
self::HTML => 'html',
23+
self::Serialized => 'phppt',
24+
};
25+
}
26+
}

src/Presentation/Presentation.php

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use BernskioldMedia\LaravelPpt\Concerns\Slides\WithPadding;
88
use BernskioldMedia\LaravelPpt\Concerns\Slides\WithSize;
99
use BernskioldMedia\LaravelPpt\Contracts\CustomizesPowerpointBranding;
10+
use BernskioldMedia\LaravelPpt\Enums\WriterType;
11+
use Closure;
1012
use Illuminate\Contracts\Auth\Authenticatable;
1113
use Illuminate\Support\Facades\Storage;
1214
use Illuminate\Support\Traits\Conditionable;
@@ -16,6 +18,7 @@
1618
use PhpOffice\PhpPresentation\IOFactory;
1719
use PhpOffice\PhpPresentation\PhpPresentation;
1820

21+
use PhpOffice\PhpPresentation\Writer\PDF\DomPDF;
1922
use function config;
2023
use function method_exists;
2124

@@ -45,12 +48,23 @@ class Presentation
4548
*/
4649
public ?Branding $branding = null;
4750

51+
/**
52+
* The writer type to use when saving the presentation.
53+
*/
54+
protected WriterType $writerType = WriterType::PowerPoint2007;
55+
56+
/**
57+
* Custom configuration callback for the writer.
58+
*/
59+
protected ?Closure $writerCallback = null;
60+
4861
public function __construct(
4962
protected string $title,
50-
?int $width = null,
51-
?int $height = null,
52-
?string $branding = null
53-
) {
63+
?int $width = null,
64+
?int $height = null,
65+
?string $branding = null
66+
)
67+
{
5468
$this->user = auth()->user();
5569

5670
// Set default sizes.
@@ -112,22 +126,37 @@ public function create(): static
112126
*/
113127
public function save(string $filename, ?string $disk = null, bool $inRootFolder = false): string
114128
{
115-
if (! $disk) {
129+
if (!$disk) {
116130
$disk = config('powerpoint.output.disk', 'local');
117131
}
118132

119-
if (! $inRootFolder) {
133+
// Get the file extension based on the writer type
134+
$extension = $this->writerType->extension();
135+
136+
if (!$inRootFolder) {
120137
$directory = config('powerpoint.output.directory', 'ppt');
121138
Storage::disk($disk)->makeDirectory($directory);
122139

123-
$path = "$directory/$filename.pptx";
140+
$path = "$directory/$filename.$extension";
124141
} else {
125-
$path = "$filename.pptx";
142+
$path = "$filename.$extension";
126143
}
127144

128145
$path = Storage::disk($disk)->path($path);
129146

130-
$writer = IOFactory::createWriter($this->document);
147+
// Create the writer with the specified type
148+
$writer = IOFactory::createWriter($this->document, $this->writerType->value);
149+
150+
// Auto-configure PDF writer with DomPDF adapter
151+
if ($this->writerType === WriterType::PDF && empty($this->writerCallback)) {
152+
$writer->setPDFAdapter(new DomPDF());
153+
}
154+
155+
// Apply custom writer configuration if provided
156+
if ($this->writerCallback) {
157+
($this->writerCallback)($writer);
158+
}
159+
131160
$writer->save($path);
132161

133162
return $path;
@@ -136,7 +165,7 @@ public function save(string $filename, ?string $disk = null, bool $inRootFolder
136165
/**
137166
* The slides to add to the presentation.
138167
*
139-
* @param array<BaseSlide> $slides
168+
* @param array<BaseSlide> $slides
140169
*/
141170
public function slides(array $slides = []): static
142171
{
@@ -181,6 +210,43 @@ public function forUser(Authenticatable $user): static
181210
return $this;
182211
}
183212

213+
/**
214+
* Set the writer type to use when saving the presentation.
215+
*/
216+
public function writer(WriterType $type): static
217+
{
218+
$this->writerType = $type;
219+
220+
return $this;
221+
}
222+
223+
public function asPowerPoint(): static
224+
{
225+
return $this->writer(WriterType::PowerPoint2007);
226+
}
227+
228+
public function asPdf(): static
229+
{
230+
return $this->writer(WriterType::PDF);
231+
}
232+
233+
public function asHtml(): static
234+
{
235+
return $this->writer(WriterType::HTML);
236+
}
237+
238+
/**
239+
* Configure the writer with a custom callback.
240+
* The callback receives the writer instance and can be used to set
241+
* writer-specific options like PDF adapters or ZIP adapters.
242+
*/
243+
public function configureWriter(callable $callback): static
244+
{
245+
$this->writerCallback = $callback;
246+
247+
return $this;
248+
}
249+
184250
/**
185251
* Set various document properties to the document.
186252
*/

0 commit comments

Comments
 (0)