1 <?php namespace BookStack\Entities\Tools;
3 use BookStack\Entities\Models\Book;
4 use BookStack\Entities\Models\Chapter;
5 use BookStack\Entities\Models\Page;
6 use BookStack\Uploads\ImageService;
15 protected $imageService;
18 * ExportService constructor.
20 public function __construct(ImageService $imageService)
22 $this->imageService = $imageService;
26 * Convert a page to a self-contained HTML file.
27 * Includes required CSS & image content. Images are base64 encoded into the HTML.
30 public function pageToContainedHtml(Page $page)
32 $page->html = (new PageContent($page))->render();
33 $pageHtml = view('pages.export', [
37 return $this->containHtml($pageHtml);
41 * Convert a chapter to a self-contained HTML file.
44 public function chapterToContainedHtml(Chapter $chapter)
46 $pages = $chapter->getVisiblePages();
47 $pages->each(function ($page) {
48 $page->html = (new PageContent($page))->render();
50 $html = view('chapters.export', [
51 'chapter' => $chapter,
55 return $this->containHtml($html);
59 * Convert a book to a self-contained HTML file.
62 public function bookToContainedHtml(Book $book)
64 $bookTree = (new BookContents($book))->getTree(false, true);
65 $html = view('books.export', [
67 'bookChildren' => $bookTree,
70 return $this->containHtml($html);
74 * Convert a page to a PDF file.
77 public function pageToPdf(Page $page)
79 $page->html = (new PageContent($page))->render();
80 $html = view('pages.export', [
84 return $this->htmlToPdf($html);
88 * Convert a chapter to a PDF file.
91 public function chapterToPdf(Chapter $chapter)
93 $pages = $chapter->getVisiblePages();
94 $pages->each(function ($page) {
95 $page->html = (new PageContent($page))->render();
98 $html = view('chapters.export', [
99 'chapter' => $chapter,
104 return $this->htmlToPdf($html);
108 * Convert a book to a PDF file.
111 public function bookToPdf(Book $book)
113 $bookTree = (new BookContents($book))->getTree(false, true);
114 $html = view('books.export', [
116 'bookChildren' => $bookTree,
119 return $this->htmlToPdf($html);
123 * Convert normal web-page HTML to a PDF.
126 protected function htmlToPdf(string $html): string
128 $containedHtml = $this->containHtml($html);
129 $useWKHTML = config('snappy.pdf.binary') !== false;
131 $pdf = SnappyPDF::loadHTML($containedHtml);
132 $pdf->setOption('print-media-type', true);
134 $pdf = DomPDF::loadHTML($containedHtml);
136 return $pdf->output();
140 * Bundle of the contents of a html file to be self-contained.
143 protected function containHtml(string $htmlContent): string
145 $imageTagsOutput = [];
146 preg_match_all("/\<img.*?src\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $imageTagsOutput);
148 // Replace image src with base64 encoded image strings
149 if (isset($imageTagsOutput[0]) && count($imageTagsOutput[0]) > 0) {
150 foreach ($imageTagsOutput[0] as $index => $imgMatch) {
151 $oldImgTagString = $imgMatch;
152 $srcString = $imageTagsOutput[2][$index];
153 $imageEncoded = $this->imageService->imageUriToBase64($srcString);
154 if ($imageEncoded === null) {
155 $imageEncoded = $srcString;
157 $newImgTagString = str_replace($srcString, $imageEncoded, $oldImgTagString);
158 $htmlContent = str_replace($oldImgTagString, $newImgTagString, $htmlContent);
163 preg_match_all("/\<a.*href\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $linksOutput);
165 // Replace image src with base64 encoded image strings
166 if (isset($linksOutput[0]) && count($linksOutput[0]) > 0) {
167 foreach ($linksOutput[0] as $index => $linkMatch) {
168 $oldLinkString = $linkMatch;
169 $srcString = $linksOutput[2][$index];
170 if (strpos(trim($srcString), 'http') !== 0) {
171 $newSrcString = url($srcString);
172 $newLinkString = str_replace($srcString, $newSrcString, $oldLinkString);
173 $htmlContent = str_replace($oldLinkString, $newLinkString, $htmlContent);
178 // Replace any relative links with system domain
183 * Converts the page contents into simple plain text.
184 * This method filters any bad looking content to provide a nice final output.
186 public function pageToPlainText(Page $page): string
188 $html = (new PageContent($page))->render();
189 $text = strip_tags($html);
190 // Replace multiple spaces with single spaces
191 $text = preg_replace('/\ {2,}/', ' ', $text);
192 // Reduce multiple horrid whitespace characters.
193 $text = preg_replace('/(\x0A|\xA0|\x0A|\r|\n){2,}/su', "\n\n", $text);
194 $text = html_entity_decode($text);
196 $text = $page->name . "\n\n" . $text;
201 * Convert a chapter into a plain text string.
203 public function chapterToPlainText(Chapter $chapter): string
205 $text = $chapter->name . "\n\n";
206 $text .= $chapter->description . "\n\n";
207 foreach ($chapter->getVisiblePages() as $page) {
208 $text .= $this->pageToPlainText($page);
214 * Convert a book into a plain text string.
216 public function bookToPlainText(Book $book): string
218 $bookTree = (new BookContents($book))->getTree(false, false);
219 $text = $book->name . "\n\n";
220 foreach ($bookTree as $bookChild) {
221 if ($bookChild->isA('chapter')) {
222 $text .= $this->chapterToPlainText($bookChild);
224 $text .= $this->pageToPlainText($bookChild);