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