<?php

/*
 +--------------------------------------------------------------------+
 | CiviCRM version 3.0                                                |
 +--------------------------------------------------------------------+
 | Copyright CiviCRM LLC (c) 2004-2009                                |
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007.                                       |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License along with this program; if not, contact CiviCRM LLC       |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
*/

/**
 *
 * @package CRM
 * @copyright CiviCRM LLC (c) 2004-2009
 * $Id$
 *
 */


class CRM_Utils_PDF_Utils {

    static function domlib( $text,
                            $fileName = 'civicrm.pdf' ) {
        require_once 'packages/dompdf/dompdf_config.inc.php';
        $dompdf = new DOMPDF( );
        
        $values = array( );
        if ( ! is_array( $text ) ) {
            $values =  array( $text );
        } else {
            $values =& $text;
        }

        $first = true;
        $html = "
<style>
.page_break {
  page-break-before: always;
}
</style>
";

        foreach ( $values as $value ) {
            if ( ! $first ) {
                $html .= "<h2 class=\"page_break\">{$value['to']}: {$value['subject']}</h2><p>";
            } else {
                $html .= "<h2>{$value['to']}: {$value['subject']}</h2><p>";
                $first = false;
            }
            $html .= "{$value['body']}\n";
        }
        $dompdf->load_html( $html );
        $dompdf->render( );
        $dompdf->stream( $fileName );
    }

    static function html2pdf( $text,
                              $fileName = 'civicrm.pdf',
                              $orientation = 'landscape',
                              $paperSize   = 'a3' ) {
        require_once 'packages/dompdf/dompdf_config.inc.php';
        spl_autoload_register('DOMPDF_autoload');
        $dompdf = new DOMPDF( );
        
        $values = array( );
        if ( ! is_array( $text ) ) {
            $values =  array( $text );
        } else {
            $values =& $text;
        }

        $html = "
<style>
.page_break {
  page-break-before: always;
}
</style>
";

        foreach ( $values as $value ) {
            $html .= "{$value}\n";
        }
        $dompdf->load_html( $html );
        $dompdf->set_paper ($paperSize, $orientation);
        $dompdf->render( );
        $dompdf->stream( $fileName );
    }

    static function &pdflib( $fileName,
                             $searchPath,
                             &$values,
                             $numPages = 1,
                             $echo    = true,
                             $output  = 'College_Match_App',
                             $creator = 'CiviCRM',
                             $author  = 'http://www.civicrm.org/',
                             $title   = '2006 College Match Scholarship Application' ) {
        try {
            $pdf = new PDFlib( );
            $pdf->set_parameter( "compatibility", "1.6");
            $pdf->set_parameter( "licensefile", "/home/paras/bin/license/pdflib.txt");

            if ( $pdf->begin_document( '', '' ) == 0 ) {
                CRM_Core_Error::statusBounce( "PDFlib Error: " . $pdf->get_errmsg( ) );
            }

            $config =& CRM_Core_Config::singleton( );
            $pdf->set_parameter( 'resourcefile', $config->templateDir . '/Quest/pdf/pdflib.upr' );
            $pdf->set_parameter( 'textformat', 'utf8' );

            /* Set the search path for fonts and PDF files */
            $pdf->set_parameter( 'SearchPath', $searchPath );

            /* This line is required to avoid problems on Japanese systems */
            $pdf->set_parameter( 'hypertextencoding', 'winansi' );

            $pdf->set_info( 'Creator', $creator );
            $pdf->set_info( 'Author' , $author  );
            $pdf->set_info( 'Title'  , $title   );

            $blockContainer = $pdf->open_pdi( $fileName, '', 0 );
            if ( $blockContainer == 0 ) {
                CRM_Core_Error::statusBounce( 'PDFlib Error: ' . $pdf->get_errmsg( ) );
            }

            for ( $i = 1; $i  <= $numPages; $i++ ) {
                $page = $pdf->open_pdi_page( $blockContainer, $i, '' );
                if ( $page == 0 ) {
                    CRM_Core_Error::statusBounce( 'PDFlib Error: ' . $pdf->get_errmsg( ) );
                }
                
                $pdf->begin_page_ext( 20, 20, '' ); /* dummy page size */
                
                /* This will adjust the page size to the block container's size. */
                $pdf->fit_pdi_page( $page, 0, 0, 'adjustpage' );

             
                $status = array( );
                /* Fill all text blocks with dynamic data */
                foreach ( $values as $key => $value ) {
                    if ( is_array( $value ) ) {
                        continue;
                    }

                    // pdflib does like the forward slash character, hence convert
                    $value = str_replace( '/', '_', $value );

                    $res = $pdf->fill_textblock( $page,
                                                 $key,
                                                 $value,
                                                 'embedding encoding=winansi' );
                    /**
                    if ( $res == 0 ) {
                        CRM_Core_Error::debug( "$key, $value: $res", $pdf->get_errmsg( ) );
                    } else {
                        CRM_Core_Error::debug( "SUCCESS: $key, $value", null );
                    }
                    **/
                }
                
                $pdf->end_page_ext( '' );
                $pdf->close_pdi_page( $page );
            }

            $pdf->end_document( '' );
            $pdf->close_pdi( $blockContainer );

            $buf = $pdf->get_buffer();
            $len = strlen($buf);

            if ( $echo ) {
                header('Content-type: application/pdf');
                header("Content-Length: $len");
                header("Content-Disposition: inline; filename={$output}.pdf");
                echo $buf;
                exit( );
            } else {
                return $buf;
            }
        }
        catch ( PDFlibException $excp ) {
            CRM_Core_Error::statusBounce( 'PDFlib Error: Exception' .
                                          "[" . $excp->get_errnum( ) . "] " . $excp->get_apiname( ) . ": " .
                                          $excp->get_errmsg( ) );
        }
        catch (Exception $excp) {
            CRM_Core_Error::statusBounce( "PDFlib Error: " . $excp->get_errmsg( ) );
        }
    }
}


