1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpEpub\Converters; |
6
|
|
|
|
7
|
|
|
use Dompdf\Dompdf; |
8
|
|
|
use Dompdf\Options; |
9
|
|
|
use PhpEpub\Exception; |
10
|
|
|
|
11
|
|
|
class DompdfAdapter implements ConverterInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array<string, mixed> |
15
|
|
|
*/ |
16
|
|
|
private array $styles; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* DompdfAdapter constructor. |
20
|
|
|
* |
21
|
|
|
* @param array<string, mixed> $styles Optional styling parameters. |
22
|
|
|
*/ |
23
|
2 |
|
public function __construct(array $styles = []) |
24
|
|
|
{ |
25
|
|
|
// Default styling parameters |
26
|
2 |
|
$defaultStyles = [ |
27
|
2 |
|
'font' => 'Arial', |
28
|
2 |
|
'font_size' => 12, |
29
|
2 |
|
'paper_size' => 'A4', |
30
|
2 |
|
'orientation' => 'portrait', |
31
|
2 |
|
]; |
32
|
|
|
|
33
|
|
|
// Merge default styles with user-provided styles |
34
|
2 |
|
$this->styles = array_merge($defaultStyles, $styles); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Converts the EPUB content to a PDF using Dompdf. |
39
|
|
|
* |
40
|
|
|
* @param string $epubDirectory The directory containing the extracted EPUB contents. |
41
|
|
|
* @param string $outputPath The path where the converted PDF should be saved. |
42
|
|
|
* |
43
|
|
|
* @throws Exception If the conversion fails. |
44
|
|
|
*/ |
45
|
2 |
|
public function convert(string $epubDirectory, string $outputPath): void |
46
|
|
|
{ |
47
|
|
|
// Initialize Dompdf with options |
48
|
2 |
|
$options = new Options(); |
49
|
2 |
|
$options->set('defaultFont', $this->styles['font']); |
50
|
2 |
|
$dompdf = new Dompdf($options); |
51
|
|
|
|
52
|
|
|
// Load EPUB content (this is a simplified example) |
53
|
2 |
|
$content = $this->loadEpubContent($epubDirectory); |
54
|
|
|
|
55
|
|
|
// Load HTML content into Dompdf |
56
|
1 |
|
$dompdf->loadHtml($content); |
57
|
|
|
|
58
|
1 |
|
$paperSize = 'A4'; |
59
|
1 |
|
if (isset($this->styles['paper_size']) && is_string($this->styles['paper_size'])) { |
60
|
1 |
|
$paperSize = $this->styles['paper_size']; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
$orientation = 'portrait'; |
64
|
1 |
|
if (isset($this->styles['orientation']) && is_string($this->styles['orientation'])) { |
65
|
1 |
|
$orientation = $this->styles['orientation']; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Set paper size and orientation |
69
|
1 |
|
$dompdf->setPaper($paperSize, $orientation); |
70
|
|
|
|
71
|
|
|
// Render the PDF |
72
|
1 |
|
$dompdf->render(); |
73
|
|
|
|
74
|
|
|
// Output the generated PDF to a file |
75
|
1 |
|
file_put_contents($outputPath, $dompdf->output()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Loads the EPUB content for conversion. |
80
|
|
|
* |
81
|
|
|
* @throws Exception If the content cannot be loaded. |
82
|
|
|
*/ |
83
|
2 |
|
private function loadEpubContent(string $epubDirectory): string |
84
|
|
|
{ |
85
|
|
|
// Simplified example: Load content from a specific file |
86
|
2 |
|
$contentFile = $epubDirectory . '/content.xhtml'; // Adjust this path as needed |
87
|
2 |
|
if (! file_exists($contentFile)) { |
88
|
1 |
|
throw new Exception("Content file not found: {$contentFile}"); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
$content = file_get_contents($contentFile); |
92
|
1 |
|
if ($content === false) { |
93
|
|
|
throw new Exception("Failed to read content from: {$contentFile}"); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
return $content; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|