1 <?php namespace BookStack\Entities;
3 use BookStack\Entities\Repos\EntityRepo;
4 use BookStack\Uploads\ImageService;
5 use BookStack\Exceptions\ExportException;
9 protected $contentMatching = [
10 'video' => ["www.youtube.com", "player.vimeo.com", "www.dailymotion.com"],
11 'map' => ['maps.google.com']
14 protected $entityRepo;
15 protected $imageService;
18 * ExportService constructor.
19 * @param EntityRepo $entityRepo
20 * @param ImageService $imageService
22 public function __construct(EntityRepo $entityRepo, ImageService $imageService)
24 $this->entityRepo = $entityRepo;
25 $this->imageService = $imageService;
29 * Convert a page to a self-contained HTML file.
30 * Includes required CSS & image content. Images are base64 encoded into the HTML.
31 * @param \BookStack\Entities\Page $page
32 * @return mixed|string
35 public function pageToContainedHtml(Page $page)
37 $this->entityRepo->renderPage($page);
38 $pageHtml = view('pages/export', [
41 return $this->containHtml($pageHtml);
45 * Convert a chapter to a self-contained HTML file.
46 * @param \BookStack\Entities\Chapter $chapter
47 * @return mixed|string
50 public function chapterToContainedHtml(Chapter $chapter)
52 $pages = $this->entityRepo->getChapterChildren($chapter);
53 $pages->each(function ($page) {
54 $page->html = $this->entityRepo->renderPage($page);
56 $html = view('chapters/export', [
57 'chapter' => $chapter,
60 return $this->containHtml($html);
64 * Convert a book to a self-contained HTML file.
66 * @return mixed|string
69 public function bookToContainedHtml(Book $book)
71 $bookTree = $this->entityRepo->getBookChildren($book, true, true);
72 $html = view('books/export', [
74 'bookChildren' => $bookTree
76 return $this->containHtml($html);
80 * Convert a page to a PDF file.
82 * @param bool $isTesting
83 * @return mixed|string
86 public function pageToPdf(Page $page, bool $isTesting = false)
88 $this->entityRepo->renderPage($page);
89 $html = view('pages/pdf', [
92 return $this->htmlToPdf($html, $isTesting);
96 * Convert a chapter to a PDF file.
97 * @param \BookStack\Entities\Chapter $chapter
98 * @return mixed|string
101 public function chapterToPdf(Chapter $chapter)
103 $pages = $this->entityRepo->getChapterChildren($chapter);
104 $pages->each(function ($page) {
105 $page->html = $this->entityRepo->renderPage($page);
107 $html = view('chapters/export', [
108 'chapter' => $chapter,
111 return $this->htmlToPdf($html);
115 * Convert a book to a PDF file
116 * @param \BookStack\Entities\Book $book
120 public function bookToPdf(Book $book)
122 $bookTree = $this->entityRepo->getBookChildren($book, true, true);
123 $html = view('books/export', [
125 'bookChildren' => $bookTree
127 return $this->htmlToPdf($html);
131 * Convert normal webpage HTML to a PDF.
137 protected function htmlToPdf($html, $isTesting = false)
139 $containedHtml = $this->containHtml($html, true);
141 return $containedHtml;
143 $useWKHTML = config('snappy.pdf.binary') !== false;
145 $pdf = \SnappyPDF::loadHTML($containedHtml);
146 $pdf->setOption('print-media-type', true);
148 $pdf = \DomPDF::loadHTML($containedHtml);
150 return $pdf->output();
154 * Bundle of the contents of a html file to be self-contained.
155 * @param $htmlContent
157 * @return mixed|string
158 * @throws \BookStack\Exceptions\ExportException
160 protected function containHtml(string $htmlContent, bool $isPDF = false) : string
162 $dom = $this->getDOM($htmlContent);
163 if ($dom === false) {
164 throw new ExportException(trans('errors.dom_parse_error'));
167 // replace image src with base64 encoded image strings
168 $images = $dom->getElementsByTagName('img');
169 foreach ($images as $img) {
170 $base64String = $this->imageService->imageUriToBase64($img->getAttribute('src'));
171 if ($base64String !== null) {
172 $img->setAttribute('src', $base64String);
173 $dom->saveHTML($img);
177 // replace all relative hrefs.
178 $links = $dom->getElementsByTagName('a');
179 foreach ($links as $link) {
180 $href = $link->getAttribute('href');
181 if (strpos(trim($href), 'http') !== 0) {
182 $newHref = url($href);
183 $link->setAttribute('href', $newHref);
184 $dom->saveHTML($link);
188 // replace all src in video, audio and iframe tags
189 $xmlDoc = new \DOMXPath($dom);
190 $srcElements = $xmlDoc->query('//video | //audio | //iframe');
191 foreach ($srcElements as $element) {
192 $element = $this->fixRelativeSrc($element);
193 $dom->saveHTML($element);
196 $src = $element->getAttribute('src');
197 $label = $this->getContentLabel($src);
199 $div = $dom->createElement('div');
200 $textNode = $dom->createTextNode($label);
202 $anchor = $dom->createElement('a');
203 $anchor->setAttribute('href', $src);
204 $anchor->textContent = $src;
206 $div->appendChild($textNode);
207 $div->appendChild($anchor);
209 $element->parentNode->replaceChild($div, $element);
213 return $dom->saveHTML();
217 * Converts the page contents into simple plain text.
218 * This method filters any bad looking content to provide a nice final output.
221 * @throws \BookStack\Exceptions\ExportException
223 public function pageToPlainText(Page $page)
225 $html = $this->entityRepo->renderPage($page);
226 $dom = $this->getDom($html);
228 if ($dom === false) {
229 throw new ExportException(trans('errors.dom_parse_error'));
232 // handle anchor tags.
233 $links = $dom->getElementsByTagName('a');
234 foreach ($links as $link) {
235 $href = $link->getAttribute('href');
236 if (strpos(trim($href), 'http') !== 0) {
237 $newHref = url($href);
238 $link->setAttribute('href', $newHref);
241 $link->textContent = trim($link->textContent . " ($href)");
245 $xmlDoc = new \DOMXPath($dom);
246 $srcElements = $xmlDoc->query('//video | //audio | //iframe | //img');
247 foreach ($srcElements as $element) {
248 $element = $this->fixRelativeSrc($element);
249 $fixedSrc = $element->getAttribute('src');
250 $label = $this->getContentLabel($fixedSrc);
251 $finalLabel = "\n\n$label $fixedSrc\n\n";
253 $textNode = $dom->createTextNode($finalLabel);
254 $element->parentNode->replaceChild($textNode, $element);
257 $text = strip_tags($dom->saveHTML());
258 // Replace multiple spaces with single spaces
259 $text = preg_replace('/\ {2,}/', ' ', $text);
260 // Reduce multiple horrid whitespace characters.
261 $text = preg_replace('/(\x0A|\xA0|\x0A|\r|\n){2,}/su', "\n\n", $text);
262 $text = html_entity_decode($text);
264 $text = $page->name . "\n\n" . $text;
269 * Convert a chapter into a plain text string.
270 * @param \BookStack\Entities\Chapter $chapter
273 public function chapterToPlainText(Chapter $chapter)
275 $text = $chapter->name . "\n\n";
276 $text .= $chapter->description . "\n\n";
277 foreach ($chapter->pages as $page) {
278 $text .= $this->pageToPlainText($page);
284 * Convert a book into a plain text string.
288 public function bookToPlainText(Book $book)
290 $bookTree = $this->entityRepo->getBookChildren($book, true, true);
291 $text = $book->name . "\n\n";
292 foreach ($bookTree as $bookChild) {
293 if ($bookChild->isA('chapter')) {
294 $text .= $this->chapterToPlainText($bookChild);
296 $text .= $this->pageToPlainText($bookChild);
302 protected function getDom(string $htmlContent) : \DOMDocument
304 // See - https://stackoverflow.com/a/17559716/903324
305 $dom = new \DOMDocument();
306 libxml_use_internal_errors(true);
307 $dom->loadHTML($htmlContent);
308 libxml_clear_errors();
312 protected function fixRelativeSrc(\DOMElement $element): \DOMElement
314 $src = $element->getAttribute('src');
315 if (strpos(trim($src), 'http') !== 0) {
316 $newSrc = 'https:' . $src;
317 $element->setAttribute('src', $newSrc);
323 protected function getContentLabel(string $src) : string
325 foreach ($this->contentMatching as $key => $possibleValues) {
326 foreach ($possibleValues as $value) {
327 if (strpos($src, $value)) {
328 return trans("entities.$key");
332 return trans('entities.embedded_content');