1 <?php namespace BookStack\Entities\Tools;
3 use BookStack\Entities\Models\Book;
4 use BookStack\Entities\Models\Chapter;
5 use BookStack\Entities\Models\Page;
6 use BookStack\Uploads\ImageService;
10 use League\HTMLToMarkdown\HtmlConverter;
17 protected $imageService;
20 * ExportService constructor.
22 public function __construct(ImageService $imageService)
24 $this->imageService = $imageService;
28 * Convert a page to a self-contained HTML file.
29 * Includes required CSS & image content. Images are base64 encoded into the HTML.
32 public function pageToContainedHtml(Page $page)
34 $page->html = (new PageContent($page))->render();
35 $pageHtml = view('pages.export', [
39 return $this->containHtml($pageHtml);
43 * Convert a chapter to a self-contained HTML file.
46 public function chapterToContainedHtml(Chapter $chapter)
48 $pages = $chapter->getVisiblePages();
49 $pages->each(function ($page) {
50 $page->html = (new PageContent($page))->render();
52 $html = view('chapters.export', [
53 'chapter' => $chapter,
57 return $this->containHtml($html);
61 * Convert a book to a self-contained HTML file.
64 public function bookToContainedHtml(Book $book)
66 $bookTree = (new BookContents($book))->getTree(false, true);
67 $html = view('books.export', [
69 'bookChildren' => $bookTree,
72 return $this->containHtml($html);
76 * Convert a page to a PDF file.
79 public function pageToPdf(Page $page)
81 $page->html = (new PageContent($page))->render();
82 $html = view('pages.export', [
86 return $this->htmlToPdf($html);
90 * Convert a chapter to a PDF file.
93 public function chapterToPdf(Chapter $chapter)
95 $pages = $chapter->getVisiblePages();
96 $pages->each(function ($page) {
97 $page->html = (new PageContent($page))->render();
100 $html = view('chapters.export', [
101 'chapter' => $chapter,
106 return $this->htmlToPdf($html);
110 * Convert a book to a PDF file.
113 public function bookToPdf(Book $book)
115 $bookTree = (new BookContents($book))->getTree(false, true);
116 $html = view('books.export', [
118 'bookChildren' => $bookTree,
121 return $this->htmlToPdf($html);
125 * Convert normal web-page HTML to a PDF.
128 protected function htmlToPdf(string $html): string
130 $containedHtml = $this->containHtml($html);
131 $useWKHTML = config('snappy.pdf.binary') !== false;
133 $pdf = SnappyPDF::loadHTML($containedHtml);
134 $pdf->setOption('print-media-type', true);
136 $pdf = DomPDF::loadHTML($containedHtml);
138 return $pdf->output();
142 * Bundle of the contents of a html file to be self-contained.
145 protected function containHtml(string $htmlContent): string
147 $imageTagsOutput = [];
148 preg_match_all("/\<img.*?src\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $imageTagsOutput);
150 // Replace image src with base64 encoded image strings
151 if (isset($imageTagsOutput[0]) && count($imageTagsOutput[0]) > 0) {
152 foreach ($imageTagsOutput[0] as $index => $imgMatch) {
153 $oldImgTagString = $imgMatch;
154 $srcString = $imageTagsOutput[2][$index];
155 $imageEncoded = $this->imageService->imageUriToBase64($srcString);
156 if ($imageEncoded === null) {
157 $imageEncoded = $srcString;
159 $newImgTagString = str_replace($srcString, $imageEncoded, $oldImgTagString);
160 $htmlContent = str_replace($oldImgTagString, $newImgTagString, $htmlContent);
165 preg_match_all("/\<a.*href\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $linksOutput);
167 // Replace image src with base64 encoded image strings
168 if (isset($linksOutput[0]) && count($linksOutput[0]) > 0) {
169 foreach ($linksOutput[0] as $index => $linkMatch) {
170 $oldLinkString = $linkMatch;
171 $srcString = $linksOutput[2][$index];
172 if (strpos(trim($srcString), 'http') !== 0) {
173 $newSrcString = url($srcString);
174 $newLinkString = str_replace($srcString, $newSrcString, $oldLinkString);
175 $htmlContent = str_replace($oldLinkString, $newLinkString, $htmlContent);
180 // Replace any relative links with system domain
185 * Converts the page contents into simple plain text.
186 * This method filters any bad looking content to provide a nice final output.
188 public function pageToPlainText(Page $page): string
190 $html = (new PageContent($page))->render();
191 $text = strip_tags($html);
192 // Replace multiple spaces with single spaces
193 $text = preg_replace('/\ {2,}/', ' ', $text);
194 // Reduce multiple horrid whitespace characters.
195 $text = preg_replace('/(\x0A|\xA0|\x0A|\r|\n){2,}/su', "\n\n", $text);
196 $text = html_entity_decode($text);
198 $text = $page->name . "\n\n" . $text;
203 * Convert a chapter into a plain text string.
205 public function chapterToPlainText(Chapter $chapter): string
207 $text = $chapter->name . "\n\n";
208 $text .= $chapter->description . "\n\n";
209 foreach ($chapter->getVisiblePages() as $page) {
210 $text .= $this->pageToPlainText($page);
216 * Convert a book into a plain text string.
218 public function bookToPlainText(Book $book): string
220 $bookTree = (new BookContents($book))->getTree(false, false);
221 $text = $book->name . "\n\n";
222 foreach ($bookTree as $bookChild) {
223 if ($bookChild->isA('chapter')) {
224 $text .= $this->chapterToPlainText($bookChild);
226 $text .= $this->pageToPlainText($bookChild);
233 * Convert a page to a Markdown file.
236 public function pageToMarkdown(Page $page)
238 if (property_exists($page, 'markdown') && $page->markdown != '') {
239 return "# " . $page->name . "\n\n" . $page->markdown;
241 $converter = new HtmlConverter();
242 return "# " . $page->name . "\n\n" . $converter->convert($page->html);
247 * Convert a chapter to a Markdown file.
250 public function chapterToMarkdown(Chapter $chapter)
252 $text = "# " . $chapter->name . "\n\n";
253 $text .= $chapter->description . "\n\n";
254 foreach ($chapter->pages as $page) {
255 $text .= $this->pageToMarkdown($page);
261 * Convert a book into a plain text string.
263 public function bookToMarkdown(Book $book): string
265 $bookTree = (new BookContents($book))->getTree(false, true);
266 $text = "# " . $book->name . "\n\n";
267 foreach ($bookTree as $bookChild) {
268 if ($bookChild->isA('chapter')) {
269 $text .= $this->chapterToMarkdown($bookChild);
271 $text .= $this->pageToMarkdown($bookChild);
278 * Convert a book into a zip file.
280 public function bookToZip(Book $book): string
282 // TODO: Is not unlinking the file a security risk?
283 $z = new ZipArchive();
284 $z->open("book.zip", \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
285 $bookTree = (new BookContents($book))->getTree(false, true);
286 foreach ($bookTree as $bookChild) {
287 if ($bookChild->isA('chapter')) {
288 $z->addEmptyDir($bookChild->name);
289 foreach ($bookChild->pages as $page) {
290 $filename = $bookChild->name . "/" . $page->name . ".md";
291 $z->addFromString($filename, $this->pageToMarkdown($page));
294 $z->addFromString($bookChild->name . ".md", $this->pageToMarkdown($bookChild));