-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphp2latex.php
executable file
·53 lines (44 loc) · 1.57 KB
/
php2latex.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
class LatexCompiler {
private $templateFile;
private $beginId;
private $endId;
private $path_output;
public $latexContent;
public $return;
private function setID($beginId, $endId) {
$this->beginId = $beginId;
$this->endId = $endId;
}
public function __construct($templateFile, $output) {
$this->templateFile = $templateFile;
$this->latexContent = file_get_contents($templateFile);
$this->setID("%begin-content","%end-content");
$this->path_output = $output;
}
public function addContent($content) {
$startPos = strpos($this->latexContent, $this->beginId);
$endPos = strpos($this->latexContent, $this->endId);
if ($startPos === false || $endPos === false) {
$this->latexContent .= $content;
} else {
$this->latexContent = substr_replace($this->latexContent, $content, $endPos, 0);
}
}
public function compileToPDF() : bool
{
$tempLatexFile = "./temp.tex";
file_put_contents($tempLatexFile, $this->latexContent);
$command = "/usr/bin/pdflatex -interaction=nonstopmode ".escapeshellarg($tempLatexFile);
try {
$this->return = shell_exec($command);
shell_exec("rm temp.aux; rm temp.log; rm temp.tex");
shell_exec("mv temp.pdf ".escapeshellarg($this->path_output."/".uniqid().".pdf"));
return true;
} catch (Exception $e) {
$this->return = 'Exception reçue : '.$e->getMessage();
return false;
}
}
}
?>