1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
<?php
namespace DompdfCli;
use Dompdf\Dompdf;
use Dompdf\Options;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SingleCommandApplication;
class DompdfCli
{
/**
* DompdfCli constructor.
*/
public function __construct()
{
$application = new SingleCommandApplication();
$application = $this->configure($application);
$application->setCode([$this, 'execute']);
$application->run();
}
/**
* Configure the Application
*/
public function configure(SingleCommandApplication $application): SingleCommandApplication
{
$application->addArgument('input', InputArgument::REQUIRED, 'Path to html input file');
$application->addArgument('output', InputArgument::REQUIRED, 'Path to pdf output file');
$application->addOption('options', null, InputOption::VALUE_REQUIRED, 'https://github.com/dompdf/dompdf/blob/master/src/Options.php');
return $application;
}
/**
* Execute
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
$dompdfOptions = new Options([
'chroot' => dirname($input->getArgument('input')),
'isRemoteEnabled' => true,
]);
$dompdfRootDir = dirname(__DIR__) . '/vendor/dompdf/dompdf';
$dompdfOptions->setRootDir($dompdfRootDir);
$dompdfOptions->setFontDir($dompdfRootDir . '/lib/fonts');
$dompdfOptions->setFontCache($dompdfRootDir . '/lib/fonts');
// parse and set options
$options = explode(',', $input->getOption('options'));
foreach ($options as $option) {
$option = explode('=', $option);
$dompdfOptions->set($option[0], $option[1] ?? true);
}
// load and render html file to output path
$dompdf = new Dompdf($dompdfOptions);
$dompdf->loadHtmlFile($input->getArgument('input'));
$dompdf->render();
file_put_contents($input->getArgument('output'), $dompdf->output());
$output->writeln('Wrote PDF file to ' . $input->getArgument('output'));
return Command::SUCCESS;
}
}
|