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.2.php

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

update with: clear cache function, correct headers, more configration options

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     * Document Class (appened to \documentclass clause)
40     * @var array
41     */
42    public $documentClass= '[11pt]{article}';
43/**
44     * Document packages (will be enclosed in \usepackage clauses)
45     * @var array
46     */
47    public $packages= array('graphicx');
48/**
49     * Document properties
50     * @var array
51     */
52    public $properties= array('topmargin'=>'-1.5cm', 'oddsidemargin'=>'-0.04cm',  'evensidemargin'=> '-0.04cm', 'textwidth'=>'16.59cm', 'textheight'=>'21.94cm','parskip'=>'7.2pt', 'parindent'=>'0pt');
53   
54/**
55     * complete path to the pdflatex executable
56     * @var string
57     */
58 public $pdflatexPath='pdflatex';
59
60 /**
61    * constructor;
62    * setup the template engine
63    */
64    function __construct ($attributes=array()){
65        $this->body = new jTpl();
66        parent::__construct($attributes);
67    }
68    /**
69     * output the pdf content
70     *
71     * @return boolean    true if the generated content is ok
72     */
73function output(){
74     global $gJConfig;
75     ob_start();
76    echo'\\documentclass'.$this->documentClass."\n";
77foreach ($this->packages as $pack) 
78     echo '\\usepackage{'.$pack."}\n";
79foreach ($this->properties as $prop=>$val) 
80     echo '\\'.$prop.' '.$val."\n";
81echo'
82\begin{document}
83\title{'.$this->title.'}
84\author{';
85    foreach ($this->authors as $a) echo $a.'\\\\'."\n";
86    echo'}
87\date{\today}
88\maketitle
89';
90    $this->body->display($this->bodyTpl);
91    echo  '
92   
93\end{document}';
94
95    $data= ob_get_contents();
96    ob_end_clean();
97    $fbase='cache-'.md5($data);
98   
99    $texFile=PDF_LATEX_RESPONSE_CACHE.$fbase.'.tex';
100    $pdfFile=PDF_LATEX_RESPONSE_CACHE.$fbase.'.pdf';
101
102    if (! file_exists($pdfFile)){
103        //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/
104        jFile::write($texFile, $data);
105        $output=array();
106        $retVal=1;     
107         exec('
108        TEXMFOUTPUT='.PDF_LATEX_RESPONSE_CACHE.' && export TEXMFOUTPUT && TEXINPUTS=:'.PDF_LATEX_RESPONSE_CACHE.' && export TEXINPUTS &&
109        '.$this->pdflatexPath.' --interaction=batchmode '.$texFile, $output, $retVal);
110        if ($retVal==0){
111            $outputStr=implode('<br />',$output);
112            throw new jException('Error: %s execution returned %s',array($this->pdflatexPath, $outputStr));
113            return true;
114        }
115    }
116
117        $this->_httpHeaders['Content-Type']='application/pdf';
118        $this->_httpHeaders['Content-length']=@filesize($pdfFile);
119        $this->_httpHeaders['Content-Disposition']='attachment; filename='.$this->title.'.pdf';
120        $this->sendHttpHeaders();
121       
122        readfile($pdfFile);
123        return true;
124 }
125   // modifications communes aux actions utilisant cette reponses
126   protected function _commonProcess(){
127       
128   }
129/**
130     * Clears the cache directory
131     *
132     * @return boolean    true if the generated content is ok
133     */
134public function clearCache(){
135    jFile::removeDir(PDF_LATEX_RESPONSE_CACHE, false);
136}
137
138}
139?>