| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Sfneal\ViewExport\Pdf; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Contracts\View\View; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Sfneal\Actions\AbstractService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Sfneal\ViewExport\Pdf\Utils\Renderer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Sfneal\ViewModels\AbstractViewModel; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  | class PdfExportService extends AbstractService | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     // todo: add ability to pass urls to export | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |      * Provide a view to build the PDF from. | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * @param View $view | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * @param string|null $uploadPath | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * @return Renderer | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public static function fromView(View $view, string $uploadPath = null): Renderer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  |         return new Renderer($view, $uploadPath); | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * Create a view to build the PDF from. | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * @param string $viewName | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @param array $viewData | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @param string|null $uploadPath | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @return Renderer | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     public static function fromViewData(string $viewName, array $viewData = [], string $uploadPath = null): Renderer | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         return new Renderer(view($viewName, $viewData), $uploadPath); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * Provide a view to build the PDF from. | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * @param AbstractViewModel $viewModel | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * @param string|null $uploadPath | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * @return Renderer | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     public static function fromViewModel(AbstractViewModel $viewModel, string $uploadPath = null): Renderer | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         return new Renderer($viewModel->renderNoCache(), $uploadPath); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      * Provide an HTML string to build the PDF from. | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * @param string $html | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * @param string|null $uploadPath | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * @return Renderer | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public static function fromHtml(string $html, string $uploadPath = null): Renderer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |         return new Renderer($html, $uploadPath); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * Provide an HTML path to build the PDF from. | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * @param string $path | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      * @param string|null $uploadPath | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      * @return Renderer | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     public static function fromHtmlFile(string $path, string $uploadPath = null): Renderer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 72 |  |  |         return new Renderer(file_get_contents($path), $uploadPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |  |