Skip to content

Commit ea2e16c

Browse files
committed
Added page HTML export
1 parent 7bcd967 commit ea2e16c

File tree

12 files changed

+263
-121
lines changed

12 files changed

+263
-121
lines changed

app/Http/Controllers/PageController.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BookStack\Http\Controllers;
44

55
use Activity;
6+
use BookStack\Services\ExportService;
67
use Illuminate\Http\Request;
78

89
use Illuminate\Support\Facades\Auth;
@@ -18,18 +19,21 @@ class PageController extends Controller
1819
protected $pageRepo;
1920
protected $bookRepo;
2021
protected $chapterRepo;
22+
protected $exportService;
2123

2224
/**
2325
* PageController constructor.
24-
* @param PageRepo $pageRepo
25-
* @param BookRepo $bookRepo
26-
* @param ChapterRepo $chapterRepo
26+
* @param PageRepo $pageRepo
27+
* @param BookRepo $bookRepo
28+
* @param ChapterRepo $chapterRepo
29+
* @param ExportService $exportService
2730
*/
28-
public function __construct(PageRepo $pageRepo, BookRepo $bookRepo, ChapterRepo $chapterRepo)
31+
public function __construct(PageRepo $pageRepo, BookRepo $bookRepo, ChapterRepo $chapterRepo, ExportService $exportService)
2932
{
3033
$this->pageRepo = $pageRepo;
3134
$this->bookRepo = $bookRepo;
3235
$this->chapterRepo = $chapterRepo;
36+
$this->exportService = $exportService;
3337
parent::__construct();
3438
}
3539

@@ -221,4 +225,30 @@ public function restoreRevision($bookSlug, $pageSlug, $revisionId)
221225
Activity::add($page, 'page_restore', $book->id);
222226
return redirect($page->getUrl());
223227
}
228+
229+
public function exportPdf($bookSlug, $pageSlug)
230+
{
231+
$book = $this->bookRepo->getBySlug($bookSlug);
232+
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
233+
$cssContent = file_get_contents(public_path('/css/styles.css'));
234+
235+
return $pdf->download($pageSlug . '.pdf');
236+
}
237+
238+
/**
239+
* Export a page to a self-contained HTML file.
240+
* @param $bookSlug
241+
* @param $pageSlug
242+
* @return \Illuminate\Http\Response
243+
*/
244+
public function exportHtml($bookSlug, $pageSlug)
245+
{
246+
$book = $this->bookRepo->getBySlug($bookSlug);
247+
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
248+
$containedHtml = $this->exportService->pageToContainedHtml($page);
249+
return response()->make($containedHtml, 200, [
250+
'Content-Type' => 'application/octet-stream',
251+
'Content-Disposition' => 'attachment; filename="'.$pageSlug.'.html'
252+
]);
253+
}
224254
}

app/Http/routes.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@
1818
Route::get('/{bookSlug}/sort', 'BookController@sort');
1919
Route::put('/{bookSlug}/sort', 'BookController@saveSort');
2020

21-
2221
// Pages
2322
Route::get('/{bookSlug}/page/create', 'PageController@create');
2423
Route::post('/{bookSlug}/page', 'PageController@store');
2524
Route::get('/{bookSlug}/page/{pageSlug}', 'PageController@show');
25+
Route::get('/{bookSlug}/page/{pageSlug}/export/pdf', 'PageController@exportPdf');
26+
Route::get('/{bookSlug}/page/{pageSlug}/export/html', 'PageController@exportHtml');
2627
Route::get('/{bookSlug}/page/{pageSlug}/edit', 'PageController@edit');
2728
Route::get('/{bookSlug}/page/{pageSlug}/delete', 'PageController@showDelete');
2829
Route::put('/{bookSlug}/page/{pageSlug}', 'PageController@update');
2930
Route::delete('/{bookSlug}/page/{pageSlug}', 'PageController@destroy');
3031

31-
//Revisions
32+
// Revisions
3233
Route::get('/{bookSlug}/page/{pageSlug}/revisions', 'PageController@showRevisions');
3334
Route::get('/{bookSlug}/page/{pageSlug}/revisions/{revId}', 'PageController@showRevision');
3435
Route::get('/{bookSlug}/page/{pageSlug}/revisions/{revId}/restore', 'PageController@restoreRevision');

