2019-09-16 05:33:27 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Http\Controllers;
|
|
|
|
|
2021-06-26 23:23:15 +08:00
|
|
|
use BookStack\Entities\Repos\PageRepo;
|
2020-11-22 08:17:45 +08:00
|
|
|
use BookStack\Entities\Tools\ExportFormatter;
|
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;
|
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
class PageExportController extends Controller
|
|
|
|
{
|
2019-10-05 19:55:01 +08:00
|
|
|
protected $pageRepo;
|
2020-11-22 09:26:14 +08:00
|
|
|
protected $exportFormatter;
|
2019-09-16 05:33:27 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PageExportController constructor.
|
|
|
|
*/
|
2020-11-22 09:26:14 +08:00
|
|
|
public function __construct(PageRepo $pageRepo, ExportFormatter $exportFormatter)
|
2019-09-16 05:33:27 +08:00
|
|
|
{
|
|
|
|
$this->pageRepo = $pageRepo;
|
2020-11-22 09:26:14 +08:00
|
|
|
$this->exportFormatter = $exportFormatter;
|
2021-08-29 04:48:17 +08:00
|
|
|
$this->middleware('can:content-export');
|
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)
|
|
|
|
{
|
2019-10-05 19:55:01 +08:00
|
|
|
$page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
|
|
|
|
$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
|
|
|
|
2019-09-16 05:33:27 +08:00
|
|
|
return $this->downloadResponse($pdfContent, $pageSlug . '.pdf');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2019-10-05 19:55:01 +08:00
|
|
|
$page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
|
|
|
|
$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
|
|
|
|
2019-09-16 05:33:27 +08:00
|
|
|
return $this->downloadResponse($containedHtml, $pageSlug . '.html');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2019-10-05 19:55:01 +08:00
|
|
|
$page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
|
2020-11-22 09:26:14 +08:00
|
|
|
$pageText = $this->exportFormatter->pageToPlainText($page);
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2019-09-16 05:33:27 +08:00
|
|
|
return $this->downloadResponse($pageText, $pageSlug . '.txt');
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
$page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
|
2021-06-23 04:02:18 +08:00
|
|
|
$pageText = $this->exportFormatter->pageToMarkdown($page);
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2020-05-13 12:12:26 +08:00
|
|
|
return $this->downloadResponse($pageText, $pageSlug . '.md');
|
|
|
|
}
|
2019-09-16 05:33:27 +08:00
|
|
|
}
|