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