| 
									
										
										
										
											2021-11-25 23:12:32 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Entities\Tools; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-17 21:01:59 +08:00
										 |  |  | use Barryvdh\DomPDF\Facade\Pdf as DomPDF; | 
					
						
							| 
									
										
										
										
											2021-11-29 05:01:35 +08:00
										 |  |  | use Barryvdh\Snappy\Facades\SnappyPdf; | 
					
						
							| 
									
										
										
										
											2021-11-25 23:12:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class PdfGenerator | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-01-25 01:24:00 +08:00
										 |  |  |     const ENGINE_DOMPDF = 'dompdf'; | 
					
						
							|  |  |  |     const ENGINE_WKHTML = 'wkhtml'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 23:12:32 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Generate PDF content from the given HTML content. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function fromHtml(string $html): string | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-01-25 01:24:00 +08:00
										 |  |  |         if ($this->getActiveEngine() === self::ENGINE_WKHTML) { | 
					
						
							| 
									
										
										
										
											2021-11-25 23:12:32 +08:00
										 |  |  |             $pdf = SnappyPDF::loadHTML($html); | 
					
						
							|  |  |  |             $pdf->setOption('print-media-type', true); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $pdf = DomPDF::loadHTML($html); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $pdf->output(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-25 01:24:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get the currently active PDF engine. | 
					
						
							|  |  |  |      * Returns the value of an `ENGINE_` const on this class. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getActiveEngine(): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $useWKHTML = config('snappy.pdf.binary') !== false && config('app.allow_untrusted_server_fetching') === true; | 
					
						
							| 
									
										
										
										
											2022-01-25 04:55:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-25 01:24:00 +08:00
										 |  |  |         return $useWKHTML ? self::ENGINE_WKHTML : self::ENGINE_DOMPDF; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-29 05:01:35 +08:00
										 |  |  | } |