]> BookStack Code Mirror - bookstack/blob - app/Services/ExportService.php
Fixes typo causing the message not to be displayed
[bookstack] / app / Services / ExportService.php
1 <?php namespace BookStack\Services;
2
3 use BookStack\Page;
4
5 class ExportService
6 {
7
8     /**
9      * Convert a page to a self-contained HTML file.
10      * Includes required CSS & image content. Images are base64 encoded into the HTML.
11      * @param Page $page
12      * @return mixed|string
13      */
14     public function pageToContainedHtml(Page $page)
15     {
16         $cssContent = file_get_contents(public_path('/css/export-styles.css'));
17         $pageHtml = view('pages/export', ['page' => $page, 'css' => $cssContent])->render();
18         return $this->containHtml($pageHtml);
19     }
20
21     /**
22      * Convert a page to a pdf file.
23      * @param Page $page
24      * @return mixed|string
25      */
26     public function pageToPdf(Page $page)
27     {
28         $cssContent = file_get_contents(public_path('/css/export-styles.css'));
29         $pageHtml = view('pages/pdf', ['page' => $page, 'css' => $cssContent])->render();
30         $useWKHTML = config('snappy.pdf.binary') !== false;
31         $containedHtml = $this->containHtml($pageHtml);
32         if ($useWKHTML) {
33             $pdf = \SnappyPDF::loadHTML($containedHtml);
34         } else {
35             $pdf = \PDF::loadHTML($containedHtml);
36         }
37         return $pdf->output();
38     }
39
40     /**
41      * Bundle of the contents of a html file to be self-contained.
42      * @param $htmlContent
43      * @return mixed|string
44      */
45     protected function containHtml($htmlContent)
46     {
47         $imageTagsOutput = [];
48         preg_match_all("/\<img.*src\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $imageTagsOutput);
49
50         // Replace image src with base64 encoded image strings
51         if (isset($imageTagsOutput[0]) && count($imageTagsOutput[0]) > 0) {
52             foreach ($imageTagsOutput[0] as $index => $imgMatch) {
53                 $oldImgString = $imgMatch;
54                 $srcString = $imageTagsOutput[2][$index];
55                 $isLocal = strpos(trim($srcString), 'http') !== 0;
56                 if ($isLocal) {
57                     $pathString = public_path(trim($srcString, '/'));
58                 } else {
59                     $pathString = $srcString;
60                 }
61                 if ($isLocal && !file_exists($pathString)) continue;
62                 $imageContent = file_get_contents($pathString);
63                 $imageEncoded = 'data:image/' . pathinfo($pathString, PATHINFO_EXTENSION) . ';base64,' . base64_encode($imageContent);
64                 $newImageString = str_replace($srcString, $imageEncoded, $oldImgString);
65                 $htmlContent = str_replace($oldImgString, $newImageString, $htmlContent);
66             }
67         }
68
69         $linksOutput = [];
70         preg_match_all("/\<a.*href\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $linksOutput);
71
72         // Replace image src with base64 encoded image strings
73         if (isset($linksOutput[0]) && count($linksOutput[0]) > 0) {
74             foreach ($linksOutput[0] as $index => $linkMatch) {
75                 $oldLinkString = $linkMatch;
76                 $srcString = $linksOutput[2][$index];
77                 if (strpos(trim($srcString), 'http') !== 0) {
78                     $newSrcString = url($srcString);
79                     $newLinkString = str_replace($srcString, $newSrcString, $oldLinkString);
80                     $htmlContent = str_replace($oldLinkString, $newLinkString, $htmlContent);
81                 }
82             }
83         }
84
85         // Replace any relative links with system domain
86         return $htmlContent;
87     }
88
89     /**
90      * Converts the page contents into simple plain text.
91      * This method filters any bad looking content to
92      * provide a nice final output.
93      * @param Page $page
94      * @return mixed
95      */
96     public function pageToPlainText(Page $page)
97     {
98         $text = $page->text;
99         // Replace multiple spaces with single spaces
100         $text = preg_replace('/\ {2,}/', ' ', $text);
101         // Reduce multiple horrid whitespace characters.
102         $text = preg_replace('/(\x0A|\xA0|\x0A|\r|\n){2,}/su', "\n\n", $text);
103         $text = html_entity_decode($text);
104         // Add title
105         $text = $page->name . "\n\n" . $text;
106         return $text;
107     }
108
109 }
110
111
112
113
114
115
116
117
118
119
120
121