1 <?php namespace BookStack\Entities;
3 use BookStack\Entities\Repos\EntityRepo;
4 use BookStack\Uploads\ImageService;
10 protected $imageService;
13 * ExportService constructor.
14 * @param EntityRepo $entityRepo
15 * @param ImageService $imageService
17 public function __construct(EntityRepo $entityRepo, ImageService $imageService)
19 $this->entityRepo = $entityRepo;
20 $this->imageService = $imageService;
24 * Convert a page to a self-contained HTML file.
25 * Includes required CSS & image content. Images are base64 encoded into the HTML.
26 * @param \BookStack\Entities\Page $page
27 * @return mixed|string
30 public function pageToContainedHtml(Page $page)
32 $this->entityRepo->renderPage($page);
33 $pageHtml = view('pages/export', [
36 return $this->containHtml($pageHtml);
40 * Convert a chapter to a self-contained HTML file.
41 * @param \BookStack\Entities\Chapter $chapter
42 * @return mixed|string
45 public function chapterToContainedHtml(Chapter $chapter)
47 $pages = $this->entityRepo->getChapterChildren($chapter);
48 $pages->each(function ($page) {
49 $page->html = $this->entityRepo->renderPage($page);
51 $html = view('chapters/export', [
52 'chapter' => $chapter,
55 return $this->containHtml($html);
59 * Convert a book to a self-contained HTML file.
61 * @return mixed|string
64 public function bookToContainedHtml(Book $book)
66 $bookTree = $this->entityRepo->getBookChildren($book, true, true);
67 $html = view('books/export', [
69 'bookChildren' => $bookTree
71 return $this->containHtml($html);
75 * Convert a page to a PDF file.
77 * @return mixed|string
80 public function pageToPdf(Page $page)
82 $this->entityRepo->renderPage($page);
83 $html = view('pages/pdf', [
86 return $this->htmlToPdf($html);
90 * Convert a chapter to a PDF file.
91 * @param \BookStack\Entities\Chapter $chapter
92 * @return mixed|string
95 public function chapterToPdf(Chapter $chapter)
97 $pages = $this->entityRepo->getChapterChildren($chapter);
98 $pages->each(function ($page) {
99 $page->html = $this->entityRepo->renderPage($page);
101 $html = view('chapters/export', [
102 'chapter' => $chapter,
105 return $this->htmlToPdf($html);
109 * Convert a book to a PDF file
110 * @param \BookStack\Entities\Book $book
114 public function bookToPdf(Book $book)
116 $bookTree = $this->entityRepo->getBookChildren($book, true, true);
117 $html = view('books/export', [
119 'bookChildren' => $bookTree
121 return $this->htmlToPdf($html);
125 * Convert normal webpage HTML to a PDF.
130 protected function htmlToPdf($html)
132 $containedHtml = $this->containHtml($html);
133 $useWKHTML = config('snappy.pdf.binary') !== false;
135 $pdf = \SnappyPDF::loadHTML($containedHtml);
136 $pdf->setOption('print-media-type', true);
138 $pdf = \DomPDF::loadHTML($containedHtml);
140 return $pdf->output();
144 * Bundle of the contents of a html file to be self-contained.
145 * @param $htmlContent
146 * @return mixed|string
149 protected function containHtml($htmlContent)
151 $imageTagsOutput = [];
152 preg_match_all("/\<img.*src\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $imageTagsOutput);
154 // Replace image src with base64 encoded image strings
155 if (isset($imageTagsOutput[0]) && count($imageTagsOutput[0]) > 0) {
156 foreach ($imageTagsOutput[0] as $index => $imgMatch) {
157 $oldImgTagString = $imgMatch;
158 $srcString = $imageTagsOutput[2][$index];
159 $imageEncoded = $this->imageService->imageUriToBase64($srcString);
160 if ($imageEncoded === null) {
161 $imageEncoded = $srcString;
163 $newImgTagString = str_replace($srcString, $imageEncoded, $oldImgTagString);
164 $htmlContent = str_replace($oldImgTagString, $newImgTagString, $htmlContent);
169 preg_match_all("/\<a.*href\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $linksOutput);
171 // Replace image src with base64 encoded image strings
172 if (isset($linksOutput[0]) && count($linksOutput[0]) > 0) {
173 foreach ($linksOutput[0] as $index => $linkMatch) {
174 $oldLinkString = $linkMatch;
175 $srcString = $linksOutput[2][$index];
176 if (strpos(trim($srcString), 'http') !== 0) {
177 $newSrcString = url($srcString);
178 $newLinkString = str_replace($srcString, $newSrcString, $oldLinkString);
179 $htmlContent = str_replace($oldLinkString, $newLinkString, $htmlContent);
184 // Replace any relative links with system domain
189 * Converts the page contents into simple plain text.
190 * This method filters any bad looking content to provide a nice final output.
194 public function pageToPlainText(Page $page)
196 $html = $this->entityRepo->renderPage($page);
197 $text = strip_tags($html);
198 // Replace multiple spaces with single spaces
199 $text = preg_replace('/\ {2,}/', ' ', $text);
200 // Reduce multiple horrid whitespace characters.
201 $text = preg_replace('/(\x0A|\xA0|\x0A|\r|\n){2,}/su', "\n\n", $text);
202 $text = html_entity_decode($text);
204 $text = $page->name . "\n\n" . $text;
209 * Convert a chapter into a plain text string.
210 * @param \BookStack\Entities\Chapter $chapter
213 public function chapterToPlainText(Chapter $chapter)
215 $text = $chapter->name . "\n\n";
216 $text .= $chapter->description . "\n\n";
217 foreach ($chapter->pages as $page) {
218 $text .= $this->pageToPlainText($page);
224 * Convert a book into a plain text string.
228 public function bookToPlainText(Book $book)
230 $bookTree = $this->entityRepo->getBookChildren($book, true, true);
231 $text = $book->name . "\n\n";
232 foreach ($bookTree as $bookChild) {
233 if ($bookChild->isA('chapter')) {
234 $text .= $this->chapterToPlainText($bookChild);
236 $text .= $this->pageToPlainText($bookChild);