1 <?php namespace BookStack\Services;
6 use BookStack\Repos\EntityRepo;
11 protected $entityRepo;
14 * ExportService constructor.
17 public function __construct(EntityRepo $entityRepo)
19 $this->entityRepo = $entityRepo;
23 * Convert a page to a self-contained HTML file.
24 * Includes required CSS & image content. Images are base64 encoded into the HTML.
26 * @return mixed|string
28 public function pageToContainedHtml(Page $page)
30 $pageHtml = view('pages/export', [
32 'pageContent' => $this->entityRepo->renderPage($page)
34 return $this->containHtml($pageHtml);
38 * Convert a chapter to a self-contained HTML file.
39 * @param Chapter $chapter
40 * @return mixed|string
42 public function chapterToContainedHtml(Chapter $chapter)
44 $pages = $this->entityRepo->getChapterChildren($chapter);
45 $pages->each(function($page) {
46 $page->html = $this->entityRepo->renderPage($page);
48 $html = view('chapters/export', [
49 'chapter' => $chapter,
52 return $this->containHtml($html);
56 * Convert a book to a self-contained HTML file.
58 * @return mixed|string
60 public function bookToContainedHtml(Book $book)
62 $bookTree = $this->entityRepo->getBookChildren($book, true, true);
63 $html = view('books/export', [
65 'bookChildren' => $bookTree
67 return $this->containHtml($html);
71 * Convert a page to a PDF file.
73 * @return mixed|string
75 public function pageToPdf(Page $page)
77 $html = view('pages/pdf', [
79 'pageContent' => $this->entityRepo->renderPage($page)
81 return $this->htmlToPdf($html);
85 * Convert a chapter to a PDF file.
86 * @param Chapter $chapter
87 * @return mixed|string
89 public function chapterToPdf(Chapter $chapter)
91 $pages = $this->entityRepo->getChapterChildren($chapter);
92 $pages->each(function($page) {
93 $page->html = $this->entityRepo->renderPage($page);
95 $html = view('chapters/export', [
96 'chapter' => $chapter,
99 return $this->htmlToPdf($html);
103 * Convert a book to a PDF file
107 public function bookToPdf(Book $book)
109 $bookTree = $this->entityRepo->getBookChildren($book, true, true);
110 $html = view('books/export', [
112 'bookChildren' => $bookTree
114 return $this->htmlToPdf($html);
118 * Convert normal webpage HTML to a PDF.
122 protected function htmlToPdf($html)
124 $containedHtml = $this->containHtml($html);
125 $useWKHTML = config('snappy.pdf.binary') !== false;
127 $pdf = \SnappyPDF::loadHTML($containedHtml);
128 $pdf->setOption('print-media-type', true);
130 $pdf = \PDF::loadHTML($containedHtml);
132 return $pdf->output();
136 * Bundle of the contents of a html file to be self-contained.
137 * @param $htmlContent
138 * @return mixed|string
140 protected function containHtml($htmlContent)
142 $imageTagsOutput = [];
143 preg_match_all("/\<img.*src\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $imageTagsOutput);
145 // Replace image src with base64 encoded image strings
146 if (isset($imageTagsOutput[0]) && count($imageTagsOutput[0]) > 0) {
147 foreach ($imageTagsOutput[0] as $index => $imgMatch) {
148 $oldImgString = $imgMatch;
149 $srcString = $imageTagsOutput[2][$index];
150 $isLocal = strpos(trim($srcString), 'http') !== 0;
152 $pathString = public_path(trim($srcString, '/'));
154 $pathString = $srcString;
156 if ($isLocal && !file_exists($pathString)) continue;
158 $imageContent = file_get_contents($pathString);
159 $imageEncoded = 'data:image/' . pathinfo($pathString, PATHINFO_EXTENSION) . ';base64,' . base64_encode($imageContent);
160 $newImageString = str_replace($srcString, $imageEncoded, $oldImgString);
161 } catch (\ErrorException $e) {
162 $newImageString = '';
164 $htmlContent = str_replace($oldImgString, $newImageString, $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 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);