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;
17 protected $imageService;
20 * ExportService constructor.
22 public function __construct(ImageService $imageService)
24 $this->imageService = $imageService;
28 * Convert a page to a self-contained HTML file.
29 * Includes required CSS & image content. Images are base64 encoded into the HTML.
33 public function pageToContainedHtml(Page $page)
35 $page->html = (new PageContent($page))->render();
36 $pageHtml = view('pages.export', [
41 return $this->containHtml($pageHtml);
45 * Convert a chapter to a self-contained HTML file.
49 public function chapterToContainedHtml(Chapter $chapter)
51 $pages = $chapter->getVisiblePages();
52 $pages->each(function ($page) {
53 $page->html = (new PageContent($page))->render();
55 $html = view('chapters.export', [
56 'chapter' => $chapter,
61 return $this->containHtml($html);
65 * Convert a book to a self-contained HTML file.
69 public function bookToContainedHtml(Book $book)
71 $bookTree = (new BookContents($book))->getTree(false, true);
72 $html = view('books.export', [
74 'bookChildren' => $bookTree,
78 return $this->containHtml($html);
82 * Convert a page to a PDF file.
86 public function pageToPdf(Page $page)
88 $page->html = (new PageContent($page))->render();
89 $html = view('pages.export', [
94 return $this->htmlToPdf($html);
98 * Convert a chapter to a PDF file.
102 public function chapterToPdf(Chapter $chapter)
104 $pages = $chapter->getVisiblePages();
105 $pages->each(function ($page) {
106 $page->html = (new PageContent($page))->render();
109 $html = view('chapters.export', [
110 'chapter' => $chapter,
115 return $this->htmlToPdf($html);
119 * Convert a book to a PDF file.
123 public function bookToPdf(Book $book)
125 $bookTree = (new BookContents($book))->getTree(false, true);
126 $html = view('books.export', [
128 'bookChildren' => $bookTree,
132 return $this->htmlToPdf($html);
136 * Convert normal web-page HTML to a PDF.
140 protected function htmlToPdf(string $html): string
142 $containedHtml = $this->containHtml($html);
143 $useWKHTML = config('snappy.pdf.binary') !== false;
145 $pdf = SnappyPDF::loadHTML($containedHtml);
146 $pdf->setOption('print-media-type', true);
148 $pdf = DomPDF::loadHTML($containedHtml);
151 return $pdf->output();
155 * Bundle of the contents of a html file to be self-contained.
159 protected function containHtml(string $htmlContent): string
161 $imageTagsOutput = [];
162 preg_match_all("/\<img.*?src\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $imageTagsOutput);
164 // Replace image src with base64 encoded image strings
165 if (isset($imageTagsOutput[0]) && count($imageTagsOutput[0]) > 0) {
166 foreach ($imageTagsOutput[0] as $index => $imgMatch) {
167 $oldImgTagString = $imgMatch;
168 $srcString = $imageTagsOutput[2][$index];
169 $imageEncoded = $this->imageService->imageUriToBase64($srcString);
170 if ($imageEncoded === null) {
171 $imageEncoded = $srcString;
173 $newImgTagString = str_replace($srcString, $imageEncoded, $oldImgTagString);
174 $htmlContent = str_replace($oldImgTagString, $newImgTagString, $htmlContent);
179 preg_match_all("/\<a.*href\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $linksOutput);
181 // Replace image src with base64 encoded image strings
182 if (isset($linksOutput[0]) && count($linksOutput[0]) > 0) {
183 foreach ($linksOutput[0] as $index => $linkMatch) {
184 $oldLinkString = $linkMatch;
185 $srcString = $linksOutput[2][$index];
186 if (strpos(trim($srcString), 'http') !== 0) {
187 $newSrcString = url($srcString);
188 $newLinkString = str_replace($srcString, $newSrcString, $oldLinkString);
189 $htmlContent = str_replace($oldLinkString, $newLinkString, $htmlContent);
194 // Replace any relative links with system domain
199 * Converts the page contents into simple plain text.
200 * This method filters any bad looking content to provide a nice final output.
202 public function pageToPlainText(Page $page): string
204 $html = (new PageContent($page))->render();
205 $text = strip_tags($html);
206 // Replace multiple spaces with single spaces
207 $text = preg_replace('/\ {2,}/', ' ', $text);
208 // Reduce multiple horrid whitespace characters.
209 $text = preg_replace('/(\x0A|\xA0|\x0A|\r|\n){2,}/su', "\n\n", $text);
210 $text = html_entity_decode($text);
212 $text = $page->name . "\n\n" . $text;
218 * Convert a chapter into a plain text string.
220 public function chapterToPlainText(Chapter $chapter): string
222 $text = $chapter->name . "\n\n";
223 $text .= $chapter->description . "\n\n";
224 foreach ($chapter->getVisiblePages() as $page) {
225 $text .= $this->pageToPlainText($page);
232 * Convert a book into a plain text string.
234 public function bookToPlainText(Book $book): string
236 $bookTree = (new BookContents($book))->getTree(false, false);
237 $text = $book->name . "\n\n";
238 foreach ($bookTree as $bookChild) {
239 if ($bookChild->isA('chapter')) {
240 $text .= $this->chapterToPlainText($bookChild);
242 $text .= $this->pageToPlainText($bookChild);
250 * Convert a page to a Markdown file.
252 public function pageToMarkdown(Page $page): string
254 if ($page->markdown) {
255 return '# ' . $page->name . "\n\n" . $page->markdown;
258 return '# ' . $page->name . "\n\n" . (new HtmlToMarkdown($page->html))->convert();
262 * Convert a chapter to a Markdown file.
264 public function chapterToMarkdown(Chapter $chapter): string
266 $text = '# ' . $chapter->name . "\n\n";
267 $text .= $chapter->description . "\n\n";
268 foreach ($chapter->pages as $page) {
269 $text .= $this->pageToMarkdown($page) . "\n\n";
276 * Convert a book into a plain text string.
278 public function bookToMarkdown(Book $book): string
280 $bookTree = (new BookContents($book))->getTree(false, true);
281 $text = '# ' . $book->name . "\n\n";
282 foreach ($bookTree as $bookChild) {
283 if ($bookChild instanceof Chapter) {
284 $text .= $this->chapterToMarkdown($bookChild);
286 $text .= $this->pageToMarkdown($bookChild);