summaryrefslogtreecommitdiff
path: root/src/DompdfCli.php
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2021-12-29 16:21:38 +0100
committerDaniel Weipert <code@drogueronin.de>2021-12-29 16:21:38 +0100
commit08e523874c18112d07ba23be84fe71ff83a9a175 (patch)
tree24020fa2a7839ece06f47a37f9c883e9c865aa7d /src/DompdfCli.php
Initial commitHEADmain
Diffstat (limited to 'src/DompdfCli.php')
-rw-r--r--src/DompdfCli.php76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/DompdfCli.php b/src/DompdfCli.php
new file mode 100644
index 0000000..57281e4
--- /dev/null
+++ b/src/DompdfCli.php
@@ -0,0 +1,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;
+ }
+}
+