2019-09-15 17:33:27 -04:00
|
|
|
<?php
|
|
|
|
|
2023-05-17 12:56:55 -04:00
|
|
|
namespace BookStack\Entities\Controllers;
|
2019-09-15 17:33:27 -04:00
|
|
|
|
2021-06-26 11:23:15 -04:00
|
|
|
use BookStack\Entities\Repos\PageRepo;
|
2020-11-21 19:17:45 -05:00
|
|
|
use BookStack\Entities\Tools\ExportFormatter;
|
2020-11-21 18:20:54 -05:00
|
|
|
use BookStack\Entities\Tools\PageContent;
|
2019-09-15 17:33:27 -04:00
|
|
|
use BookStack\Exceptions\NotFoundException;
|
2023-05-18 15:53:39 -04:00
|
|
|
use BookStack\Http\Controller;
|
2019-09-15 17:33:27 -04:00
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
class PageExportController extends Controller
|
|
|
|
{
|
2019-10-05 07:55:01 -04:00
|
|
|
protected $pageRepo;
|
2020-11-21 20:26:14 -05:00
|
|
|
protected $exportFormatter;
|
2019-09-15 17:33:27 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PageExportController constructor.
|
|
|
|
*/
|
2020-11-21 20:26:14 -05:00
|
|
|
public function __construct(PageRepo $pageRepo, ExportFormatter $exportFormatter)
|
2019-09-15 17:33:27 -04:00
|
|
|
{
|
|
|
|
$this->pageRepo = $pageRepo;
|
2020-11-21 20:26:14 -05:00
|
|
|
$this->exportFormatter = $exportFormatter;
|
2021-08-28 16:48:17 -04:00
|
|
|
$this->middleware('can:content-export');
|
2019-09-15 17:33:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exports a page to a PDF.
|
2021-06-26 11:23:15 -04:00
|
|
|
* https://github.com/barryvdh/laravel-dompdf.
|
|
|
|
*
|
2019-09-15 17:33:27 -04:00
|
|
|
* @throws NotFoundException
|
|
|
|
* @throws Throwable
|
|
|
|
*/
|
|
|
|
public function pdf(string $bookSlug, string $pageSlug)
|
|
|
|
{
|
2019-10-05 07:55:01 -04:00
|
|
|
$page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
|
|
|
|
$page->html = (new PageContent($page))->render();
|
2020-11-21 20:26:14 -05:00
|
|
|
$pdfContent = $this->exportFormatter->pageToPdf($page);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2022-06-08 18:50:42 -04:00
|
|
|
return $this->download()->directly($pdfContent, $pageSlug . '.pdf');
|
2019-09-15 17:33:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export a page to a self-contained HTML file.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2019-09-15 17:33:27 -04:00
|
|
|
* @throws NotFoundException
|
|
|
|
* @throws Throwable
|
|
|
|
*/
|
|
|
|
public function html(string $bookSlug, string $pageSlug)
|
|
|
|
{
|
2019-10-05 07:55:01 -04:00
|
|
|
$page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
|
|
|
|
$page->html = (new PageContent($page))->render();
|
2020-11-21 20:26:14 -05:00
|
|
|
$containedHtml = $this->exportFormatter->pageToContainedHtml($page);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2022-06-08 18:50:42 -04:00
|
|
|
return $this->download()->directly($containedHtml, $pageSlug . '.html');
|
2019-09-15 17:33:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export a page to a simple plaintext .txt file.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2019-09-15 17:33:27 -04:00
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
public function plainText(string $bookSlug, string $pageSlug)
|
|
|
|
{
|
2019-10-05 07:55:01 -04:00
|
|
|
$page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
|
2020-11-21 20:26:14 -05:00
|
|
|
$pageText = $this->exportFormatter->pageToPlainText($page);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2022-06-08 18:50:42 -04:00
|
|
|
return $this->download()->directly($pageText, $pageSlug . '.txt');
|
2019-09-15 17:33:27 -04:00
|
|
|
}
|
2020-05-13 00:12:26 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Export a page to a simple markdown .md file.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2020-05-13 00:12:26 -04:00
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
public function markdown(string $bookSlug, string $pageSlug)
|
|
|
|
{
|
|
|
|
$page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
|
2021-06-22 16:02:18 -04:00
|
|
|
$pageText = $this->exportFormatter->pageToMarkdown($page);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2022-06-08 18:50:42 -04:00
|
|
|
return $this->download()->directly($pageText, $pageSlug . '.md');
|
2020-05-13 00:12:26 -04:00
|
|
|
}
|
2019-09-15 17:33:27 -04:00
|
|
|
}
|