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', [
36 return $this->containHtml($pageHtml);
40 * Convert a chapter to a self-contained HTML file.
43 public function chapterToContainedHtml(Chapter $chapter)
45 $pages = $chapter->getVisiblePages();
46 $pages->each(function ($page) {
47 $page->html = (new PageContent($page))->render();
49 $html = view('chapters.export', [
50 'chapter' => $chapter,
54 return $this->containHtml($html);
58 * Convert a book to a self-contained HTML file.
61 public function bookToContainedHtml(Book $book)
63 $bookTree = (new BookContents($book))->getTree(false, true);
64 $html = view('books.export', [
66 'bookChildren' => $bookTree,
69 return $this->containHtml($html);
73 * Convert a page to a PDF file.
76 public function pageToPdf(Page $page)
78 $page->html = (new PageContent($page))->render();
79 $html = view('pages.export', [
83 return $this->htmlToPdf($html);
87 * Convert a chapter to a PDF file.
90 public function chapterToPdf(Chapter $chapter)
92 $pages = $chapter->getVisiblePages();
93 $pages->each(function ($page) {
94 $page->html = (new PageContent($page))->render();
97 $html = view('chapters.export', [
98 'chapter' => $chapter,
103 return $this->htmlToPdf($html);
107 * Convert a book to a PDF file.
110 public function bookToPdf(Book $book)
112 $bookTree = (new BookContents($book))->getTree(false, true);
113 $html = view('books.export', [
115 'bookChildren' => $bookTree,
118 return $this->htmlToPdf($html);
122 * Convert normal web-page HTML to a PDF.
125 protected function htmlToPdf(string $html): string
127 $containedHtml = $this->containHtml($html);
128 $useWKHTML = config('snappy.pdf.binary') !== false;
130 $pdf = SnappyPDF::loadHTML($containedHtml);
131 $pdf->setOption('print-media-type', true);
133 $pdf = DomPDF::loadHTML($containedHtml);
135 return $pdf->output();
139 * Bundle of the contents of a html file to be self-contained.
142 protected function containHtml(string $htmlContent): string
144 $imageTagsOutput = [];
145 preg_match_all("/\<img.*src\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $imageTagsOutput);
147 // Replace image src with base64 encoded image strings
148 if (isset($imageTagsOutput[0]) && count($imageTagsOutput[0]) > 0) {
149 foreach ($imageTagsOutput[0] as $index => $imgMatch) {
150 $oldImgTagString = $imgMatch;
151 $srcString = $imageTagsOutput[2][$index];
152 $imageEncoded = $this->imageService->imageUriToBase64($srcString);
153 if ($imageEncoded === null) {
154 $imageEncoded = $srcString;
156 $newImgTagString = str_replace($srcString, $imageEncoded, $oldImgTagString);
157 $htmlContent = str_replace($oldImgTagString, $newImgTagString, $htmlContent);
162 preg_match_all("/\<a.*href\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $linksOutput);
164 // Replace image src with base64 encoded image strings
165 if (isset($linksOutput[0]) && count($linksOutput[0]) > 0) {
166 foreach ($linksOutput[0] as $index => $linkMatch) {
167 $oldLinkString = $linkMatch;
168 $srcString = $linksOutput[2][$index];
169 if (strpos(trim($srcString), 'http') !== 0) {
170 $newSrcString = url($srcString);
171 $newLinkString = str_replace($srcString, $newSrcString, $oldLinkString);
172 $htmlContent = str_replace($oldLinkString, $newLinkString, $htmlContent);
177 // Replace any relative links with system domain
182 * Converts the page contents into simple plain text.
183 * This method filters any bad looking content to provide a nice final output.
185 public function pageToPlainText(Page $page): string
187 $html = (new PageContent($page))->render();
188 $text = strip_tags($html);
189 // Replace multiple spaces with single spaces
190 $text = preg_replace('/\ {2,}/', ' ', $text);
191 // Reduce multiple horrid whitespace characters.
192 $text = preg_replace('/(\x0A|\xA0|\x0A|\r|\n){2,}/su', "\n\n", $text);
193 $text = html_entity_decode($text);
195 $text = $page->name . "\n\n" . $text;
200 * Convert a chapter into a plain text string.
202 public function chapterToPlainText(Chapter $chapter): string
204 $text = $chapter->name . "\n\n";
205 $text .= $chapter->description . "\n\n";
206 foreach ($chapter->pages as $page) {
207 $text .= $this->pageToPlainText($page);
213 * Convert a book into a plain text string.
215 public function bookToPlainText(Book $book): string
217 $bookTree = (new BookContents($book))->getTree(false, true);
218 $text = $book->name . "\n\n";
219 foreach ($bookTree as $bookChild) {
220 if ($bookChild->isA('chapter')) {
221 $text .= $this->chapterToPlainText($bookChild);
223 $text .= $this->pageToPlainText($bookChild);