<?php
require_once(dirname(__FILE__) . '/sw_config.php');
require_once(dirname(__FILE__) . '/tcpdf.php');
define("_PDF_CREATOR_", "SW_Post2PDF");

define("__TITLE__", get_the_title($id));
define("__BLOGNAME__", get_option("blogname"));
define("__TOTAL_NUMBER_OF_PAGES__", "{nb}");

/* Clase que recibe un post y lo transforma a PDF
   usando las clases de TCPDF (http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf) */
class SW_Post2PDF extends TCPDF{
	var $_DOC_TITLE = "Blog post";
	var $_DOC_SUBJECT = "";
	var $_DOC_KEYWORDS = "";
	var $_html = "";

	function SW_Post2PDF() {
		parent::TCPDF(PDF_PAGE_ORIENTATION, "pt", PDF_PAGE_FORMAT, true);
		
		// Información del documento
		parent::SetCreator(_PDF_CREATOR_);
		parent::SetAuthor(_PDF_AUTHOR_);
		parent::SetTitle($this->_DOC_TITLE);
		parent::SetSubject($this->_DOC_SUBJECT);
		parent::SetKeywords($this->_DOC_KEYWORDS);
		
		// Márgenes del documento
		parent::SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);

		// Salto de página automático
		parent::SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

		// Imágenes escaladas
		parent::setImageScale(PDF_IMAGE_SCALE_RATIO);
		
		// Idioma
		$l = Array();
		$l['a_meta_charset'] = "UTF-8";
		$l['a_meta_dir'] = "ltr";
		$l['a_meta_language'] = "en";
		$l['w_page'] = "page";
		parent::setLanguageArray($l);

		// Toma las fuentes por defecto
//		parent::setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
		parent::SetFont(get_option("sw_pdf_body_font"), "", get_option("sw_pdf_header_title_font_size"));
		parent::setHeaderFont(Array(get_option("sw_pdf_header_title_font"), '', get_option("sw_pdf_header_title_font_size")));
		parent::setBodyFont(Array(get_option("sw_pdf_body_font"), '', get_option("sw_pdf_body_font_size")));
		parent::setFooterFont(Array(get_option("sw_pdf_footer_font"), '', get_option("sw_pdf_footer_font_size")));
		parent::SetFooterMargin(PDF_MARGIN_FOOTER);
		parent::AliasNbPages();
	}
	
	function setHeader() {
		parent::SetHeaderMargin(PDF_MARGIN_HEADER);
		parent::SetHeaderData(get_option("sw_pdf_header_image"), 
		                           get_option("sw_pdf_header_image_width"), 
								   preg_replace('/(__\w+__)/e', '($1)', get_option("sw_pdf_title")), 
								   preg_replace('/(__\w+__)/e', '($1)', get_option("sw_pdf_subtitle")));
	}

	function setText($_txt) {
		$this->_html .= $_txt;
	}
	
	function Footer() {
		parent::SetY(-15);
		parent::SetFont(get_option("sw_pdf_footer_font"),'I',get_option("sw_pdf_footer_font_size"));
		parent::SetTextColor(get_option("sw_pdf_footer_title_font_color_R"),
							get_option("sw_pdf_footer_title_font_color_G"),
							get_option("sw_pdf_footer_title_font_color_B"));
		$footer = str_replace("__PAGE_NUMBER__", parent::PageNo(), preg_replace('/(__\w+__)/e', '($1)', get_option("sw_pdf_footer")));
		parent::Cell(0,10, $footer,0,0,'C');
	}
}


?>
