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