1 <?php namespace BookStack\Entities;
3 use BookStack\Entities\Book;
4 use BookStack\Entities\Chapter;
5 use BookStack\Entities\Page;
6 use BookStack\Entities\EntityRepo;
7 use BookStack\Uploads\ImageService;
12 protected $entityRepo;
13 protected $imageService;
16 * ExportService constructor.
19 public function __construct(EntityRepo $entityRepo, ImageService $imageService)
21 $this->entityRepo = $entityRepo;
22 $this->imageService = $imageService;
26 * Convert a page to a self-contained HTML file.
27 * Includes required CSS & image content. Images are base64 encoded into the HTML.
28 * @param \BookStack\Entities\Page $page
29 * @return mixed|string
32 public function pageToContainedHtml(Page $page)
34 $this->entityRepo->renderPage($page);
35 $pageHtml = view('pages/export', [
38 return $this->containHtml($pageHtml);
42 * Convert a chapter to a self-contained HTML file.
43 * @param \BookStack\Entities\Chapter $chapter
44 * @return mixed|string
47 public function chapterToContainedHtml(Chapter $chapter)
49 $pages = $this->entityRepo->getChapterChildren($chapter);
50 $pages->each(function ($page) {
51 $page->html = $this->entityRepo->renderPage($page);
53 $html = view('chapters/export', [
54 'chapter' => $chapter,
57 return $this->containHtml($html);
61 * Convert a book to a self-contained HTML file.
63 * @return mixed|string
66 public function bookToContainedHtml(Book $book)
68 $bookTree = $this->entityRepo->getBookChildren($book, true, true);
69 $html = view('books/export', [
71 'bookChildren' => $bookTree
73 return $this->containHtml($html);
77 * Convert a page to a PDF file.
79 * @return mixed|string
82 public function pageToPdf(Page $page)
84 $this->entityRepo->renderPage($page);
85 $html = view('pages/pdf', [
88 return $this->htmlToPdf($html);
92 * Convert a chapter to a PDF file.
93 * @param \BookStack\Entities\Chapter $chapter
94 * @return mixed|string
97 public function chapterToPdf(Chapter $chapter)
99 $pages = $this->entityRepo->getChapterChildren($chapter);
100 $pages->each(function ($page) {
101 $page->html = $this->entityRepo->renderPage($page);
103 $html = view('chapters/export', [
104 'chapter' => $chapter,
107 return $this->htmlToPdf($html);
111 * Convert a book to a PDF file
112 * @param \BookStack\Entities\Book $book
116 public function bookToPdf(Book $book)
118 $bookTree = $this->entityRepo->getBookChildren($book, true, true);
119 $html = view('books/export', [
121 'bookChildren' => $bookTree
123 return $this->htmlToPdf($html);
127 * Convert normal webpage HTML to a PDF.
132 protected function htmlToPdf($html)
134 $containedHtml = $this->containHtml($html);
135 $useWKHTML = config('snappy.pdf.binary') !== false;
137 $pdf = \SnappyPDF::loadHTML($containedHtml);
138 $pdf->setOption('print-media-type', true);
140 $pdf = \DomPDF::loadHTML($containedHtml);
142 return $pdf->output();
146 * Bundle of the contents of a html file to be self-contained.
147 * @param $htmlContent
148 * @return mixed|string
151 protected function containHtml($htmlContent)
153 $imageTagsOutput = [];
154 preg_match_all("/\<img.*src\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $imageTagsOutput);
156 // Replace image src with base64 encoded image strings
157 if (isset($imageTagsOutput[0]) && count($imageTagsOutput[0]) > 0) {
158 foreach ($imageTagsOutput[0] as $index => $imgMatch) {
159 $oldImgTagString = $imgMatch;
160 $srcString = $imageTagsOutput[2][$index];
161 $imageEncoded = $this->imageService->imageUriToBase64($srcString);
162 if ($imageEncoded === null) {
163 $imageEncoded = $srcString;
165 $newImgTagString = str_replace($srcString, $imageEncoded, $oldImgTagString);
166 $htmlContent = str_replace($oldImgTagString, $newImgTagString, $htmlContent);
171 preg_match_all("/\<a.*href\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $linksOutput);
173 // Replace image src with base64 encoded image strings
174 if (isset($linksOutput[0]) && count($linksOutput[0]) > 0) {
175 foreach ($linksOutput[0] as $index => $linkMatch) {
176 $oldLinkString = $linkMatch;
177 $srcString = $linksOutput[2][$index];
178 if (strpos(trim($srcString), 'http') !== 0) {
179 $newSrcString = url($srcString);
180 $newLinkString = str_replace($srcString, $newSrcString, $oldLinkString);
181 $htmlContent = str_replace($oldLinkString, $newLinkString, $htmlContent);
186 // Replace any relative links with system domain
191 * Converts the page contents into simple plain text.
192 * This method filters any bad looking content to provide a nice final output.
196 public function pageToPlainText(Page $page)
198 $html = $this->entityRepo->renderPage($page);
199 $text = strip_tags($html);
200 // Replace multiple spaces with single spaces
201 $text = preg_replace('/\ {2,}/', ' ', $text);
202 // Reduce multiple horrid whitespace characters.
203 $text = preg_replace('/(\x0A|\xA0|\x0A|\r|\n){2,}/su', "\n\n", $text);
204 $text = html_entity_decode($text);
206 $text = $page->name . "\n\n" . $text;
211 * Convert a chapter into a plain text string.
212 * @param \BookStack\Entities\Chapter $chapter
215 public function chapterToPlainText(Chapter $chapter)
217 $text = $chapter->name . "\n\n";
218 $text .= $chapter->description . "\n\n";
219 foreach ($chapter->pages as $page) {
220 $text .= $this->pageToPlainText($page);
226 * Convert a book into a plain text string.
230 public function bookToPlainText(Book $book)
232 $bookTree = $this->entityRepo->getBookChildren($book, true, true);
233 $text = $book->name . "\n\n";
234 foreach ($bookTree as $bookChild) {
235 if ($bookChild->isA('chapter')) {
236 $text .= $this->chapterToPlainText($bookChild);
238 $text .= $this->pageToPlainText($bookChild);