1 <?php namespace BookStack\Entities;
3 use BookStack\Entities\Managers\BookContents;
4 use BookStack\Entities\Managers\PageContent;
5 use BookStack\Uploads\ImageService;
14 protected $imageService;
17 * ExportService constructor.
19 public function __construct(ImageService $imageService)
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.
29 public function pageToContainedHtml(Page $page)
31 $page->html = (new PageContent($page))->render();
32 $pageHtml = view('pages/export', [
35 return $this->containHtml($pageHtml);
39 * Convert a chapter to a self-contained HTML file.
42 public function chapterToContainedHtml(Chapter $chapter)
44 $pages = $chapter->getVisiblePages();
45 $pages->each(function ($page) {
46 $page->html = (new PageContent($page))->render();
48 $html = view('chapters/export', [
49 'chapter' => $chapter,
52 return $this->containHtml($html);
56 * Convert a book to a self-contained HTML file.
59 public function bookToContainedHtml(Book $book)
61 $bookTree = (new BookContents($book))->getTree(false, true);
62 $html = view('books/export', [
64 'bookChildren' => $bookTree
66 return $this->containHtml($html);
70 * Convert a page to a PDF file.
73 public function pageToPdf(Page $page)
75 $page->html = (new PageContent($page))->render();
76 $html = view('pages/pdf', [
79 return $this->htmlToPdf($html);
83 * Convert a chapter to a PDF file.
86 public function chapterToPdf(Chapter $chapter)
88 $pages = $chapter->getVisiblePages();
89 $pages->each(function ($page) {
90 $page->html = (new PageContent($page))->render();
93 $html = view('chapters/export', [
94 'chapter' => $chapter,
98 return $this->htmlToPdf($html);
102 * Convert a book to a PDF file.
105 public function bookToPdf(Book $book)
107 $bookTree = (new BookContents($book))->getTree(false, true);
108 $html = view('books/export', [
110 'bookChildren' => $bookTree
112 return $this->htmlToPdf($html);
116 * Convert normal web-page HTML to a PDF.
119 protected function htmlToPdf(string $html): string
121 $containedHtml = $this->containHtml($html);
122 $useWKHTML = config('snappy.pdf.binary') !== false;
124 $pdf = SnappyPDF::loadHTML($containedHtml);
125 $pdf->setOption('print-media-type', true);
127 $pdf = DomPDF::loadHTML($containedHtml);
129 return $pdf->output();
133 * Bundle of the contents of a html file to be self-contained.
136 protected function containHtml(string $htmlContent): string
138 $imageTagsOutput = [];
139 preg_match_all("/\<img.*src\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $imageTagsOutput);
141 // Replace image src with base64 encoded image strings
142 if (isset($imageTagsOutput[0]) && count($imageTagsOutput[0]) > 0) {
143 foreach ($imageTagsOutput[0] as $index => $imgMatch) {
144 $oldImgTagString = $imgMatch;
145 $srcString = $imageTagsOutput[2][$index];
146 $imageEncoded = $this->imageService->imageUriToBase64($srcString);
147 if ($imageEncoded === null) {
148 $imageEncoded = $srcString;
150 $newImgTagString = str_replace($srcString, $imageEncoded, $oldImgTagString);
151 $htmlContent = str_replace($oldImgTagString, $newImgTagString, $htmlContent);
156 preg_match_all("/\<a.*href\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $linksOutput);
158 // Replace image src with base64 encoded image strings
159 if (isset($linksOutput[0]) && count($linksOutput[0]) > 0) {
160 foreach ($linksOutput[0] as $index => $linkMatch) {
161 $oldLinkString = $linkMatch;
162 $srcString = $linksOutput[2][$index];
163 if (strpos(trim($srcString), 'http') !== 0) {
164 $newSrcString = url($srcString);
165 $newLinkString = str_replace($srcString, $newSrcString, $oldLinkString);
166 $htmlContent = str_replace($oldLinkString, $newLinkString, $htmlContent);
171 // Replace any relative links with system domain
176 * Converts the page contents into simple plain text.
177 * This method filters any bad looking content to provide a nice final output.
179 public function pageToPlainText(Page $page): string
181 $html = (new PageContent($page))->render();
182 $text = strip_tags($html);
183 // Replace multiple spaces with single spaces
184 $text = preg_replace('/\ {2,}/', ' ', $text);
185 // Reduce multiple horrid whitespace characters.
186 $text = preg_replace('/(\x0A|\xA0|\x0A|\r|\n){2,}/su', "\n\n", $text);
187 $text = html_entity_decode($text);
189 $text = $page->name . "\n\n" . $text;
194 * Convert a chapter into a plain text string.
196 public function chapterToPlainText(Chapter $chapter): string
198 $text = $chapter->name . "\n\n";
199 $text .= $chapter->description . "\n\n";
200 foreach ($chapter->pages as $page) {
201 $text .= $this->pageToPlainText($page);
207 * Convert a book into a plain text string.
209 public function bookToPlainText(Book $book): string
211 $bookTree = (new BookContents($book))->getTree(false, true);
212 $text = $book->name . "\n\n";
213 foreach ($bookTree as $bookChild) {
214 if ($bookChild->isA('chapter')) {
215 $text .= $this->chapterToPlainText($bookChild);
217 $text .= $this->pageToPlainText($bookChild);