|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the trefoil application. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Miguel Angel Gabriel <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Trefoil\Publishers; |
|
13
|
|
|
|
|
14
|
|
|
use ZendPdf\PdfDocument; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* It publishes the book as a PDF file. All the internal links are transformed |
|
18
|
|
|
* into clickable cross-section book links. These links even display automatically |
|
19
|
|
|
* the page number where they point into, so no information is lost when printing |
|
20
|
|
|
* the book. |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class PdfPublisher extends BasePublisher |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
public function loadContents() |
|
26
|
|
|
{ |
|
27
|
|
|
parent::loadContents(); |
|
28
|
|
|
|
|
29
|
|
|
// if the book includes its own cover as a PDF file, |
|
30
|
|
|
// remove the default 'cover' element to prevent |
|
31
|
|
|
// publishing a book with two covers |
|
32
|
|
|
if (null !== $this->getCustomCover()) { |
|
33
|
|
|
$bookItems = array(); |
|
34
|
|
|
|
|
35
|
|
|
// remove any element of type 'cover' from the |
|
36
|
|
|
// publishing items |
|
37
|
|
|
foreach ($this->app['publishing.items'] as $item) { |
|
38
|
|
|
if ('cover' != $item['config']['element']) { |
|
39
|
|
|
$bookItems[] = $item; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$this->app['publishing.items'] = $bookItems; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* If the book defines a custom PDF cover, this method prepends it |
|
49
|
|
|
* to the PDF book. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $bookFilePath The path of the original PDF book without the cover |
|
52
|
|
|
* @param string $coverFilePath The path of the PDF file which will be displayed as the cover of the book |
|
53
|
|
|
* |
|
54
|
|
|
* @protected (set to public to be able to unit test it) |
|
55
|
|
|
*/ |
|
56
|
|
View Code Duplication |
public function addBookCover($bookFilePath, $coverFilePath) |
|
57
|
|
|
{ |
|
58
|
|
|
if (!empty($coverFilePath)) { |
|
59
|
|
|
$pdfBook = PdfDocument::load($bookFilePath); |
|
60
|
|
|
$pdfCover = PdfDocument::load($coverFilePath); |
|
61
|
|
|
|
|
62
|
|
|
$pdfCover = clone $pdfCover->pages[0]; |
|
63
|
|
|
array_unshift($pdfBook->pages, $pdfCover); |
|
64
|
|
|
|
|
65
|
|
|
$pdfBook->save($bookFilePath, true); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* It looks for custom book cover PDF. The search order is: |
|
71
|
|
|
* 1. <book>/Resources/Templates/<edition-name>/cover.pdf |
|
72
|
|
|
* 2. <book>/Resources/Templates/pdf/cover.pdf |
|
73
|
|
|
* 3. <book>/Resources/Templates/cover.pdf |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $coverFileName The name of the PDF file that defines the book cover |
|
76
|
|
|
* |
|
77
|
|
|
* @return null|string The filePath of the PDF cover or null if none exists |
|
78
|
|
|
* |
|
79
|
|
|
* @protected (set to public to be able to unit test it) |
|
80
|
|
|
*/ |
|
81
|
|
View Code Duplication |
public function getCustomCover($coverFileName = 'cover.pdf') |
|
82
|
|
|
{ |
|
83
|
|
|
$paths = array( |
|
84
|
|
|
$this->app['publishing.dir.templates'] . '/' . $this->app['publishing.edition'], |
|
85
|
|
|
$this->app['publishing.dir.templates'] . '/pdf', |
|
86
|
|
|
$this->app['publishing.dir.templates'], |
|
87
|
|
|
); |
|
88
|
|
|
|
|
89
|
|
|
return $this->app->getFirstExistingFile($coverFileName, $paths); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|