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);
151 return $this->pdfGenerator->fromHtml($html);
155 * Within the given HTML content, replace any iframe elements
156 * with anchor links within paragraph blocks.
158 protected function replaceIframesWithLinks(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 $iframes = $xPath->query('//iframe');
167 /** @var DOMElement $iframe */
168 foreach ($iframes as $iframe) {
169 $link = $iframe->getAttribute('src');
170 if (strpos($link, '//') === 0) {
171 $link = 'https:' . $link;
174 $anchor = $doc->createElement('a', $link);
175 $anchor->setAttribute('href', $link);
176 $paragraph = $doc->createElement('p');
177 $paragraph->appendChild($anchor);
178 $iframe->parentNode->replaceChild($paragraph, $iframe);
181 return $doc->saveHTML();
185 * Bundle of the contents of a html file to be self-contained.
189 protected function containHtml(string $htmlContent): string
191 $imageTagsOutput = [];
192 preg_match_all("/\<img.*?src\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $imageTagsOutput);
194 // Replace image src with base64 encoded image strings
195 if (isset($imageTagsOutput[0]) && count($imageTagsOutput[0]) > 0) {
196 foreach ($imageTagsOutput[0] as $index => $imgMatch) {
197 $oldImgTagString = $imgMatch;
198 $srcString = $imageTagsOutput[2][$index];
199 $imageEncoded = $this->imageService->imageUriToBase64($srcString);
200 if ($imageEncoded === null) {
201 $imageEncoded = $srcString;
203 $newImgTagString = str_replace($srcString, $imageEncoded, $oldImgTagString);
204 $htmlContent = str_replace($oldImgTagString, $newImgTagString, $htmlContent);
209 preg_match_all("/\<a.*href\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $linksOutput);
211 // Replace image src with base64 encoded image strings
212 if (isset($linksOutput[0]) && count($linksOutput[0]) > 0) {
213 foreach ($linksOutput[0] as $index => $linkMatch) {
214 $oldLinkString = $linkMatch;
215 $srcString = $linksOutput[2][$index];
216 if (strpos(trim($srcString), 'http') !== 0) {
217 $newSrcString = url($srcString);
218 $newLinkString = str_replace($srcString, $newSrcString, $oldLinkString);
219 $htmlContent = str_replace($oldLinkString, $newLinkString, $htmlContent);
224 // Replace any relative links with system domain
229 * Converts the page contents into simple plain text.
230 * This method filters any bad looking content to provide a nice final output.
232 public function pageToPlainText(Page $page): string
234 $html = (new PageContent($page))->render();
235 $text = strip_tags($html);
236 // Replace multiple spaces with single spaces
237 $text = preg_replace('/\ {2,}/', ' ', $text);
238 // Reduce multiple horrid whitespace characters.
239 $text = preg_replace('/(\x0A|\xA0|\x0A|\r|\n){2,}/su', "\n\n", $text);
240 $text = html_entity_decode($text);
242 $text = $page->name . "\n\n" . $text;
248 * Convert a chapter into a plain text string.
250 public function chapterToPlainText(Chapter $chapter): string
252 $text = $chapter->name . "\n\n";
253 $text .= $chapter->description . "\n\n";
254 foreach ($chapter->getVisiblePages() as $page) {
255 $text .= $this->pageToPlainText($page);
262 * Convert a book into a plain text string.
264 public function bookToPlainText(Book $book): string
266 $bookTree = (new BookContents($book))->getTree(false, false);
267 $text = $book->name . "\n\n";
268 foreach ($bookTree as $bookChild) {
269 if ($bookChild->isA('chapter')) {
270 $text .= $this->chapterToPlainText($bookChild);
272 $text .= $this->pageToPlainText($bookChild);
280 * Convert a page to a Markdown file.
282 public function pageToMarkdown(Page $page): string
284 if ($page->markdown) {
285 return '# ' . $page->name . "\n\n" . $page->markdown;
288 return '# ' . $page->name . "\n\n" . (new HtmlToMarkdown($page->html))->convert();
292 * Convert a chapter to a Markdown file.
294 public function chapterToMarkdown(Chapter $chapter): string
296 $text = '# ' . $chapter->name . "\n\n";
297 $text .= $chapter->description . "\n\n";
298 foreach ($chapter->pages as $page) {
299 $text .= $this->pageToMarkdown($page) . "\n\n";
306 * Convert a book into a plain text string.
308 public function bookToMarkdown(Book $book): string
310 $bookTree = (new BookContents($book))->getTree(false, true);
311 $text = '# ' . $book->name . "\n\n";
312 foreach ($bookTree as $bookChild) {
313 if ($bookChild instanceof Chapter) {
314 $text .= $this->chapterToMarkdown($bookChild);
316 $text .= $this->pageToMarkdown($bookChild);