2019-10-05 07:55:01 -04:00
|
|
|
<?php namespace BookStack\Http\Controllers;
|
2019-09-15 17:33:27 -04:00
|
|
|
|
2020-11-21 19:17:45 -05:00
|
|
|
use BookStack\Entities\Tools\ExportFormatter;
|
2019-10-05 07:55:01 -04:00
|
|
|
use BookStack\Entities\Repos\ChapterRepo;
|
2019-09-15 17:33:27 -04:00
|
|
|
use BookStack\Exceptions\NotFoundException;
|
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
class ChapterExportController extends Controller
|
|
|
|
{
|
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
protected $chapterRepo;
|
2019-09-15 17:33:27 -04:00
|
|
|
protected $exportService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ChapterExportController constructor.
|
|
|
|
*/
|
2020-11-21 19:17:45 -05:00
|
|
|
public function __construct(ChapterRepo $chapterRepo, ExportFormatter $exportService)
|
2019-09-15 17:33:27 -04:00
|
|
|
{
|
2019-10-05 07:55:01 -04:00
|
|
|
$this->chapterRepo = $chapterRepo;
|
2019-09-15 17:33:27 -04:00
|
|
|
$this->exportService = $exportService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-05 07:55:01 -04:00
|
|
|
* Exports a chapter to pdf.
|
2019-09-15 17:33:27 -04:00
|
|
|
* @throws NotFoundException
|
|
|
|
* @throws Throwable
|
|
|
|
*/
|
|
|
|
public function pdf(string $bookSlug, string $chapterSlug)
|
|
|
|
{
|
2019-10-05 07:55:01 -04:00
|
|
|
$chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
|
2019-09-15 17:33:27 -04:00
|
|
|
$pdfContent = $this->exportService->chapterToPdf($chapter);
|
|
|
|
return $this->downloadResponse($pdfContent, $chapterSlug . '.pdf');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export a chapter to a self-contained HTML file.
|
|
|
|
* @throws NotFoundException
|
|
|
|
* @throws Throwable
|
|
|
|
*/
|
|
|
|
public function html(string $bookSlug, string $chapterSlug)
|
|
|
|
{
|
2019-10-05 07:55:01 -04:00
|
|
|
$chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
|
2019-09-15 17:33:27 -04:00
|
|
|
$containedHtml = $this->exportService->chapterToContainedHtml($chapter);
|
|
|
|
return $this->downloadResponse($containedHtml, $chapterSlug . '.html');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export a chapter to a simple plaintext .txt file.
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
public function plainText(string $bookSlug, string $chapterSlug)
|
|
|
|
{
|
2019-10-05 07:55:01 -04:00
|
|
|
$chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
|
2019-09-15 17:33:27 -04:00
|
|
|
$chapterText = $this->exportService->chapterToPlainText($chapter);
|
|
|
|
return $this->downloadResponse($chapterText, $chapterSlug . '.txt');
|
|
|
|
}
|
|
|
|
}
|