1 <?php namespace BookStack\Services;
9 * Convert a page to a self-contained HTML file.
10 * Includes required CSS & image content. Images are base64 encoded into the HTML.
12 * @return mixed|string
14 public function pageToContainedHtml(Page $page)
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);
22 * Convert a page to a pdf file.
24 * @return mixed|string
26 public function pageToPdf(Page $page)
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);
33 $pdf = \SnappyPDF::loadHTML($containedHtml);
35 $pdf = \PDF::loadHTML($containedHtml);
37 return $pdf->output();
41 * Bundle of the contents of a html file to be self-contained.
43 * @return mixed|string
45 protected function containHtml($htmlContent)
47 $imageTagsOutput = [];
48 preg_match_all("/\<img.*src\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $imageTagsOutput);
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;
57 $pathString = public_path(trim($srcString, '/'));
59 $pathString = $srcString;
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);
70 preg_match_all("/\<a.*href\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $linksOutput);
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);
85 // Replace any relative links with system domain
90 * Converts the page contents into simple plain text.
91 * This method filters any bad looking content to
92 * provide a nice final output.
96 public function pageToPlainText(Page $page)
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);
105 $text = $page->name . "\n\n" . $text;