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