]> BookStack Code Mirror - bookstack/blob - app/Entities/ExportService.php
Re-structured the app code to be feature based rather than code type based
[bookstack] / app / Entities / ExportService.php
1 <?php namespace BookStack\Entities;
2
3 use BookStack\Entities\Book;
4 use BookStack\Entities\Chapter;
5 use BookStack\Entities\Page;
6 use BookStack\Entities\EntityRepo;
7 use BookStack\Uploads\ImageService;
8
9 class ExportService
10 {
11
12     protected $entityRepo;
13     protected $imageService;
14
15     /**
16      * ExportService constructor.
17      * @param $entityRepo
18      */
19     public function __construct(EntityRepo $entityRepo, ImageService $imageService)
20     {
21         $this->entityRepo = $entityRepo;
22         $this->imageService = $imageService;
23     }
24
25     /**
26      * Convert a page to a self-contained HTML file.
27      * Includes required CSS & image content. Images are base64 encoded into the HTML.
28      * @param \BookStack\Entities\Page $page
29      * @return mixed|string
30      * @throws \Throwable
31      */
32     public function pageToContainedHtml(Page $page)
33     {
34         $this->entityRepo->renderPage($page);
35         $pageHtml = view('pages/export', [
36             'page' => $page
37         ])->render();
38         return $this->containHtml($pageHtml);
39     }
40
41     /**
42      * Convert a chapter to a self-contained HTML file.
43      * @param \BookStack\Entities\Chapter $chapter
44      * @return mixed|string
45      * @throws \Throwable
46      */
47     public function chapterToContainedHtml(Chapter $chapter)
48     {
49         $pages = $this->entityRepo->getChapterChildren($chapter);
50         $pages->each(function ($page) {
51             $page->html = $this->entityRepo->renderPage($page);
52         });
53         $html = view('chapters/export', [
54             'chapter' => $chapter,
55             'pages' => $pages
56         ])->render();
57         return $this->containHtml($html);
58     }
59
60     /**
61      * Convert a book to a self-contained HTML file.
62      * @param Book $book
63      * @return mixed|string
64      * @throws \Throwable
65      */
66     public function bookToContainedHtml(Book $book)
67     {
68         $bookTree = $this->entityRepo->getBookChildren($book, true, true);
69         $html = view('books/export', [
70             'book' => $book,
71             'bookChildren' => $bookTree
72         ])->render();
73         return $this->containHtml($html);
74     }
75
76     /**
77      * Convert a page to a PDF file.
78      * @param Page $page
79      * @return mixed|string
80      * @throws \Throwable
81      */
82     public function pageToPdf(Page $page)
83     {
84         $this->entityRepo->renderPage($page);
85         $html = view('pages/pdf', [
86             'page' => $page
87         ])->render();
88         return $this->htmlToPdf($html);
89     }
90
91     /**
92      * Convert a chapter to a PDF file.
93      * @param \BookStack\Entities\Chapter $chapter
94      * @return mixed|string
95      * @throws \Throwable
96      */
97     public function chapterToPdf(Chapter $chapter)
98     {
99         $pages = $this->entityRepo->getChapterChildren($chapter);
100         $pages->each(function ($page) {
101             $page->html = $this->entityRepo->renderPage($page);
102         });
103         $html = view('chapters/export', [
104             'chapter' => $chapter,
105             'pages' => $pages
106         ])->render();
107         return $this->htmlToPdf($html);
108     }
109
110     /**
111      * Convert a book to a PDF file
112      * @param \BookStack\Entities\Book $book
113      * @return string
114      * @throws \Throwable
115      */
116     public function bookToPdf(Book $book)
117     {
118         $bookTree = $this->entityRepo->getBookChildren($book, true, true);
119         $html = view('books/export', [
120             'book' => $book,
121             'bookChildren' => $bookTree
122         ])->render();
123         return $this->htmlToPdf($html);
124     }
125
126     /**
127      * Convert normal webpage HTML to a PDF.
128      * @param $html
129      * @return string
130      * @throws \Exception
131      */
132     protected function htmlToPdf($html)
133     {
134         $containedHtml = $this->containHtml($html);
135         $useWKHTML = config('snappy.pdf.binary') !== false;
136         if ($useWKHTML) {
137             $pdf = \SnappyPDF::loadHTML($containedHtml);
138             $pdf->setOption('print-media-type', true);
139         } else {
140             $pdf = \DomPDF::loadHTML($containedHtml);
141         }
142         return $pdf->output();
143     }
144
145     /**
146      * Bundle of the contents of a html file to be self-contained.
147      * @param $htmlContent
148      * @return mixed|string
149      * @throws \Exception
150      */
151     protected function containHtml($htmlContent)
152     {
153         $imageTagsOutput = [];
154         preg_match_all("/\<img.*src\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $imageTagsOutput);
155
156         // Replace image src with base64 encoded image strings
157         if (isset($imageTagsOutput[0]) && count($imageTagsOutput[0]) > 0) {
158             foreach ($imageTagsOutput[0] as $index => $imgMatch) {
159                 $oldImgTagString = $imgMatch;
160                 $srcString = $imageTagsOutput[2][$index];
161                 $imageEncoded = $this->imageService->imageUriToBase64($srcString);
162                 if ($imageEncoded === null) {
163                     $imageEncoded = $srcString;
164                 }
165                 $newImgTagString = str_replace($srcString, $imageEncoded, $oldImgTagString);
166                 $htmlContent = str_replace($oldImgTagString, $newImgTagString, $htmlContent);
167             }
168         }
169
170         $linksOutput = [];
171         preg_match_all("/\<a.*href\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $linksOutput);
172
173         // Replace image src with base64 encoded image strings
174         if (isset($linksOutput[0]) && count($linksOutput[0]) > 0) {
175             foreach ($linksOutput[0] as $index => $linkMatch) {
176                 $oldLinkString = $linkMatch;
177                 $srcString = $linksOutput[2][$index];
178                 if (strpos(trim($srcString), 'http') !== 0) {
179                     $newSrcString = url($srcString);
180                     $newLinkString = str_replace($srcString, $newSrcString, $oldLinkString);
181                     $htmlContent = str_replace($oldLinkString, $newLinkString, $htmlContent);
182                 }
183             }
184         }
185
186         // Replace any relative links with system domain
187         return $htmlContent;
188     }
189
190     /**
191      * Converts the page contents into simple plain text.
192      * This method filters any bad looking content to provide a nice final output.
193      * @param Page $page
194      * @return mixed
195      */
196     public function pageToPlainText(Page $page)
197     {
198         $html = $this->entityRepo->renderPage($page);
199         $text = strip_tags($html);
200         // Replace multiple spaces with single spaces
201         $text = preg_replace('/\ {2,}/', ' ', $text);
202         // Reduce multiple horrid whitespace characters.
203         $text = preg_replace('/(\x0A|\xA0|\x0A|\r|\n){2,}/su', "\n\n", $text);
204         $text = html_entity_decode($text);
205         // Add title
206         $text = $page->name . "\n\n" . $text;
207         return $text;
208     }
209
210     /**
211      * Convert a chapter into a plain text string.
212      * @param \BookStack\Entities\Chapter $chapter
213      * @return string
214      */
215     public function chapterToPlainText(Chapter $chapter)
216     {
217         $text = $chapter->name . "\n\n";
218         $text .= $chapter->description . "\n\n";
219         foreach ($chapter->pages as $page) {
220             $text .= $this->pageToPlainText($page);
221         }
222         return $text;
223     }
224
225     /**
226      * Convert a book into a plain text string.
227      * @param Book $book
228      * @return string
229      */
230     public function bookToPlainText(Book $book)
231     {
232         $bookTree = $this->entityRepo->getBookChildren($book, true, true);
233         $text = $book->name . "\n\n";
234         foreach ($bookTree as $bookChild) {
235             if ($bookChild->isA('chapter')) {
236                 $text .= $this->chapterToPlainText($bookChild);
237             } else {
238                 $text .= $this->pageToPlainText($bookChild);
239             }
240         }
241         return $text;
242     }
243 }