developer.jelix.org is not used any more and exists only for history. Post new tickets on the Github account.
developer.jelix.org n'est plus utilisée, et existe uniquement pour son historique. Postez les nouveaux tickets sur le compte github.

Ticket #130: genericLatex.class.3.php

File genericLatex.class.3.php, 4.5 KB (added by Aubanel MONNIER, 13 years ago)

Yet another update, finally understood how latex commands should work (should look less clumsy to latex users). Also added a default commands function + other minor fixes. Still the problem of the exception though, if someone can guide me here ...

Line 
1<?php
2/**
3* @package     jelix
4* @subpackage  response
5* @version     $Id$
6* @author      Aubanel MONNIER
7* @contributor
8* @copyright   2007 Aubanel MONNIER
9* @link        http://aubanel.info
10* @licence     GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
11*/
12
13require_once (JELIX_LIB_RESPONSE_PATH.'jResponseText.class.php');
14define (PDF_LATEX_RESPONSE_CACHE, JELIX_APP_TEMP_PATH.'responsePdfLatex/');
15
16class genericLatex extends jResponseText {
17 /**
18     * selector of the main template file
19     * This template should contains the body content, and is used by the $body template engine
20     * @var string
21     */
22    public $bodyTpl = '';
23/**
24     * The template engine used to generate the content
25     * @var jTpl
26     */
27    public $body = null;
28/**
29     * Authors of the document
30     * @var array
31     */
32    public $authors = array();
33/**
34     * Document title
35     * @var string
36     */
37    public $title = '';
38/**
39     * Contains the list of commands to write in the preamble.
40     * @var array
41     */
42    protected $_commands=array();
43
44/**
45     * complete path to the pdflatex executable
46     * @var string
47     */
48 public $pdflatexPath='pdflatex';
49
50 /**
51    * constructor;
52    * setup the template engine
53    */
54    function __construct ($attributes=array()){
55        $this->body = new jTpl();
56        parent::__construct($attributes);
57    }
58/**
59     * Add a command to the preamble, e.g. \documentclass[a4,11pt]{article}
60     * @param string $command name of the command to add
61     * @param string $argument argument of the command to add
62     * @param array $options options of the command to add
63     */
64    function addCommand($command, $argument, $options=array()){
65            $cmd = '\\'.$command;
66            if (count($options)) $cmd.='['.join(',',$options).']';
67            $cmd.='{'.$argument.'}';
68            $this->_commands []= $cmd;
69    }
70/**
71     * A list of commands that can be safely used as default, or as a template for the _commonProcess function
72     * Tis function is called if the command stack is empty (useful to get quicly started)
73     */
74    function addDefaultCommands(){
75        $this->addCommand('documentclass', 'article', array('a4', '11pt'));
76        $this->addCommand('usepackage', 'fontenc', array('T1'));
77        $this->addCommand('usepackage', 'graphicx');
78        $this->addCommand('usepackage', 'geometry', array('pdftex'));
79        $this->addCommand('geometry', 'hmargin=1cm, vmargin=1cm');
80    }
81    /**
82     * output the pdf content
83     *
84     * @return boolean    true if the generated content is ok
85     */
86function output(){
87     if (count($this->_commands) <= 0) //No commands, likewise we need some...
88         $this->addDefaultCommands();
89     
90     ob_start();
91     echo join("\n", $this->_commands).'
92\begin{document}
93\title{'.$this->title.'}
94\author{';
95    foreach ($this->authors as $a) echo $a.'\\\\'."\n";
96    echo'}
97\date{\today}
98\maketitle
99';
100    $this->body->display($this->bodyTpl);
101    echo  '
102   
103\end{document}';
104
105    $data= ob_get_contents();
106    ob_end_clean();
107    $fbase='cache-'.md5($data);
108   
109    $texFile=PDF_LATEX_RESPONSE_CACHE.$fbase.'.tex';
110    $pdfFile=PDF_LATEX_RESPONSE_CACHE.$fbase.'.pdf';
111
112    if (! file_exists($pdfFile)){
113        //Naïve cache: we have an md5 on the content of the tex file. If the pdf corresponding to this content already exists, just serve it. No managment of cache deletion :o/
114        jFile::write($texFile, $data);
115        $output=array();
116        $retVal=1;     
117         exec('
118        TEXMFOUTPUT='.PDF_LATEX_RESPONSE_CACHE.' && export TEXMFOUTPUT && TEXINPUTS=:'.PDF_LATEX_RESPONSE_CACHE.' && export TEXINPUTS &&
119        '.$this->pdflatexPath.' --interaction=batchmode '.$texFile, $output, $retVal);
120        if ($retVal==0){
121            $outputStr=implode('<br />',$output);
122            throw new jException('Error: %s execution returned %s',array($this->pdflatexPath, $outputStr));
123            return false;
124        }
125    }
126
127        $this->_httpHeaders['Content-Type']='application/pdf';
128        $this->_httpHeaders['Content-length']=@filesize($pdfFile);
129        $this->_httpHeaders['Content-Disposition']='attachment; filename='.$this->title.'.pdf';
130        $this->sendHttpHeaders();
131       
132        readfile($pdfFile);
133        return true;
134 }
135   // modifications communes aux actions utilisant cette reponses
136   protected function _commonProcess(){
137       
138   }
139/**
140     * Clears the cache directory
141     */
142public function clearCache(){
143    jFile::removeDir(PDF_LATEX_RESPONSE_CACHE, false);
144}
145
146}
147?>