1 <?php namespace BookStack\Services;
6 use BookStack\Repos\EntityRepo;
11 protected $entityRepo;
12 protected $imageService;
15 * ExportService constructor.
18 public function __construct(EntityRepo $entityRepo, ImageService $imageService)
20 $this->entityRepo = $entityRepo;
21 $this->imageService = $imageService;
25 * Convert a page to a self-contained HTML file.
26 * Includes required CSS & image content. Images are base64 encoded into the HTML.
28 * @return mixed|string
31 public function pageToContainedHtml(Page $page)
33 $this->entityRepo->renderPage($page);
34 $pageHtml = view('pages/export', [
37 return $this->containHtml($pageHtml);
41 * Convert a chapter to a self-contained HTML file.
42 * @param Chapter $chapter
43 * @return mixed|string
46 public function chapterToContainedHtml(Chapter $chapter)
48 $pages = $this->entityRepo->getChapterChildren($chapter);
49 $pages->each(function ($page) {
50 $page->html = $this->entityRepo->renderPage($page);
52 $html = view('chapters/export', [
53 'chapter' => $chapter,
56 return $this->containHtml($html);
60 * Convert a book to a self-contained HTML file.
62 * @return mixed|string
65 public function bookToContainedHtml(Book $book)
67 $bookTree = $this->entityRepo->getBookChildren($book, true, true);
68 $html = view('books/export', [
70 'bookChildren' => $bookTree
72 return $this->containHtml($html);
76 * Convert a page to a PDF file.
78 * @return mixed|string
81 public function pageToPdf(Page $page)
83 $this->entityRepo->renderPage($page);
84 $html = view('pages/pdf', [
87 return $this->htmlToPdf($html);
91 * Convert a chapter to a PDF file.
92 * @param Chapter $chapter
93 * @return mixed|string
96 public function chapterToPdf(Chapter $chapter)
98 $pages = $this->entityRepo->getChapterChildren($chapter);
99 $pages->each(function ($page) {
100 $page->html = $this->entityRepo->renderPage($page);
102 $html = view('chapters/export', [
103 'chapter' => $chapter,
106 return $this->htmlToPdf($html);
110 * Convert a book to a PDF file
115 public function bookToPdf(Book $book)
117 $bookTree = $this->entityRepo->getBookChildren($book, true, true);
118 $html = view('books/export', [
120 'bookChildren' => $bookTree
122 return $this->htmlToPdf($html);
126 * Convert normal webpage HTML to a PDF.
131 protected function htmlToPdf($html)
133 $containedHtml = $this->containHtml($html);
134 $useWKHTML = config('snappy.pdf.binary') !== false;
136 $pdf = \SnappyPDF::loadHTML($containedHtml);
137 $pdf->setOption('print-media-type', true);
139 $pdf = \DomPDF::loadHTML($containedHtml);
141 return $pdf->output();
145 * Bundle of the contents of a html file to be self-contained.
146 * @param $htmlContent
147 * @return mixed|string
150 protected function containHtml($htmlContent)
152 $imageTagsOutput = [];
153 preg_match_all("/\<img.*src\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $imageTagsOutput);
155 // Replace image src with base64 encoded image strings
156 if (isset($imageTagsOutput[0]) && count($imageTagsOutput[0]) > 0) {
157 foreach ($imageTagsOutput[0] as $index => $imgMatch) {
158 $oldImgTagString = $imgMatch;
159 $srcString = $imageTagsOutput[2][$index];
160 $imageEncoded = $this->imageService->imageUriToBase64($srcString);
161 if ($imageEncoded === null) {
162 $imageEncoded = $srcString;
164 $newImgTagString = str_replace($srcString, $imageEncoded, $oldImgTagString);
165 $htmlContent = str_replace($oldImgTagString, $newImgTagString, $htmlContent);
170 preg_match_all("/\<a.*href\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $linksOutput);
172 // Replace image src with base64 encoded image strings
173 if (isset($linksOutput[0]) && count($linksOutput[0]) > 0) {
174 foreach ($linksOutput[0] as $index => $linkMatch) {
175 $oldLinkString = $linkMatch;
176 $srcString = $linksOutput[2][$index];
177 if (strpos(trim($srcString), 'http') !== 0) {
178 $newSrcString = url($srcString);
179 $newLinkString = str_replace($srcString, $newSrcString, $oldLinkString);
180 $htmlContent = str_replace($oldLinkString, $newLinkString, $htmlContent);
185 // Replace any relative links with system domain
190 * Converts the page contents into simple plain text.
191 * This method filters any bad looking content to provide a nice final output.
195 public function pageToPlainText(Page $page)
197 $html = $this->entityRepo->renderPage($page);
198 $text = strip_tags($html);
199 // Replace multiple spaces with single spaces
200 $text = preg_replace('/\ {2,}/', ' ', $text);
201 // Reduce multiple horrid whitespace characters.
202 $text = preg_replace('/(\x0A|\xA0|\x0A|\r|\n){2,}/su', "\n\n", $text);
203 $text = html_entity_decode($text);
205 $text = $page->name . "\n\n" . $text;
210 * Convert a chapter into a plain text string.
211 * @param Chapter $chapter
214 public function chapterToPlainText(Chapter $chapter)
216 $text = $chapter->name . "\n\n";
217 $text .= $chapter->description . "\n\n";
218 foreach ($chapter->pages as $page) {
219 $text .= $this->pageToPlainText($page);
225 * Convert a book into a plain text string.
229 public function bookToPlainText(Book $book)
231 $bookTree = $this->entityRepo->getBookChildren($book, true, true);
232 $text = $book->name . "\n\n";
233 foreach ($bookTree as $bookChild) {
234 if ($bookChild->isA('chapter')) {
235 $text .= $this->chapterToPlainText($bookChild);
237 $text .= $this->pageToPlainText($bookChild);