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\Entities\Tools\Markdown\HtmlToMarkdown;
7 use BookStack\Uploads\ImageService;
16 protected $imageService;
19 * ExportService constructor.
21 public function __construct(ImageService $imageService)
23 $this->imageService = $imageService;
27 * Convert a page to a self-contained HTML file.
28 * Includes required CSS & image content. Images are base64 encoded into the HTML.
31 public function pageToContainedHtml(Page $page)
33 $page->html = (new PageContent($page))->render();
34 $pageHtml = view('pages.export', [
38 return $this->containHtml($pageHtml);
42 * Convert a chapter to a self-contained HTML file.
45 public function chapterToContainedHtml(Chapter $chapter)
47 $pages = $chapter->getVisiblePages();
48 $pages->each(function ($page) {
49 $page->html = (new PageContent($page))->render();
51 $html = view('chapters.export', [
52 'chapter' => $chapter,
56 return $this->containHtml($html);
60 * Convert a book to a self-contained HTML file.
63 public function bookToContainedHtml(Book $book)
65 $bookTree = (new BookContents($book))->getTree(false, true);
66 $html = view('books.export', [
68 'bookChildren' => $bookTree,
71 return $this->containHtml($html);
75 * Convert a page to a PDF file.
78 public function pageToPdf(Page $page)
80 $page->html = (new PageContent($page))->render();
81 $html = view('pages.export', [
85 return $this->htmlToPdf($html);
89 * Convert a chapter to a PDF file.
92 public function chapterToPdf(Chapter $chapter)
94 $pages = $chapter->getVisiblePages();
95 $pages->each(function ($page) {
96 $page->html = (new PageContent($page))->render();
99 $html = view('chapters.export', [
100 'chapter' => $chapter,
105 return $this->htmlToPdf($html);
109 * Convert a book to a PDF file.
112 public function bookToPdf(Book $book)
114 $bookTree = (new BookContents($book))->getTree(false, true);
115 $html = view('books.export', [
117 'bookChildren' => $bookTree,
120 return $this->htmlToPdf($html);
124 * Convert normal web-page HTML to a PDF.
127 protected function htmlToPdf(string $html): string
129 $containedHtml = $this->containHtml($html);
130 $useWKHTML = config('snappy.pdf.binary') !== false;
132 $pdf = SnappyPDF::loadHTML($containedHtml);
133 $pdf->setOption('print-media-type', true);
135 $pdf = DomPDF::loadHTML($containedHtml);
137 return $pdf->output();
141 * Bundle of the contents of a html file to be self-contained.
144 protected function containHtml(string $htmlContent): string
146 $imageTagsOutput = [];
147 preg_match_all("/\<img.*?src\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $imageTagsOutput);
149 // Replace image src with base64 encoded image strings
150 if (isset($imageTagsOutput[0]) && count($imageTagsOutput[0]) > 0) {
151 foreach ($imageTagsOutput[0] as $index => $imgMatch) {
152 $oldImgTagString = $imgMatch;
153 $srcString = $imageTagsOutput[2][$index];
154 $imageEncoded = $this->imageService->imageUriToBase64($srcString);
155 if ($imageEncoded === null) {
156 $imageEncoded = $srcString;
158 $newImgTagString = str_replace($srcString, $imageEncoded, $oldImgTagString);
159 $htmlContent = str_replace($oldImgTagString, $newImgTagString, $htmlContent);
164 preg_match_all("/\<a.*href\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $linksOutput);
166 // Replace image src with base64 encoded image strings
167 if (isset($linksOutput[0]) && count($linksOutput[0]) > 0) {
168 foreach ($linksOutput[0] as $index => $linkMatch) {
169 $oldLinkString = $linkMatch;
170 $srcString = $linksOutput[2][$index];
171 if (strpos(trim($srcString), 'http') !== 0) {
172 $newSrcString = url($srcString);
173 $newLinkString = str_replace($srcString, $newSrcString, $oldLinkString);
174 $htmlContent = str_replace($oldLinkString, $newLinkString, $htmlContent);
179 // Replace any relative links with system domain
184 * Converts the page contents into simple plain text.
185 * This method filters any bad looking content to provide a nice final output.
187 public function pageToPlainText(Page $page): string
189 $html = (new PageContent($page))->render();
190 $text = strip_tags($html);
191 // Replace multiple spaces with single spaces
192 $text = preg_replace('/\ {2,}/', ' ', $text);
193 // Reduce multiple horrid whitespace characters.
194 $text = preg_replace('/(\x0A|\xA0|\x0A|\r|\n){2,}/su', "\n\n", $text);
195 $text = html_entity_decode($text);
197 $text = $page->name . "\n\n" . $text;
202 * Convert a chapter into a plain text string.
204 public function chapterToPlainText(Chapter $chapter): string
206 $text = $chapter->name . "\n\n";
207 $text .= $chapter->description . "\n\n";
208 foreach ($chapter->getVisiblePages() as $page) {
209 $text .= $this->pageToPlainText($page);
215 * Convert a book into a plain text string.
217 public function bookToPlainText(Book $book): string
219 $bookTree = (new BookContents($book))->getTree(false, false);
220 $text = $book->name . "\n\n";
221 foreach ($bookTree as $bookChild) {
222 if ($bookChild->isA('chapter')) {
223 $text .= $this->chapterToPlainText($bookChild);
225 $text .= $this->pageToPlainText($bookChild);
232 * Convert a page to a Markdown file.
234 public function pageToMarkdown(Page $page): string
236 if ($page->markdown) {
237 return "# " . $page->name . "\n\n" . $page->markdown;
240 return "# " . $page->name . "\n\n" . (new HtmlToMarkdown($page->html))->convert();
244 * Convert a chapter to a Markdown file.
246 public function chapterToMarkdown(Chapter $chapter): string
248 $text = "# " . $chapter->name . "\n\n";
249 $text .= $chapter->description . "\n\n";
250 foreach ($chapter->pages as $page) {
251 $text .= $this->pageToMarkdown($page) . "\n\n";
257 * Convert a book into a plain text string.
259 public function bookToMarkdown(Book $book): string
261 $bookTree = (new BookContents($book))->getTree(false, true);
262 $text = "# " . $book->name . "\n\n";
263 foreach ($bookTree as $bookChild) {
264 if ($bookChild instanceof Chapter) {
265 $text .= $this->chapterToMarkdown($bookChild);
267 $text .= $this->pageToMarkdown($bookChild);