1 <?php namespace BookStack\Services;
4 use BookStack\Repos\EntityRepo;
12 * ExportService constructor.
15 public function __construct(EntityRepo $entityRepo)
17 $this->entityRepo = $entityRepo;
21 * Convert a page to a self-contained HTML file.
22 * Includes required CSS & image content. Images are base64 encoded into the HTML.
24 * @return mixed|string
26 public function pageToContainedHtml(Page $page)
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);
34 * Convert a page to a pdf file.
36 * @return mixed|string
38 public function pageToPdf(Page $page)
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);
45 $pdf = \SnappyPDF::loadHTML($containedHtml);
47 $pdf = \PDF::loadHTML($containedHtml);
49 return $pdf->output();
53 * Bundle of the contents of a html file to be self-contained.
55 * @return mixed|string
57 protected function containHtml($htmlContent)
59 $imageTagsOutput = [];
60 preg_match_all("/\<img.*src\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $imageTagsOutput);
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;
69 $pathString = public_path(trim($srcString, '/'));
71 $pathString = $srcString;
73 if ($isLocal && !file_exists($pathString)) continue;
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) {
81 $htmlContent = str_replace($oldImgString, $newImageString, $htmlContent);
86 preg_match_all("/\<a.*href\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $linksOutput);
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);
101 // Replace any relative links with system domain
106 * Converts the page contents into simple plain text.
107 * This method filters any bad looking content to provide a nice final output.
111 public function pageToPlainText(Page $page)
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);
121 $text = $page->name . "\n\n" . $text;