html = ''; $this->filename = null; $this->module_path = WD . '/modules/html2pdf/'; $this->template_path = $this->module_path . 'templates/'; $this->html2pdf = new Html2Pdf( 'P', 'A4', 'en', true, 'UTF-8', array(0, 0, 0, 0) ); $this->html2pdf->pdf->setDisplayMode('fullpage'); $this->templates = new html2pdf_templates(); //self::create_tmp_dir(); self::require_valid_paths([$this->template_path, $this->module_path]); } /** * @inheritDoc */ public function require_valid_paths(array $paths): bool { if (!self::validate_paths($paths)) { foreach ( $paths as $path ) { if (!self::validate_paths(array($path))) { // Check if the path is valid if (!is_dir($path)) { throw new Exception('The path ' . $path . ' is not valid'); } // Check if the path is readable if (!is_readable($path)) { throw new Exception('The path ' . $path . ' is not readable'); } } } } return true; } /** * @inheritDoc */ public function validate_paths(array $paths): bool { // Check if the paths are valid foreach ( $paths as $path ) { if (!is_dir($path) || !is_readable($path)) { return false; } } return true; } /** * @inheritDoc */ public function is_writeable(string $path): bool { // Check if the path is writeable if (is_writable($path)) { return true; } return false; } /** * Add the HTML content to be converted to PDF * @param string $html HTML content to be converted to PDF * @return self */ public function add_html(string $html): self { $this->html .= $html; return $this; } /** * Set the filename to save the PDF as (While the file is stored in a temporary location) * @param string|null $filename Filename to save the PDF as * @return self */ public function set_filename(string|null $filename): self { $this->filename = $filename; return $this; } /** * @inheritDoc * @throws Html2PdfException If there is an error generating the PDF * @throws Exception */ public function generate_pdf(): string { // Set the HTML content $this->html2pdf->writeHTML($this->html); // Generate a random tmp file name if the filename is not set self::generate_tmp_file(); // Set the path to save the PDF $pdf_file = $this->pdf_path; // Output the PDF to a file $this->html2pdf->output($pdf_file, 'F'); // Set the path to the tmp file $this->tmp_file_path = $pdf_file; self::store_tmp_file(); return $this->tmp_file_path; } /** * @inheritDoc */ public function generate_tmp_file(): pdf_generator_i { // Generate a random filename $this->filename = uniqid() . '.pdf'; // Set the path to save the PDF $this->pdf_path = '/tmp/' . $this->filename; return $this; } /** * @inheritDoc */ public function store_tmp_file(): pdf_generator_i { // Check if the tmp_file_path is set if (!self::is_temp_file_path_set()) { throw new Exception('The tmp_file_path is not set, cannot store unknown file'); } // Save the PDF to the minio store $pdf_store = new pdf_store(); $pdf_store->upload(self::generate_tmp_file_unique_id(), self::get_temp_file_path()); // Delete the tmp file if (file_exists($this->tmp_file_path)) { unlink($this->tmp_file_path); } // Set the path to the tmp file $this->tmp_file_path = $pdf_store->get_pdf_key_name($this->tmp_file_unique_id); return $this; } /** * Check if the tmp_file_path is set * @notation This function checks if the tmp_file_path is set * @return bool True if the tmp_file_path is set, false otherwise * @see is_temp_file_path_set() * @see store_tmp_file() * @see generate_tmp_file() */ private function is_temp_file_path_set(): bool { // Check if the tmp_file_path is set if (!empty($this->tmp_file_path)) { return true; } return false; } /** * Generate a unique ID for the tmp file * @notation This function generates a unique ID for the tmp file * @return string The unique ID for the tmp file */ private function generate_tmp_file_unique_id(): string { // Generate a unique ID for the tmp file (numbers only) $unique_id = uniqid(); // Set the unique ID for the tmp file $this->tmp_file_unique_id = (string)$unique_id; return $this->tmp_file_unique_id; } /** * Get the path to the tmp file * @notation This function gets the path to the tmp file * @return string The path to the tmp file * @throws Exception If the tmp_file_path is not set */ public function get_temp_file_path(): string { // Check if the tmp_file_path is set if (!self::is_temp_file_path_set()) { throw new Exception('The tmp_file_path is not set, cannot get unknown file'); } return $this->tmp_file_path; } }