3 namespace BookStack\Entities\Tools;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Entities\Tools\Markdown\HtmlToMarkdown;
9 use BookStack\Uploads\ImageService;
18 protected $imageService;
19 protected $pdfGenerator;
22 * ExportService constructor.
24 public function __construct(ImageService $imageService, PdfGenerator $pdfGenerator)
26 $this->imageService = $imageService;
27 $this->pdfGenerator = $pdfGenerator;
31 * Convert a page to a self-contained HTML file.
32 * Includes required CSS & image content. Images are base64 encoded into the HTML.
36 public function pageToContainedHtml(Page $page)
38 $page->html = (new PageContent($page))->render();
39 $pageHtml = view('pages.export', [
44 return $this->containHtml($pageHtml);
48 * Convert a chapter to a self-contained HTML file.
52 public function chapterToContainedHtml(Chapter $chapter)
54 $pages = $chapter->getVisiblePages();
55 $pages->each(function ($page) {
56 $page->html = (new PageContent($page))->render();
58 $html = view('chapters.export', [
59 'chapter' => $chapter,
64 return $this->containHtml($html);
68 * Convert a book to a self-contained HTML file.
72 public function bookToContainedHtml(Book $book)
74 $bookTree = (new BookContents($book))->getTree(false, true);
75 $html = view('books.export', [
77 'bookChildren' => $bookTree,
81 return $this->containHtml($html);
85 * Convert a page to a PDF file.
89 public function pageToPdf(Page $page)
91 $page->html = (new PageContent($page))->render();
92 $html = view('pages.export', [
95 'engine' => $this->pdfGenerator->getActiveEngine(),
98 return $this->htmlToPdf($html);
102 * Convert a chapter to a PDF file.
106 public function chapterToPdf(Chapter $chapter)
108 $pages = $chapter->getVisiblePages();
109 $pages->each(function ($page) {
110 $page->html = (new PageContent($page))->render();
113 $html = view('chapters.export', [
114 'chapter' => $chapter,
117 'engine' => $this->pdfGenerator->getActiveEngine(),
120 return $this->htmlToPdf($html);
124 * Convert a book to a PDF file.
128 public function bookToPdf(Book $book)
130 $bookTree = (new BookContents($book))->getTree(false, true);
131 $html = view('books.export', [
133 'bookChildren' => $bookTree,
135 'engine' => $this->pdfGenerator->getActiveEngine(),
138 return $this->htmlToPdf($html);
142 * Convert normal web-page HTML to a PDF.
146 protected function htmlToPdf(string $html): string
148 $html = $this->containHtml($html);
149 $html = $this->replaceIframesWithLinks($html);
150 $html = $this->openDetailElements($html);
152 return $this->pdfGenerator->fromHtml($html);
156 * Within the given HTML content, Open any detail blocks.
158 protected function openDetailElements(string $html): string
160 libxml_use_internal_errors(true);
162 $doc = new DOMDocument();
163 $doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
164 $xPath = new DOMXPath($doc);
166 $details = $xPath->query('//details');
167 /** @var DOMElement $detail */
168 foreach ($details as $detail) {
169 $detail->setAttribute('open', 'open');
172 return $doc->saveHTML();
176 * Within the given HTML content, replace any iframe elements
177 * with anchor links within paragraph blocks.
179 protected function replaceIframesWithLinks(string $html): string
181 libxml_use_internal_errors(true);
183 $doc = new DOMDocument();
184 $doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
185 $xPath = new DOMXPath($doc);
187 $iframes = $xPath->query('//iframe');
188 /** @var DOMElement $iframe */
189 foreach ($iframes as $iframe) {
190 $link = $iframe->getAttribute('src');
191 if (strpos($link, '//') === 0) {
192 $link = 'https:' . $link;
195 $anchor = $doc->createElement('a', $link);
196 $anchor->setAttribute('href', $link);
197 $paragraph = $doc->createElement('p');
198 $paragraph->appendChild($anchor);
199 $iframe->parentNode->replaceChild($paragraph, $iframe);
202 return $doc->saveHTML();
206 * Bundle of the contents of a html file to be self-contained.
210 protected function containHtml(string $htmlContent): string
212 $imageTagsOutput = [];
213 preg_match_all("/\<img.*?src\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $imageTagsOutput);
215 // Replace image src with base64 encoded image strings
216 if (isset($imageTagsOutput[0]) && count($imageTagsOutput[0]) > 0) {
217 foreach ($imageTagsOutput[0] as $index => $imgMatch) {
218 $oldImgTagString = $imgMatch;
219 $srcString = $imageTagsOutput[2][$index];
220 $imageEncoded = $this->imageService->imageUriToBase64($srcString);
221 if ($imageEncoded === null) {
222 $imageEncoded = $srcString;
224 $newImgTagString = str_replace($srcString, $imageEncoded, $oldImgTagString);
225 $htmlContent = str_replace($oldImgTagString, $newImgTagString, $htmlContent);
230 preg_match_all("/\<a.*href\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $linksOutput);
232 // Replace image src with base64 encoded image strings
233 if (isset($linksOutput[0]) && count($linksOutput[0]) > 0) {
234 foreach ($linksOutput[0] as $index => $linkMatch) {
235 $oldLinkString = $linkMatch;
236 $srcString = $linksOutput[2][$index];
237 if (strpos(trim($srcString), 'http') !== 0) {
238 $newSrcString = url($srcString);
239 $newLinkString = str_replace($srcString, $newSrcString, $oldLinkString);
240 $htmlContent = str_replace($oldLinkString, $newLinkString, $htmlContent);
245 // Replace any relative links with system domain
250 * Converts the page contents into simple plain text.
251 * This method filters any bad looking content to provide a nice final output.
253 public function pageToPlainText(Page $page): string
255 $html = (new PageContent($page))->render();
256 $text = strip_tags($html);
257 // Replace multiple spaces with single spaces
258 $text = preg_replace('/\ {2,}/', ' ', $text);
259 // Reduce multiple horrid whitespace characters.
260 $text = preg_replace('/(\x0A|\xA0|\x0A|\r|\n){2,}/su', "\n\n", $text);
261 $text = html_entity_decode($text);
263 $text = $page->name . "\n\n" . $text;
269 * Convert a chapter into a plain text string.
271 public function chapterToPlainText(Chapter $chapter): string
273 $text = $chapter->name . "\n\n";
274 $text .= $chapter->description . "\n\n";
275 foreach ($chapter->getVisiblePages() as $page) {
276 $text .= $this->pageToPlainText($page);
283 * Convert a book into a plain text string.
285 public function bookToPlainText(Book $book): string
287 $bookTree = (new BookContents($book))->getTree(false, false);
288 $text = $book->name . "\n\n";
289 foreach ($bookTree as $bookChild) {
290 if ($bookChild->isA('chapter')) {
291 $text .= $this->chapterToPlainText($bookChild);
293 $text .= $this->pageToPlainText($bookChild);
301 * Convert a page to a Markdown file.
303 public function pageToMarkdown(Page $page): string
305 if ($page->markdown) {
306 return '# ' . $page->name . "\n\n" . $page->markdown;
309 return '# ' . $page->name . "\n\n" . (new HtmlToMarkdown($page->html))->convert();
313 * Convert a chapter to a Markdown file.
315 public function chapterToMarkdown(Chapter $chapter): string
317 $text = '# ' . $chapter->name . "\n\n";
318 $text .= $chapter->description . "\n\n";
319 foreach ($chapter->pages as $page) {
320 $text .= $this->pageToMarkdown($page) . "\n\n";
327 * Convert a book into a plain text string.
329 public function bookToMarkdown(Book $book): string
331 $bookTree = (new BookContents($book))->getTree(false, true);
332 $text = '# ' . $book->name . "\n\n";
333 foreach ($bookTree as $bookChild) {
334 if ($bookChild instanceof Chapter) {
335 $text .= $this->chapterToMarkdown($bookChild);
337 $text .= $this->pageToMarkdown($bookChild);