|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PhpEpub\Converters; |
|
6
|
|
|
|
|
7
|
|
|
use PhpEpub\Exception; |
|
8
|
|
|
use TCPDF; |
|
9
|
|
|
|
|
10
|
|
|
class TCPDFAdapter implements ConverterInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var array<string, mixed> |
|
14
|
|
|
*/ |
|
15
|
|
|
private array $styles; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* TCPDFAdapter constructor. |
|
19
|
|
|
* |
|
20
|
|
|
* @param array<string, mixed> $styles Optional styling parameters. |
|
21
|
|
|
*/ |
|
22
|
2 |
|
public function __construct(array $styles = []) |
|
23
|
|
|
{ |
|
24
|
|
|
// Default styling parameters |
|
25
|
2 |
|
$defaultStyles = [ |
|
26
|
2 |
|
'font' => 'helvetica', |
|
27
|
2 |
|
'font_size' => 12, |
|
28
|
2 |
|
'margin_left' => 15, |
|
29
|
2 |
|
'margin_top' => 27, |
|
30
|
2 |
|
'margin_right' => 15, |
|
31
|
2 |
|
'margin_bottom' => 25, |
|
32
|
2 |
|
'header' => true, |
|
33
|
2 |
|
'footer' => true, |
|
34
|
2 |
|
]; |
|
35
|
|
|
|
|
36
|
|
|
// Merge default styles with user-provided styles |
|
37
|
2 |
|
$this->styles = array_merge($defaultStyles, $styles); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Converts the EPUB content to a PDF using TCPDF. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $epubDirectory The directory containing the extracted EPUB contents. |
|
44
|
|
|
* @param string $outputPath The path where the converted PDF should be saved. |
|
45
|
|
|
* |
|
46
|
|
|
* @throws Exception If the conversion fails. |
|
47
|
|
|
*/ |
|
48
|
2 |
|
public function convert(string $epubDirectory, string $outputPath): void |
|
49
|
|
|
{ |
|
50
|
|
|
// Initialize TCPDF |
|
51
|
2 |
|
$pdf = new TCPDF(); |
|
52
|
|
|
|
|
53
|
|
|
// Set document information |
|
54
|
2 |
|
$pdf->SetCreator(PDF_CREATOR); |
|
55
|
2 |
|
$pdf->SetAuthor('Author Name'); // This should be extracted from EPUB metadata |
|
56
|
2 |
|
$pdf->SetTitle('EPUB to PDF Conversion'); |
|
57
|
2 |
|
$pdf->SetSubject('EPUB Conversion'); |
|
58
|
2 |
|
$pdf->SetKeywords('EPUB, PDF, conversion'); |
|
59
|
|
|
|
|
60
|
|
|
// Set default header and footer |
|
61
|
2 |
|
if ($this->styles['header']) { |
|
62
|
2 |
|
$pdf->setHeaderData('', 0, 'EPUB to PDF', 'Generated by TCPDF'); |
|
63
|
|
|
} else { |
|
64
|
|
|
$pdf->setPrintHeader(false); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
if ($this->styles['footer']) { |
|
68
|
2 |
|
$pdf->setFooterData(); |
|
69
|
|
|
} else { |
|
70
|
|
|
$pdf->setPrintFooter(false); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
$marginLeft = 15; |
|
74
|
2 |
|
if (isset($this->styles['margin_left']) && is_int($this->styles['margin_left'])) { |
|
75
|
2 |
|
$marginLeft = $this->styles['margin_left']; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
$marginTop = 27; |
|
79
|
2 |
|
if (isset($this->styles['margin_top']) && is_int($this->styles['margin_top'])) { |
|
80
|
2 |
|
$marginTop = $this->styles['margin_top']; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
2 |
|
$marginRight = 15; |
|
84
|
2 |
|
if (isset($this->styles['margin_right']) && is_int($this->styles['margin_right'])) { |
|
85
|
2 |
|
$marginRight = $this->styles['margin_right']; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
2 |
|
$marginBottom = 27; |
|
89
|
2 |
|
if (isset($this->styles['margin_bottom']) && is_int($this->styles['margin_bottom'])) { |
|
90
|
2 |
|
$marginBottom = $this->styles['margin_bottom']; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// Set margins |
|
94
|
2 |
|
$pdf->SetMargins($marginLeft, $marginTop, $marginRight); |
|
95
|
2 |
|
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER); |
|
96
|
2 |
|
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER); |
|
97
|
2 |
|
$pdf->SetAutoPageBreak(true, $marginBottom); |
|
98
|
|
|
|
|
99
|
2 |
|
$font = 'helvetica'; |
|
100
|
2 |
|
if (isset($this->styles['font']) && is_string($this->styles['font'])) { |
|
101
|
2 |
|
$font = $this->styles['font']; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
2 |
|
$fontSize = 27; |
|
105
|
2 |
|
if (isset($this->styles['font_size']) && is_int($this->styles['font_size'])) { |
|
106
|
2 |
|
$fontSize = $this->styles['font_size']; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// Set font |
|
110
|
2 |
|
$pdf->SetFont($font, '', $fontSize); |
|
111
|
|
|
|
|
112
|
|
|
// Add a page |
|
113
|
2 |
|
$pdf->AddPage(); |
|
114
|
|
|
|
|
115
|
|
|
// Load EPUB content (this is a simplified example) |
|
116
|
2 |
|
$content = $this->loadEpubContent($epubDirectory); |
|
117
|
|
|
|
|
118
|
|
|
// Write content to PDF |
|
119
|
1 |
|
$pdf->writeHTML($content, true, false, true, false, ''); |
|
120
|
|
|
|
|
121
|
|
|
// Output PDF to file |
|
122
|
1 |
|
$pdf->Output($outputPath, 'F'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Loads the EPUB content for conversion. |
|
127
|
|
|
* |
|
128
|
|
|
* @throws Exception If the content cannot be loaded. |
|
129
|
|
|
*/ |
|
130
|
2 |
|
private function loadEpubContent(string $epubDirectory): string |
|
131
|
|
|
{ |
|
132
|
|
|
// Simplified example: Load content from a specific file |
|
133
|
2 |
|
$contentFile = $epubDirectory . '/content.xhtml'; // Adjust this path as needed |
|
134
|
2 |
|
if (! file_exists($contentFile)) { |
|
135
|
1 |
|
throw new Exception("Content file not found: {$contentFile}"); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
1 |
|
$content = file_get_contents($contentFile); |
|
139
|
1 |
|
if ($content === false) { |
|
140
|
|
|
throw new Exception("Failed to read content from: {$contentFile}"); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
1 |
|
return $content; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|