app/Services/ExportService.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php namespace BookStack\Services;
2+
3+
4+
use BookStack\Page;
5+
6+
class ExportService
7+
{
8+
9+
10+
/**
11+
* Convert a page to a self-contained HTML file.
12+
* Includes required CSS & image content. Images are base64 encoded into the HTML.
13+
* @param Page $page
14+
* @return mixed|string
15+
*/
16+
public function pageToContainedHtml(Page $page)
17+
{
18+
$cssContent = file_get_contents(public_path('/css/export-styles.css'));
19+
$pageHtml = view('pages/pdf', ['page' => $page, 'css' => $cssContent])->render();
20+
21+
$imageTagsOutput = [];
22+
preg_match_all("/\<img.*src\=(\'|\")(.*?)(\'|\").*?\>/i", $pageHtml, $imageTagsOutput);
23+
24+
// Replace image src with base64 encoded image strings
25+
if (isset($imageTagsOutput[0]) && count($imageTagsOutput[0]) > 0) {
26+
foreach ($imageTagsOutput[0] as $index => $imgMatch) {
27+
$oldImgString = $imgMatch;
28+
$srcString = $imageTagsOutput[2][$index];
29+
if (strpos(trim($srcString), 'http') !== 0) {
30+
$pathString = public_path($srcString);
31+
} else {
32+
$pathString = $srcString;
33+
}
34+
$imageContent = file_get_contents($pathString);
35+
$imageEncoded = 'data:image/' . pathinfo($pathString, PATHINFO_EXTENSION) . ';base64,' . base64_encode($imageContent);
36+
$newImageString = str_replace($srcString, $imageEncoded, $oldImgString);
37+
$pageHtml = str_replace($oldImgString, $newImageString, $pageHtml);
38+
}
39+
}
40+
41+
$linksOutput = [];
42+
preg_match_all("/\<a.*href\=(\'|\")(.*?)(\'|\").*?\>/i", $pageHtml, $linksOutput);
43+
44+
// Replace image src with base64 encoded image strings
45+
if (isset($linksOutput[0]) && count($linksOutput[0]) > 0) {
46+
foreach ($linksOutput[0] as $index => $linkMatch) {
47+
$oldLinkString = $linkMatch;
48+
$srcString = $linksOutput[2][$index];
49+
if (strpos(trim($srcString), 'http') !== 0) {
50+
$newSrcString = url($srcString);
51+
$newLinkString = str_replace($srcString, $newSrcString, $oldLinkString);
52+
$pageHtml = str_replace($oldLinkString, $newLinkString, $pageHtml);
53+
}
54+
}
55+
}
56+
57+
// Replace any relative links with system domain
58+
return $pageHtml;
59+
}
60+
61+
}

