1 <?php namespace BookStack\Entities;
3 use BookStack\Uploads\ImageService;
9 protected $imageService;
12 * ExportService constructor.
13 * @param EntityRepo $entityRepo
14 * @param ImageService $imageService
16 public function __construct(EntityRepo $entityRepo, ImageService $imageService)
18 $this->entityRepo = $entityRepo;
19 $this->imageService = $imageService;
23 * Convert a page to a self-contained HTML file.
24 * Includes required CSS & image content. Images are base64 encoded into the HTML.
25 * @param \BookStack\Entities\Page $page
26 * @return mixed|string
29 public function pageToContainedHtml(Page $page)
31 $this->entityRepo->renderPage($page);
32 $pageHtml = view('pages/export', [
35 return $this->containHtml($pageHtml);
39 * Convert a chapter to a self-contained HTML file.
40 * @param \BookStack\Entities\Chapter $chapter
41 * @return mixed|string
44 public function chapterToContainedHtml(Chapter $chapter)
46 $pages = $this->entityRepo->getChapterChildren($chapter);
47 $pages->each(function ($page) {
48 $page->html = $this->entityRepo->renderPage($page);
50 $html = view('chapters/export', [
51 'chapter' => $chapter,
54 return $this->containHtml($html);
58 * Convert a book to a self-contained HTML file.
60 * @return mixed|string
63 public function bookToContainedHtml(Book $book)
65 $bookTree = $this->entityRepo->getBookChildren($book, true, true);
66 $html = view('books/export', [
68 'bookChildren' => $bookTree
70 return $this->containHtml($html);
74 * Convert a page to a PDF file.
76 * @return mixed|string
79 public function pageToPdf(Page $page)
81 $this->entityRepo->renderPage($page);
82 $html = view('pages/pdf', [
85 return $this->htmlToPdf($html);
89 * Convert a chapter to a PDF file.
90 * @param \BookStack\Entities\Chapter $chapter
91 * @return mixed|string
94 public function chapterToPdf(Chapter $chapter)
96 $pages = $this->entityRepo->getChapterChildren($chapter);
97 $pages->each(function ($page) {
98 $page->html = $this->entityRepo->renderPage($page);
100 $html = view('chapters/export', [
101 'chapter' => $chapter,
104 return $this->htmlToPdf($html);
108 * Convert a book to a PDF file
109 * @param \BookStack\Entities\Book $book
113 public function bookToPdf(Book $book)
115 $bookTree = $this->entityRepo->getBookChildren($book, true, true);
116 $html = view('books/export', [
118 'bookChildren' => $bookTree
120 return $this->htmlToPdf($html);
124 * Convert normal webpage HTML to a PDF.
129 protected function htmlToPdf($html)
131 $containedHtml = $this->containHtml($html);
132 $useWKHTML = config('snappy.pdf.binary') !== false;
134 $pdf = \SnappyPDF::loadHTML($containedHtml);
135 $pdf->setOption('print-media-type', true);
137 $pdf = \DomPDF::loadHTML($containedHtml);
139 return $pdf->output();
143 * Bundle of the contents of a html file to be self-contained.
144 * @param $htmlContent
145 * @return mixed|string
148 protected function containHtml($htmlContent)
150 $imageTagsOutput = [];
151 preg_match_all("/\<img.*src\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $imageTagsOutput);
153 // Replace image src with base64 encoded image strings
154 if (isset($imageTagsOutput[0]) && count($imageTagsOutput[0]) > 0) {
155 foreach ($imageTagsOutput[0] as $index => $imgMatch) {
156 $oldImgTagString = $imgMatch;
157 $srcString = $imageTagsOutput[2][$index];
158 $imageEncoded = $this->imageService->imageUriToBase64($srcString);
159 if ($imageEncoded === null) {
160 $imageEncoded = $srcString;
162 $newImgTagString = str_replace($srcString, $imageEncoded, $oldImgTagString);
163 $htmlContent = str_replace($oldImgTagString, $newImgTagString, $htmlContent);
168 preg_match_all("/\<a.*href\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $linksOutput);
170 // Replace image src with base64 encoded image strings
171 if (isset($linksOutput[0]) && count($linksOutput[0]) > 0) {
172 foreach ($linksOutput[0] as $index => $linkMatch) {
173 $oldLinkString = $linkMatch;
174 $srcString = $linksOutput[2][$index];
175 if (strpos(trim($srcString), 'http') !== 0) {
176 $newSrcString = url($srcString);
177 $newLinkString = str_replace($srcString, $newSrcString, $oldLinkString);
178 $htmlContent = str_replace($oldLinkString, $newLinkString, $htmlContent);
183 // Replace any relative links with system domain
188 * Converts the page contents into simple plain text.
189 * This method filters any bad looking content to provide a nice final output.
193 public function pageToPlainText(Page $page)
195 $html = $this->entityRepo->renderPage($page);
196 $text = strip_tags($html);
197 // Replace multiple spaces with single spaces
198 $text = preg_replace('/\ {2,}/', ' ', $text);
199 // Reduce multiple horrid whitespace characters.
200 $text = preg_replace('/(\x0A|\xA0|\x0A|\r|\n){2,}/su', "\n\n", $text);
201 $text = html_entity_decode($text);
203 $text = $page->name . "\n\n" . $text;
208 * Convert a chapter into a plain text string.
209 * @param \BookStack\Entities\Chapter $chapter
212 public function chapterToPlainText(Chapter $chapter)
214 $text = $chapter->name . "\n\n";
215 $text .= $chapter->description . "\n\n";
216 foreach ($chapter->pages as $page) {
217 $text .= $this->pageToPlainText($page);
223 * Convert a book into a plain text string.
227 public function bookToPlainText(Book $book)
229 $bookTree = $this->entityRepo->getBookChildren($book, true, true);
230 $text = $book->name . "\n\n";
231 foreach ($bookTree as $bookChild) {
232 if ($bookChild->isA('chapter')) {
233 $text .= $this->chapterToPlainText($bookChild);
235 $text .= $this->pageToPlainText($bookChild);