11<?php namespace BookStack \Services ;
22
33use BookStack \Page ;
4+ use BookStack \Repos \EntityRepo ;
45
56class ExportService
67{
78
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+
820 /**
921 * Convert a page to a self-contained HTML file.
1022 * Includes required CSS & image content. Images are base64 encoded into the HTML.
@@ -14,7 +26,7 @@ class ExportService
1426 public function pageToContainedHtml (Page $ page )
1527 {
1628 $ cssContent = file_get_contents (public_path ('/css/export-styles.css ' ));
17- $ pageHtml = view ('pages/export ' , ['page ' => $ page , 'css ' => $ cssContent ])->render ();
29+ $ pageHtml = view ('pages/export ' , ['page ' => $ page , 'pageContent ' => $ this -> entityRepo -> renderPage ( $ page ), ' css ' => $ cssContent ])->render ();
1830 return $ this ->containHtml ($ pageHtml );
1931 }
2032
@@ -26,7 +38,8 @@ public function pageToContainedHtml(Page $page)
2638 public function pageToPdf (Page $ page )
2739 {
2840 $ cssContent = file_get_contents (public_path ('/css/export-styles.css ' ));
29- $ pageHtml = view ('pages/pdf ' , ['page ' => $ page , 'css ' => $ cssContent ])->render ();
41+ $ pageHtml = view ('pages/pdf ' , ['page ' => $ page , 'pageContent ' => $ this ->entityRepo ->renderPage ($ page ), 'css ' => $ cssContent ])->render ();
42+ // return $pageHtml;
3043 $ useWKHTML = config ('snappy.pdf.binary ' ) !== false ;
3144 $ containedHtml = $ this ->containHtml ($ pageHtml );
3245 if ($ useWKHTML ) {
@@ -59,9 +72,13 @@ protected function containHtml($htmlContent)
5972 $ pathString = $ srcString ;
6073 }
6174 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 );
75+ try {
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 ) {
80+ $ newImageString = '' ;
81+ }
6582 $ htmlContent = str_replace ($ oldImgString , $ newImageString , $ htmlContent );
6683 }
6784 }
@@ -88,14 +105,14 @@ protected function containHtml($htmlContent)
88105
89106 /**
90107 * Converts the page contents into simple plain text.
91- * This method filters any bad looking content to
92- * provide a nice final output.
108+ * This method filters any bad looking content to provide a nice final output.
93109 * @param Page $page
94110 * @return mixed
95111 */
96112 public function pageToPlainText (Page $ page )
97113 {
98- $ text = $ page ->text ;
114+ $ html = $ this ->entityRepo ->renderPage ($ page );
115+ $ text = strip_tags ($ html );
99116 // Replace multiple spaces with single spaces
100117 $ text = preg_replace ('/\ {2,}/ ' , ' ' , $ text );
101118 // Reduce multiple horrid whitespace characters.
0 commit comments