composer.lock

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gulpfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ elixir.extend('queryVersion', function(inputFiles) {
2121
elixir(function(mix) {
2222
mix.sass('styles.scss')
2323
.sass('print-styles.scss')
24+
.sass('export-styles.scss')
2425
.browserify('global.js', 'public/js/common.js')
2526
.queryVersion(['css/styles.css', 'css/print-styles.css', 'js/common.js']);
2627
});

resources/assets/sass/_fonts.scss

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* Generated by Font Squirrel (http://www.fontsquirrel.com) on December 30, 2015 */
2+
@font-face {
3+
font-family: 'Roboto';
4+
src: url('/fonts/roboto-bold-webfont.eot');
5+
src: url('/fonts/roboto-bold-webfont.eot?#iefix') format('embedded-opentype'),
6+
url('/fonts/roboto-bold-webfont.woff2') format('woff2'),
7+
url('/fonts/roboto-bold-webfont.woff') format('woff'),
8+
url('/fonts/roboto-bold-webfont.ttf') format('truetype'),
9+
url('/fonts/roboto-bold-webfont.svg#robotobold') format('svg');
10+
font-weight: bold;
11+
font-style: normal;
12+
}
13+
14+
@font-face {
15+
font-family: 'Roboto';
16+
src: url('/fonts/roboto-bolditalic-webfont.eot');
17+
src: url('/fonts/roboto-bolditalic-webfont.eot?#iefix') format('embedded-opentype'),
18+
url('/fonts/roboto-bolditalic-webfont.woff2') format('woff2'),
19+
url('/fonts/roboto-bolditalic-webfont.woff') format('woff'),
20+
url('/fonts/roboto-bolditalic-webfont.ttf') format('truetype'),
21+
url('/fonts/roboto-bolditalic-webfont.svg#robotobold_italic') format('svg');
22+
font-weight: bold;
23+
font-style: italic;
24+
}
25+
26+
@font-face {
27+
font-family: 'Roboto';
28+
src: url('/fonts/roboto-italic-webfont.eot');
29+
src: url('/fonts/roboto-italic-webfont.eot?#iefix') format('embedded-opentype'),
30+
url('/fonts/roboto-italic-webfont.woff2') format('woff2'),
31+
url('/fonts/roboto-italic-webfont.woff') format('woff'),
32+
url('/fonts/roboto-italic-webfont.ttf') format('truetype'),
33+
url('/fonts/roboto-italic-webfont.svg#robotoitalic') format('svg');
34+
font-weight: normal;
35+
font-style: italic;
36+
}
37+
38+
@font-face {
39+
font-family: 'Roboto';
40+
src: url('/fonts/roboto-light-webfont.eot');
41+
src: url('/fonts/roboto-light-webfont.eot?#iefix') format('embedded-opentype'),
42+
url('/fonts/roboto-light-webfont.woff2') format('woff2'),
43+
url('/fonts/roboto-light-webfont.woff') format('woff'),
44+
url('/fonts/roboto-light-webfont.ttf') format('truetype'),
45+
url('/fonts/roboto-light-webfont.svg#robotolight') format('svg');
46+
font-weight: 300;
47+
font-style: normal;
48+
}
49+
50+
@font-face {
51+
font-family: 'Roboto';
52+
src: url('/fonts/roboto-lightitalic-webfont.eot');
53+
src: url('/fonts/roboto-lightitalic-webfont.eot?#iefix') format('embedded-opentype'),
54+
url('/fonts/roboto-lightitalic-webfont.woff2') format('woff2'),
55+
url('/fonts/roboto-lightitalic-webfont.woff') format('woff'),
56+
url('/fonts/roboto-lightitalic-webfont.ttf') format('truetype'),
57+
url('/fonts/roboto-lightitalic-webfont.svg#robotolight_italic') format('svg');
58+
font-weight: 300;
59+
font-style: italic;
60+
}
61+
62+
@font-face {
63+
font-family: 'Roboto';
64+
src: url('/fonts/roboto-medium-webfont.eot');
65+
src: url('/fonts/roboto-medium-webfont.eot?#iefix') format('embedded-opentype'),
66+
url('/fonts/roboto-medium-webfont.woff2') format('woff2'),
67+
url('/fonts/roboto-medium-webfont.woff') format('woff'),
68+
url('/fonts/roboto-medium-webfont.ttf') format('truetype'),
69+
url('/fonts/roboto-medium-webfont.svg#robotomedium') format('svg');
70+
font-weight: 500;
71+
font-style: normal;
72+
}
73+
74+
@font-face {
75+
font-family: 'Roboto';
76+
src: url('/fonts/roboto-mediumitalic-webfont.eot');
77+
src: url('/fonts/roboto-mediumitalic-webfont.eot?#iefix') format('embedded-opentype'),
78+
url('/fonts/roboto-mediumitalic-webfont.woff2') format('woff2'),
79+
url('/fonts/roboto-mediumitalic-webfont.woff') format('woff'),
80+
url('/fonts/roboto-mediumitalic-webfont.ttf') format('truetype'),
81+
url('/fonts/roboto-mediumitalic-webfont.svg#robotomedium_italic') format('svg');
82+
font-weight: 500;
83+
font-style: italic;
84+
}
85+
86+
@font-face {
87+
font-family: 'Roboto';
88+
src: url('/fonts/roboto-regular-webfont.eot');
89+
src: url('/fonts/roboto-regular-webfont.eot?#iefix') format('embedded-opentype'),
90+
url('/fonts/roboto-regular-webfont.woff2') format('woff2'),
91+
url('/fonts/roboto-regular-webfont.woff') format('woff'),
92+
url('/fonts/roboto-regular-webfont.ttf') format('truetype'),
93+
url('/fonts/roboto-regular-webfont.svg#robotoregular') format('svg');
94+
font-weight: normal;
95+
font-style: normal;
96+
}

resources/assets/sass/_header.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ form.search-box {
187187
}
188188

189189
.faded {
190-
a, button, span {
190+
a, button, span, span > div {
191191
color: #666;
192192
}
193193
.text-button {

0 commit comments

Comments
 (0)