77use BernskioldMedia \LaravelPpt \Concerns \Slides \WithPadding ;
88use BernskioldMedia \LaravelPpt \Concerns \Slides \WithSize ;
99use BernskioldMedia \LaravelPpt \Contracts \CustomizesPowerpointBranding ;
10+ use BernskioldMedia \LaravelPpt \Enums \WriterType ;
11+ use Closure ;
1012use Illuminate \Contracts \Auth \Authenticatable ;
1113use Illuminate \Support \Facades \Storage ;
1214use Illuminate \Support \Traits \Conditionable ;
1618use PhpOffice \PhpPresentation \IOFactory ;
1719use PhpOffice \PhpPresentation \PhpPresentation ;
1820
21+ use PhpOffice \PhpPresentation \Writer \PDF \DomPDF ;
1922use function config ;
2023use 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