Beispiel #1
0
function HTMLDoc_CreatePDF($ModeID, $HTML, $Prefix = '/')
{
    /****************************************************************************/
    $__args_types = array('string', 'string,object', 'string');
    #-----------------------------------------------------------------------------
    $__args__ = Func_Get_Args();
    eval(FUNCTION_INIT);
    /****************************************************************************/
    if (Is_Object($HTML)) {
        #---------------------------------------------------------------------------
        $Tables = $HTML->GetByTagName('TABLE');
        #---------------------------------------------------------------------------
        for ($i = 0; $i < Count($Tables); $i++) {
            #-------------------------------------------------------------------------
            $Table =& $Tables[$i];
            #-------------------------------------------------------------------------
            switch (@$Table->Attribs['class']) {
                case 'Standard':
                    $Table->AddAttribs(array('border' => 2, 'cellspacing' => 0, 'cellpadding' => 5), TRUE);
                    break;
                default:
                    # No more...
            }
        }
        #---------------------------------------------------------------------------
        $Tds = $HTML->GetByTagName('TD');
        #---------------------------------------------------------------------------
        for ($i = 0; $i < Count($Tds); $i++) {
            #-------------------------------------------------------------------------
            $Td =& $Tds[$i];
            #-------------------------------------------------------------------------
            switch (@$Td->Attribs['class']) {
                case 'Head':
                    $Td->AddAttribs(array('bgcolor' => '#ADC1F0'), TRUE);
                    break;
                case 'Separator':
                    $Td->AddAttribs(array('bgcolor' => '#EAEAEA'), TRUE);
                    break;
                default:
                    # No more...
            }
        }
        #---------------------------------------------------------------------------
        $Imgs = $HTML->GetByTagName('IMG');
        #---------------------------------------------------------------------------
        for ($i = 0; $i < Count($Imgs); $i++) {
            #-------------------------------------------------------------------------
            $Img =& $Imgs[$i];
            #-------------------------------------------------------------------------
            $Img->AddAttribs(array('src' => SPrintF('%s/%s', $Prefix, $Img->Attribs['src'])), TRUE);
        }
        #---------------------------------------------------------------------------
        $HTML = $HTML->Build();
    }
    #-----------------------------------------------------------------------------
    $Config = Config();
    #-----------------------------------------------------------------------------
    $Settings = $Config['HTMLDOC'];
    #-----------------------------------------------------------------------------
    $Modes = $Settings['Modes'];
    #-----------------------------------------------------------------------------
    $Mode = isset($Modes[$ModeID]) ? $Modes[$ModeID] : $ModeID;
    #-----------------------------------------------------------------------------
    $Tmp = System_Element('tmp');
    if (Is_Error($Tmp)) {
        return ERROR | @Trigger_Error('[HTMLDoc_CreatePDF]: временная папка не найдена');
    }
    #-----------------------------------------------------------------------------
    $Logs = SPrintF('%s/logs', $Tmp);
    #-----------------------------------------------------------------------------
    if (!File_Exists($Logs)) {
        #---------------------------------------------------------------------------
        if (!@MkDir($Logs, 0777, TRUE)) {
            return ERROR | @Trigger_Error(500);
        }
    }
    #-----------------------------------------------------------------------------
    $HTML = @Mb_Convert_Encoding($HTML, $Settings['ConvertToCharset']);
    if (!$HTML) {
        return ERROR | @Trigger_Error('[HTMLDoc_CreatePDF]: не удалось преобразовать кодировку');
    }
    #-----------------------------------------------------------------------------
    $UniqID = UniqID('HTMLDOC');
    #-----------------------------------------------------------------------------
    $File = IO_Write($Path = SPrintF('%s/%s', $Tmp, $UniqID), $HTML);
    Debug($File);
    if (Is_Error($File)) {
        return ERROR | @Trigger_Error('[HTMLDoc_CreatePDF]: не удалось создать временный файл');
    }
    #-----------------------------------------------------------------------------
    $Command = SPrintF('htmldoc %s %s', $Mode, $Path);
    #-----------------------------------------------------------------------------
    Debug($Command);
    #-----------------------------------------------------------------------------
    if (!PutENV('HTMLDOC_NOCGI=1')) {
        return ERROR | @Trigger_Error('[HTMLDoc_CreatePDF]: не удалось установить переменную окружения HTMLDOC_NOCGI');
    }
    #-----------------------------------------------------------------------------
    $HTMLDOC = @Proc_Open($Command, array(array('pipe', 'r'), array('pipe', 'w'), array('file', $Log = SPrintF('%s/HTMLDOC.log', $Logs), 'a')), $Pipes);
    if (!Is_Resource($HTMLDOC)) {
        return ERROR | @Trigger_Error('[HTMLDoc_CreatePDF]: не удалось открыть процесс');
    }
    #-----------------------------------------------------------------------------
    $StdOut =& $Pipes[1];
    #-----------------------------------------------------------------------------
    $Result = '';
    #-----------------------------------------------------------------------------
    while (!Feof($StdOut)) {
        $Result .= FRead($StdOut, 1024);
    }
    #-----------------------------------------------------------------------------
    Proc_Close($HTMLDOC);
    #-----------------------------------------------------------------------------
    if (!UnLink($Path)) {
        return ERROR | @Trigger_Error('[HTMLDoc_CreatePDF]: не удалось удалить временный файл');
    }
    #-----------------------------------------------------------------------------
    if (!$Result) {
        return ERROR | @Trigger_Error(SPrintF('[HTMLDoc_CreatePDF]: ошибка формирования PDF, смотрите (%s)', $Log));
    }
    #-----------------------------------------------------------------------------
    return $Result;
}
 if (Is_Error($Tmp)) {
     return ERROR | @Trigger_Error(500);
 }
 #-----------------------------------------------------------------
 $UniqID = UniqID('ImageMagick');
 #-----------------------------------------------------------------
 $File = IO_Write($PDF = SPrintF('%s/%s.pdf', $Tmp, $UniqID), $File);
 if (Is_Error($File)) {
     return ERROR | @Trigger_Error(500);
 }
 #-----------------------------------------------------------------
 $Command = SPrintF('convert -density 120 -compress group4 %s %s', $PDF, $Tiff = SPrintF('%s/%s.tiff', $Tmp, $UniqID));
 #-----------------------------------------------------------------
 Debug($Command);
 #-----------------------------------------------------------------
 $ImageMagick = @Proc_Open($Command, array(array('pipe', 'r'), array('pipe', 'w'), array('file', SPrintF('%s/logs/ImageMagic.log', $Tmp), 'a')), $Pipes);
 if (!Is_Resource($ImageMagick)) {
     return ERROR | @Trigger_Error(500);
 }
 #-----------------------------------------------------------------
 Proc_Close($ImageMagick);
 #-----------------------------------------------------------------
 UnLink($PDF);
 #-----------------------------------------------------------------
 $File = IO_Read($Tiff);
 if (Is_Error($File)) {
     return ERROR | @Trigger_Error(500);
 }
 #-----------------------------------------------------------------
 UnLink($Tiff);
 #-----------------------------------------------------------------