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 $this->entityRepo->renderPage($page);
31 $pageHtml = view('pages/export', [
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 $this->entityRepo->renderPage($page);
78 $html = view('pages/pdf', [
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
141 protected function containHtml($htmlContent)
143 $imageTagsOutput = [];
144 preg_match_all("/\<img.*src\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $imageTagsOutput);
146 // Replace image src with base64 encoded image strings
147 if (isset($imageTagsOutput[0]) && count($imageTagsOutput[0]) > 0) {
148 foreach ($imageTagsOutput[0] as $index => $imgMatch) {
149 $oldImgString = $imgMatch;
150 $srcString = $imageTagsOutput[2][$index];
151 $isLocal = strpos(trim($srcString), 'http') !== 0;
153 $pathString = public_path(trim($srcString, '/'));
155 $pathString = $srcString;
158 // Attempt to find local files even if url not absolute
159 $base = baseUrl('/');
160 if (strpos($srcString, $base) === 0) {
162 $relString = str_replace($base, '', $srcString);
163 $pathString = public_path(trim($relString, '/'));
166 if ($isLocal && !file_exists($pathString)) continue;
169 $imageContent = file_get_contents($pathString);
172 curl_setopt_array($ch, [CURLOPT_URL => $pathString, CURLOPT_RETURNTRANSFER => 1, CURLOPT_CONNECTTIMEOUT => 5]);
173 $imageContent = curl_exec($ch);
174 $err = curl_error($ch);
176 if ($err) throw new \Exception("Image fetch failed, Received error: " . $err);
178 $imageEncoded = 'data:image/' . pathinfo($pathString, PATHINFO_EXTENSION) . ';base64,' . base64_encode($imageContent);
179 $newImageString = str_replace($srcString, $imageEncoded, $oldImgString);
180 } catch (\ErrorException $e) {
181 $newImageString = '';
183 $htmlContent = str_replace($oldImgString, $newImageString, $htmlContent);
188 preg_match_all("/\<a.*href\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $linksOutput);
190 // Replace image src with base64 encoded image strings
191 if (isset($linksOutput[0]) && count($linksOutput[0]) > 0) {
192 foreach ($linksOutput[0] as $index => $linkMatch) {
193 $oldLinkString = $linkMatch;
194 $srcString = $linksOutput[2][$index];
195 if (strpos(trim($srcString), 'http') !== 0) {
196 $newSrcString = url($srcString);
197 $newLinkString = str_replace($srcString, $newSrcString, $oldLinkString);
198 $htmlContent = str_replace($oldLinkString, $newLinkString, $htmlContent);
203 // Replace any relative links with system domain
208 * Converts the page contents into simple plain text.
209 * This method filters any bad looking content to provide a nice final output.
213 public function pageToPlainText(Page $page)
215 $html = $this->entityRepo->renderPage($page);
216 $text = strip_tags($html);
217 // Replace multiple spaces with single spaces
218 $text = preg_replace('/\ {2,}/', ' ', $text);
219 // Reduce multiple horrid whitespace characters.
220 $text = preg_replace('/(\x0A|\xA0|\x0A|\r|\n){2,}/su', "\n\n", $text);
221 $text = html_entity_decode($text);
223 $text = $page->name . "\n\n" . $text;
228 * Convert a chapter into a plain text string.
229 * @param Chapter $chapter
232 public function chapterToPlainText(Chapter $chapter)
234 $text = $chapter->name . "\n\n";
235 $text .= $chapter->description . "\n\n";
236 foreach ($chapter->pages as $page) {
237 $text .= $this->pageToPlainText($page);
243 * Convert a book into a plain text string.
247 public function bookToPlainText(Book $book)
249 $bookTree = $this->entityRepo->getBookChildren($book, true, true);
250 $text = $book->name . "\n\n";
251 foreach ($bookTree as $bookChild) {
252 if ($bookChild->isA('chapter')) {
253 $text .= $this->chapterToPlainText($bookChild);
255 $text .= $this->pageToPlainText($bookChild);