| 
									
										
										
										
											2019-09-16 05:33:27 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-15 23:14:11 +08:00
										 |  |  | namespace BookStack\Exports\Controllers; | 
					
						
							| 
									
										
										
										
											2019-09-16 05:33:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-06 01:35:49 +08:00
										 |  |  | use BookStack\Entities\Queries\PageQueries; | 
					
						
							| 
									
										
										
										
											2020-11-22 07:20:54 +08:00
										 |  |  | use BookStack\Entities\Tools\PageContent; | 
					
						
							| 
									
										
										
										
											2019-09-16 05:33:27 +08:00
										 |  |  | use BookStack\Exceptions\NotFoundException; | 
					
						
							| 
									
										
										
										
											2024-10-15 23:14:11 +08:00
										 |  |  | use BookStack\Exports\ExportFormatter; | 
					
						
							| 
									
										
										
										
											2024-10-23 17:48:26 +08:00
										 |  |  | use BookStack\Exports\ZipExports\ZipExportBuilder; | 
					
						
							| 
									
										
										
										
											2023-05-19 03:53:39 +08:00
										 |  |  | use BookStack\Http\Controller; | 
					
						
							| 
									
										
										
										
											2019-09-16 05:33:27 +08:00
										 |  |  | use Throwable; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class PageExportController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-02-06 01:35:49 +08:00
										 |  |  |     public function __construct( | 
					
						
							|  |  |  |         protected PageQueries $queries, | 
					
						
							|  |  |  |         protected ExportFormatter $exportFormatter, | 
					
						
							|  |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2021-08-29 04:48:17 +08:00
										 |  |  |         $this->middleware('can:content-export'); | 
					
						
							| 
									
										
										
										
											2025-01-01 23:42:59 +08:00
										 |  |  |         $this->middleware('throttle:exports'); | 
					
						
							| 
									
										
										
										
											2019-09-16 05:33:27 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Exports a page to a PDF. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * https://github.com/barryvdh/laravel-dompdf. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-09-16 05:33:27 +08:00
										 |  |  |      * @throws NotFoundException | 
					
						
							|  |  |  |      * @throws Throwable | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function pdf(string $bookSlug, string $pageSlug) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-02-06 01:35:49 +08:00
										 |  |  |         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug); | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         $page->html = (new PageContent($page))->render(); | 
					
						
							| 
									
										
										
										
											2020-11-22 09:26:14 +08:00
										 |  |  |         $pdfContent = $this->exportFormatter->pageToPdf($page); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-09 06:50:42 +08:00
										 |  |  |         return $this->download()->directly($pdfContent, $pageSlug . '.pdf'); | 
					
						
							| 
									
										
										
										
											2019-09-16 05:33:27 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Export a page to a self-contained HTML file. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-09-16 05:33:27 +08:00
										 |  |  |      * @throws NotFoundException | 
					
						
							|  |  |  |      * @throws Throwable | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function html(string $bookSlug, string $pageSlug) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-02-06 01:35:49 +08:00
										 |  |  |         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug); | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         $page->html = (new PageContent($page))->render(); | 
					
						
							| 
									
										
										
										
											2020-11-22 09:26:14 +08:00
										 |  |  |         $containedHtml = $this->exportFormatter->pageToContainedHtml($page); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-09 06:50:42 +08:00
										 |  |  |         return $this->download()->directly($containedHtml, $pageSlug . '.html'); | 
					
						
							| 
									
										
										
										
											2019-09-16 05:33:27 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Export a page to a simple plaintext .txt file. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-09-16 05:33:27 +08:00
										 |  |  |      * @throws NotFoundException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function plainText(string $bookSlug, string $pageSlug) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-02-06 01:35:49 +08:00
										 |  |  |         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug); | 
					
						
							| 
									
										
										
										
											2020-11-22 09:26:14 +08:00
										 |  |  |         $pageText = $this->exportFormatter->pageToPlainText($page); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-09 06:50:42 +08:00
										 |  |  |         return $this->download()->directly($pageText, $pageSlug . '.txt'); | 
					
						
							| 
									
										
										
										
											2019-09-16 05:33:27 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-05-13 12:12:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Export a page to a simple markdown .md file. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-05-13 12:12:26 +08:00
										 |  |  |      * @throws NotFoundException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function markdown(string $bookSlug, string $pageSlug) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-02-06 01:35:49 +08:00
										 |  |  |         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug); | 
					
						
							| 
									
										
										
										
											2021-06-23 04:02:18 +08:00
										 |  |  |         $pageText = $this->exportFormatter->pageToMarkdown($page); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-09 06:50:42 +08:00
										 |  |  |         return $this->download()->directly($pageText, $pageSlug . '.md'); | 
					
						
							| 
									
										
										
										
											2020-05-13 12:12:26 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-10-19 22:41:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Export a page to a contained ZIP export file. | 
					
						
							|  |  |  |      * @throws NotFoundException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function zip(string $bookSlug, string $pageSlug, ZipExportBuilder $builder) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug); | 
					
						
							|  |  |  |         $zip = $builder->buildForPage($page); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-31 23:13:50 +08:00
										 |  |  |         return $this->download()->streamedFileDirectly($zip, $pageSlug . '.zip', filesize($zip), true); | 
					
						
							| 
									
										
										
										
											2024-10-19 22:41:07 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-09-16 05:33:27 +08:00
										 |  |  | } |