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', [
97 return $this->htmlToPdf($html);
101 * Convert a chapter to a PDF file.
105 public function chapterToPdf(Chapter $chapter)
107 $pages = $chapter->getVisiblePages();
108 $pages->each(function ($page) {
109 $page->html = (new PageContent($page))->render();
112 $html = view('chapters.export', [
113 'chapter' => $chapter,
118 return $this->htmlToPdf($html);
122 * Convert a book to a PDF file.
126 public function bookToPdf(Book $book)
128 $bookTree = (new BookContents($book))->getTree(false, true);
129 $html = view('books.export', [
131 'bookChildren' => $bookTree,
135 return $this->htmlToPdf($html);
139 * Convert normal web-page HTML to a PDF.
143 protected function htmlToPdf(string $html): string
145 $html = $this->containHtml($html);
146 $html = $this->replaceIframesWithLinks($html);
148 return $this->pdfGenerator->fromHtml($html);
152 * Within the given HTML content, replace any iframe elements
153 * with anchor links within paragraph blocks.
155 protected function replaceIframesWithLinks(string $html): string
157 libxml_use_internal_errors(true);
159 $doc = new DOMDocument();
160 $doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
161 $xPath = new DOMXPath($doc);
163 $iframes = $xPath->query('//iframe');
164 /** @var DOMElement $iframe */
165 foreach ($iframes as $iframe) {
166 $link = $iframe->getAttribute('src');
167 if (strpos($link, '//') === 0) {
168 $link = 'https:' . $link;
171 $anchor = $doc->createElement('a', $link);
172 $anchor->setAttribute('href', $link);
173 $paragraph = $doc->createElement('p');
174 $paragraph->appendChild($anchor);
175 $iframe->parentNode->replaceChild($paragraph, $iframe);
178 return $doc->saveHTML();
182 * Bundle of the contents of a html file to be self-contained.
186 protected function containHtml(string $htmlContent): string
188 $imageTagsOutput = [];
189 preg_match_all("/\<img.*?src\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $imageTagsOutput);
191 // Replace image src with base64 encoded image strings
192 if (isset($imageTagsOutput[0]) && count($imageTagsOutput[0]) > 0) {
193 foreach ($imageTagsOutput[0] as $index => $imgMatch) {
194 $oldImgTagString = $imgMatch;
195 $srcString = $imageTagsOutput[2][$index];
196 $imageEncoded = $this->imageService->imageUriToBase64($srcString);
197 if ($imageEncoded === null) {
198 $imageEncoded = $srcString;
200 $newImgTagString = str_replace($srcString, $imageEncoded, $oldImgTagString);
201 $htmlContent = str_replace($oldImgTagString, $newImgTagString, $htmlContent);
206 preg_match_all("/\<a.*href\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $linksOutput);
208 // Replace image src with base64 encoded image strings
209 if (isset($linksOutput[0]) && count($linksOutput[0]) > 0) {
210 foreach ($linksOutput[0] as $index => $linkMatch) {
211 $oldLinkString = $linkMatch;
212 $srcString = $linksOutput[2][$index];
213 if (strpos(trim($srcString), 'http') !== 0) {
214 $newSrcString = url($srcString);
215 $newLinkString = str_replace($srcString, $newSrcString, $oldLinkString);
216 $htmlContent = str_replace($oldLinkString, $newLinkString, $htmlContent);
221 // Replace any relative links with system domain
226 * Converts the page contents into simple plain text.
227 * This method filters any bad looking content to provide a nice final output.
229 public function pageToPlainText(Page $page): string
231 $html = (new PageContent($page))->render();
232 $text = strip_tags($html);
233 // Replace multiple spaces with single spaces
234 $text = preg_replace('/\ {2,}/', ' ', $text);
235 // Reduce multiple horrid whitespace characters.
236 $text = preg_replace('/(\x0A|\xA0|\x0A|\r|\n){2,}/su', "\n\n", $text);
237 $text = html_entity_decode($text);
239 $text = $page->name . "\n\n" . $text;
245 * Convert a chapter into a plain text string.
247 public function chapterToPlainText(Chapter $chapter): string
249 $text = $chapter->name . "\n\n";
250 $text .= $chapter->description . "\n\n";
251 foreach ($chapter->getVisiblePages() as $page) {
252 $text .= $this->pageToPlainText($page);
259 * Convert a book into a plain text string.
261 public function bookToPlainText(Book $book): string
263 $bookTree = (new BookContents($book))->getTree(false, false);
264 $text = $book->name . "\n\n";
265 foreach ($bookTree as $bookChild) {
266 if ($bookChild->isA('chapter')) {
267 $text .= $this->chapterToPlainText($bookChild);
269 $text .= $this->pageToPlainText($bookChild);
277 * Convert a page to a Markdown file.
279 public function pageToMarkdown(Page $page): string
281 if ($page->markdown) {
282 return '# ' . $page->name . "\n\n" . $page->markdown;
285 return '# ' . $page->name . "\n\n" . (new HtmlToMarkdown($page->html))->convert();
289 * Convert a chapter to a Markdown file.
291 public function chapterToMarkdown(Chapter $chapter): string
293 $text = '# ' . $chapter->name . "\n\n";
294 $text .= $chapter->description . "\n\n";
295 foreach ($chapter->pages as $page) {
296 $text .= $this->pageToMarkdown($page) . "\n\n";
303 * Convert a book into a plain text string.
305 public function bookToMarkdown(Book $book): string
307 $bookTree = (new BookContents($book))->getTree(false, true);
308 $text = '# ' . $book->name . "\n\n";
309 foreach ($bookTree as $bookChild) {
310 if ($bookChild instanceof Chapter) {
311 $text .= $this->chapterToMarkdown($bookChild);
313 $text .= $this->pageToMarkdown($bookChild);