<?php
/*
    /prince.php

    Convert an xhtml file in (see $workfile below) to PDF by calling Prince
*/

$debugging = TRUE;

if ( $debugging ) echo 'dirname(__FILE__) = ' . dirname(__FILE__) . '<br />';

if ( $debugging ) echo '$_SERVER["DOCUMENT_ROOT"] = ' . $_SERVER["DOCUMENT_ROOT"] . '<br />';
require_once $_SERVER["DOCUMENT_ROOT"] . "/code/prince-php/prince.php"; //  Prince class
require_once ($_SERVER["DOCUMENT_ROOT"] . "/code/funcPr.php"); // debug printer "pr"

$workFile = '/newprint/new_directory';    //  path for XHTML intermediate file AND for final PDF file

$outLogFile = "$workFile.log";
$outDestPath = "$workFile.xhtml";     //  derived work file path
$outPDF = "$workFile.pdf";      //  derived name for PDF file
$outHandle = false;
$myCSS = array('/newprint/printing.css');   //  CSS to be added after Joomla & template CSS.
// Define base CSS files for Prince run. These precede declared CSS for this file.
$baseCSS = array(
    '/images/fonts/fonts.css',
    '/templates/system/css/system.css',
    '/templates/system/css/general.css',
    '/templates/fha/css/template.css',
    '/templates/fha/css/custom.css'
);

if ( $debugging ) pr($baseCSS, 'baseCSS');
//        '/templates/blend_education/css/core.css',    //removed

$outLog = $_SERVER['DOCUMENT_ROOT'] . $outLogFile;

if ( $debugging ) echo "outLog: $outLog<br />";
$outDest = $_SERVER['DOCUMENT_ROOT'] . $outDestPath;

if ( $debugging ) echo "outDest: $outDest<br />";

//if (!$outDest)
//    die("<p>Failed to create temp file</p>");
if (!chmod($outDest, 0666))
    die("<p>chmod for temp file $outDest failed</p>");
//$outHandle = fopen($outDest, 'w');
//if (!$outHandle)
//    die("<p>Open file $outDest failed</p>");
foreach ($myCSS as $cssPath) {
    $cssArray[] = $_SERVER['DOCUMENT_ROOT'] . $cssPath;
}

if ( $debugging ) pr($cssArray, 'cssArray');
if ( $debugging ) echo "Instantiating Prince<br />";
$prince = new Prince('/home1/fearrin1/usr/prince', TRUE);      //  Instantiate Prince for this run
$prince->setLog($outLog);
if (!$prince)
    die("<p>Prince instantiation failed</p>");
//$outHandle = fopen($outDest, 'w');
//if ($outHandle) {
    //fclose($outHandle);
    foreach ($cssArray as $css) {
        $res = $prince->addStyleSheet($css);
        if(!$res)
            pr($res, "Return from addStyleSheet");
    }
    $pcvar = $prince->convert_file($outDest, @$princeMsgs);
    if (!$pcvar) {
        pr($princeMsgs, "PDF Conversion Messages - PDF Generation Failed");
        ?>
        <p style='margin:0.5in;'>
            <a href='<?= "$outDestPath" ?>' style='text-decoration: none;padding:5px;border:1px solid black;background-color:#ddddff'>
                Click to view HTML source file to diagnose problems.
            </a>
        </p>
        <?php } else {
        ?>
        <h2>PDF generation OK</h2>
        <p style='margin:0.5in;'>
            <a href=<?= "'$outPDF'" ?> style='text-decoration: none;padding:5px;border:1px solid black;background-color:#ddddff'>
               Click to view PDF file
        </a>
        </p>
        <p style='margin:0.5in;'>
            (If you see the error "No PDF File Generated", Reload the page.)
        </p>
        <?php
    }
//}
//else
  //  die ("<p>Failed to open output file $outHandle for writing</p>");
