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();
43 $useWKHTML = config('snappy.pdf.binary') !== false;
44 $containedHtml = $this->containHtml($pageHtml);
46 $pdf = \SnappyPDF::loadHTML($containedHtml);
48 $pdf = \PDF::loadHTML($containedHtml);
50 return $pdf->output();
54 * Bundle of the contents of a html file to be self-contained.
56 * @return mixed|string
58 protected function containHtml($htmlContent)
60 $imageTagsOutput = [];
61 preg_match_all("/\<img.*src\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $imageTagsOutput);
63 // Replace image src with base64 encoded image strings
64 if (isset($imageTagsOutput[0]) && count($imageTagsOutput[0]) > 0) {
65 foreach ($imageTagsOutput[0] as $index => $imgMatch) {
66 $oldImgString = $imgMatch;
67 $srcString = $imageTagsOutput[2][$index];
68 $isLocal = strpos(trim($srcString), 'http') !== 0;
70 $pathString = public_path(trim($srcString, '/'));
72 $pathString = $srcString;
74 if ($isLocal && !file_exists($pathString)) continue;
76 $imageContent = file_get_contents($pathString);
77 $imageEncoded = 'data:image/' . pathinfo($pathString, PATHINFO_EXTENSION) . ';base64,' . base64_encode($imageContent);
78 $newImageString = str_replace($srcString, $imageEncoded, $oldImgString);
79 } catch (\ErrorException $e) {
82 $htmlContent = str_replace($oldImgString, $newImageString, $htmlContent);
87 preg_match_all("/\<a.*href\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $linksOutput);
89 // Replace image src with base64 encoded image strings
90 if (isset($linksOutput[0]) && count($linksOutput[0]) > 0) {
91 foreach ($linksOutput[0] as $index => $linkMatch) {
92 $oldLinkString = $linkMatch;
93 $srcString = $linksOutput[2][$index];
94 if (strpos(trim($srcString), 'http') !== 0) {
95 $newSrcString = url($srcString);
96 $newLinkString = str_replace($srcString, $newSrcString, $oldLinkString);
97 $htmlContent = str_replace($oldLinkString, $newLinkString, $htmlContent);
102 // Replace any relative links with system domain
107 * Converts the page contents into simple plain text.
108 * This method filters any bad looking content to provide a nice final output.
112 public function pageToPlainText(Page $page)
114 $html = $this->entityRepo->renderPage($page);
115 $text = strip_tags($html);
116 // Replace multiple spaces with single spaces
117 $text = preg_replace('/\ {2,}/', ' ', $text);
118 // Reduce multiple horrid whitespace characters.
119 $text = preg_replace('/(\x0A|\xA0|\x0A|\r|\n){2,}/su', "\n\n", $text);
120 $text = html_entity_decode($text);
122 $text = $page->name . "\n\n" . $text;