HEX
Server: LiteSpeed
System: Linux server315.web-hosting.com 4.18.0-553.54.1.lve.el8.x86_64 #1 SMP Wed Jun 4 13:01:13 UTC 2025 x86_64
User: globfdxw (6114)
PHP: 8.1.34
Disabled: NONE
Upload Files
File: /home/globfdxw/www/wp-content/plugins/wpforms-pdf/src/PDF/Generator.php
<?php

// phpcs:disable Generic.Commenting.DocComment.MissingShort
/** @noinspection PhpUndefinedClassInspection */
/** @noinspection PhpUndefinedNamespaceInspection */
// phpcs:enable Generic.Commenting.DocComment.MissingShort

namespace WPFormsPDF\PDF;

use WPFormsPDF\API\API;
use WPFormsPDF\Storage;
use WPForms\Helpers\File;

/**
 * PDF Generator class.
 *
 * Handles the generation of PDF documents using the library wrapper.
 *
 * @since 1.0.0
 */
class Generator {

	/**
	 * API class instance.
	 *
	 * @since 1.0.0
	 *
	 * @var API
	 */
	private $api;

	/**
	 * PDF options.
	 *
	 * @since 1.0.0
	 *
	 * @var array
	 */
	private $options;

	/**
	 * Extra data.
	 *
	 * @since 1.1.0
	 *
	 * @var array
	 */
	private $extra;

	/**
	 * Generated PDF content.
	 *
	 * @since 1.0.0
	 *
	 * @var string
	 */
	private $pdf_content;

	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 *
	 * @param array $options Optional. PDF generation options. Default empty array.
	 * @param array $extra   Optional. Extra data. Default empty array.
	 */
	public function __construct( array $options = [], array $extra = [] ) {

		$this->options = $options;
		$this->extra   = $extra;
		$this->api     = new API();
	}

	/**
	 * Generate PDF from HTML content.
	 *
	 * @since 1.0.0
	 *
	 * @param string $html   HTML content to convert to PDF.
	 * @param array  $images Images data.
	 *
	 * @return Generator Instance of the Generator for method chaining.
	 */
	public function generate_from_html( string $html, array $images ): Generator {

		$this->pdf_content = $this->api->generate_pdf( $html, $this->options, $images, $this->extra );

		// Reset PDF content if there is an error.
		if ( ! empty( $this->pdf_content['error'] ) ) {
			$this->pdf_content = '';
		}

		return $this;
	}

	/**
	 * Get the PDF content as a string.
	 *
	 * @since 1.0.0
	 *
	 * @return string PDF content.
	 */
	public function output(): string {

		return $this->pdf_content;
	}

	/**
	 * Save the generated PDF to a file.
	 *
	 * @since 1.0.0
	 *
	 * @param string $file_path Path where to save the PDF file.
	 *
	 * @return bool True on success, false on failure.
	 */
	public function save( string $file_path ): bool {

		if ( wpforms_pdf()->helpers->is_debug() ) {
			File::put_contents( Storage::get_dir() . '/debug.pdf', $this->pdf_content );
		}

		if ( empty( $this->pdf_content ) ) {
			return false;
		}

		return File::put_contents( $file_path, $this->pdf_content );
	}
}