<?

require_once(HTML2PS_DIR . 'pipeline.factory.class.php');

class PDF {

	var $_s_path_ps;

	function PDF ($_s_lib_path) {

		$this->_s_path_ps = $_s_lib_path;

		@set_time_limit(0);

		require_once($this->_s_path_ps . 'config.inc.php');
		require_once($this->_s_path_ps . 'fetcher.local.class.php');
		require_once($this->_s_path_ps . 'fetcher.url.class.php');

		parse_config_file($this->_s_path_ps.'html2ps.config');

	}

	/**
	 * Runs the HTML->PDF conversion with default settings
	 *
	 * Warning: if you have any files (like CSS stylesheets and/or images referenced by this file,
	 * use absolute links (like http://my.host/image.gif).
	 *
	 */
	function convert_to_pdf($s_html, $s_html_header, $s_html_footer, $filename=null) {


		$o_pipeline = PipelineFactory::create_default_pipeline("", "");
		
		// Override HTML source
		$o_pipeline->fetchers[] = new MyFetcherLocalFile($s_html);

		$o_pipeline->pre_tree_filters[] = new PreTreeFilterHeaderFooter($s_html_header, $s_html_footer);
		$o_pipeline->pre_tree_filters[] = new PreTreeFilterHTML2PSFields();


		// Override destination to local file
		$s_tmpname = $filename 
						? $filename 
						: $this->_s_path_ps.'temp/'.uniqid(date('His').rand()).'.pdf';

		$o_pipeline->destination = new MyDestinationFile($s_tmpname);
		
		$media = Media::predefined("A4");
		$media->set_landscape(false);
		$media->set_margins(array(	'left'   => 25,
									'right'  => 15,
									'top'    => 25,
									'bottom' => 20));
		$media->set_pixels(800);
		
		global $g_config;
		$g_config = array(
						'cssmedia'			=> 'screen',
						'scalepoints'		=> '1',
						'renderimages'		=> true,
						'renderlinks'		=> true,
						'renderfields'		=> false,
						'renderforms'		=> false,
						'mode'				=> 'html',
						'encoding'			=> '',
						'debugbox'			=> false,
						'pdfversion'		=> '1.4',
						'draw_page_border'	=> false,
						'smartpagebreak'	=> true,
						'transparency_workaround' => isset($_REQUEST['transparency_workaround'])
	
					);


		$o_pipeline->configure($g_config);

		$o_pipeline->process('', $media);

		// Get the PDF data
		if ($filename) {
			return true;
		} else {
			$s_data = file_get_contents($s_tmpname);
			unlink($s_tmpname);
			return $s_data;
		}

	}
		
}


class MyDestinationFile extends Destination {

	var $_dest_filename;
	
	function MyDestinationFile($dest_filename) {
		$this->_dest_filename = $dest_filename;
	}
	
	function process($tmp_filename, $content_type) {
		copy($tmp_filename, $this->_dest_filename);
	}

}

class MyFetcherLocalFile extends Fetcher {

	var $_content;
	
	function MyFetcherLocalFile($s_html) {
		$this->_content = $s_html;
	}
	
	function get_data($dummy1) {
		return new FetchedDataURL($this->_content, array(), "");
	}

	function get_base_url() {
		/*if ($GLOBALS['globalSettings']->general->project->hostname) {
			return $GLOBALS['globalSettings']->general->project->hostname;
		} elseif ($_SERVER['HTTP_HOST']) {*/
			return "http://". $_SERVER['HTTP_HOST'];
		/*}*/
	}

}


?>