]> BookStack Code Mirror - bookstack/blob - app/Exports/Controllers/PageExportApiController.php
Implement functionality to export a book, along with its pages and chapters, as a...
[bookstack] / app / Exports / Controllers / PageExportApiController.php
1 <?php
2
3 namespace BookStack\Exports\Controllers;
4
5 use BookStack\Entities\Queries\PageQueries;
6 use BookStack\Exports\ExportFormatter;
7 use BookStack\Exports\ZipExports\ZipExportBuilder;
8 use BookStack\Http\ApiController;
9 use Throwable;
10
11 class PageExportApiController extends ApiController
12 {
13     public function __construct(
14         protected ExportFormatter $exportFormatter,
15         protected PageQueries $queries,
16     ) {
17         $this->middleware('can:content-export');
18     }
19
20     /**
21      * Export a page as a PDF file.
22      *
23      * @throws Throwable
24      */
25     public function exportPdf(int $id)
26     {
27         $page = $this->queries->findVisibleByIdOrFail($id);
28         $pdfContent = $this->exportFormatter->pageToPdf($page);
29
30         return $this->download()->directly($pdfContent, $page->slug . '.pdf');
31     }
32
33     /**
34      * Export a page as a contained HTML file.
35      *
36      * @throws Throwable
37      */
38     public function exportHtml(int $id)
39     {
40         $page = $this->queries->findVisibleByIdOrFail($id);
41         $htmlContent = $this->exportFormatter->pageToContainedHtml($page);
42
43         return $this->download()->directly($htmlContent, $page->slug . '.html');
44     }
45
46     /**
47      * Export a page as a plain text file.
48      */
49     public function exportPlainText(int $id)
50     {
51         $page = $this->queries->findVisibleByIdOrFail($id);
52         $textContent = $this->exportFormatter->pageToPlainText($page);
53
54         return $this->download()->directly($textContent, $page->slug . '.txt');
55     }
56
57     /**
58      * Export a page as a markdown file.
59      */
60     public function exportMarkdown(int $id)
61     {
62         $page = $this->queries->findVisibleByIdOrFail($id);
63         $markdown = $this->exportFormatter->pageToMarkdown($page);
64
65         return $this->download()->directly($markdown, $page->slug . '.md');
66     }
67
68
69
70     public function exportZip(int $id, ZipExportBuilder $builder)
71     {
72         $page = $this->queries->findVisibleByIdOrFail($id);
73         $pageSlug = $page->slug;
74         $zip = $builder->buildForPage($page);
75
76         return $this->download()->streamedFileDirectly($zip, $pageSlug . '.zip', filesize($zip), true);
77     }
78 }