| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- declare(strict_types=1);
- namespace App\Service\Export\Encoder;
- use \Knp\Snappy\Pdf;
- /**
- * Encode HTML to PDF
- */
- class PdfEncoder
- {
- /**
- * Default encoding options
- * @see https://wkhtmltopdf.org/libwkhtmltox/
- *
- * @var array
- */
- private array $defaultOptions = [
- 'margin-top' => 35,
- 'margin-right' => 10,
- 'margin-bottom' => 15,
- 'margin-left' => 15,
- 'header-spacing' => 5,
- 'enable-local-file-access' => true
- ];
- public function __construct(
- private Pdf $knpSnappy
- ) {}
- /**
- * Default encoding options
- * @return array
- */
- public function getDefaultOptions() {
- return $this->defaultOptions;
- }
- /**
- * Encode the given HTML content into PDF, and
- * return the encoded content
- *
- * @param string $html
- * @param array $options
- * @return string
- */
- public function encode(string $html, array $options = []): string
- {
- $options = array_merge($this->defaultOptions, $options);
- return $this->knpSnappy->getOutputFromHtml($html, $options);
- }
- /**
- * Encode the given HTML content into PDF,
- * write it into a file at the given location,
- * and returns the file path
- *
- * @param string $html
- * @param string $path
- * @param array $options
- * @return string
- */
- public function encodeToFile(string $html, string $path, array $options = []): string
- {
- $options = array_merge($this->defaultOptions, $options);
- $this->knpSnappy->generateFromHtml($html, $path, $options);
- return $path;
- }
- }
|