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