From 08e523874c18112d07ba23be84fe71ff83a9a175 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Wed, 29 Dec 2021 16:21:38 +0100 Subject: Initial commit --- src/DompdfCli.php | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/DompdfCli.php (limited to 'src/DompdfCli.php') 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 @@ +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; + } +} + -- cgit v1.2.3