-
Notifications
You must be signed in to change notification settings - Fork 0
/
Foml.php
executable file
·224 lines (189 loc) · 7.97 KB
/
Foml.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/*
* If you are NOT using a class autoloader
* you should require_once 'FomlConfig.php'
* to pre-load all of the Foml classes.
*/
function foml_escape($Var) {
return $Var == null ? $Var : htmlentities($Var, ENT_QUOTES | ENT_SUBSTITUTE | ENT_XML1);
}
class Foml
{
const PHP_MODE = 'php';
const XML_MODE = 'xml';
static $fopExec = "fop-1.1/fop"; // fopExec is relative to this directory
static $tempDir = null; // defaults to system temp directory
static $keepTempFiles = true; // set to true for debugging
static $pdfMimeType = "application/pdf";
static $defaultNamespace = "fo";
// PHP 5.4 and beyond supports XML via htmlentities, but for now we will escape manually.
// courtesy of
// http://stackoverflow.com/questions/3957360/generating-xml-document-in-php-escape-characters
static function XmlEntities($string)
{
return str_replace(array("&", "<", ">", "\"", "'"),
array("&", "<", ">", """, "'"),
$string);
}
static function GeneratePhp($Template)
{
$fomlParser = new FomlParser();
$php = $fomlParser->ParseFile($Template);
return $php;
}
// returns XSL-FO string
// REVISIT - use ob_start with a callback function to capture output directly to a file,
// and return a temporary filename instead of a string.
static function GenerateXslFo($Template, $Args=null)
{
ob_start();
Foml::RenderFoml($Template, $Args);
$xslFo = ob_get_contents();
ob_end_clean();
return $xslFo;
}
static function RenderFoml($Template, $Args)
{
// import variables
if ($Args) {
foreach ($Args as $key=>$value) {
$$key = $value;
}
}
$_php = Foml::GeneratePhp($Template);
//Dump(htmlspecialchars($_php)); return;
eval("?".">".$_php); // prefixed with ? > to exit implicit php mode
}
static function TempName($Prefix)
{
$tempDir = Foml::$tempDir;
if ($tempDir == null) $tempDir = sys_get_temp_dir();
return tempnam($tempDir, $Prefix);
}
// returns the name of the temporary pdf output file.
// The file must be unlinked by the caller.
static function XslFoToPdf($XslFo)
{
$fomlDir = dirname(__FILE__);
$docRoot = GetIfSet($_SERVER, 'DOCUMENT_ROOT');
$confFileName = "{$fomlDir}/fop.xconf";
$pdfFileName = Foml::TempName("pdf-");
$xslFoFileName = Foml::TempName("xslfo-");
$xslFile = fopen($xslFoFileName, "w");
fwrite($xslFile, $XslFo);
fclose($xslFile);
$escapedPdfFileName = escapeshellarg($pdfFileName);
$escapedXslFoFileName = escapeshellarg($xslFoFileName);
$escapedConfFileName = escapeshellarg($confFileName);
$fop = $fomlDir.'/'.Foml::$fopExec;
// We set the cwd to the directory this file is in for the subprocess.
// This allows us to use relative a path in fop.xconf to the fonts directory
// so we don't have to edit the conf file depending on our installation path.
// REVISITED:
// I was not able to get baseUrl or baseDir to work with relative paths for external-graphic nodes.
// So instead we have changed the cwd to docRoot, and set a fully-qualified path for the configuration file.
$cwd = $docRoot;
$cmd = "{$fop} {$escapedXslFoFileName} {$escapedPdfFileName} -c {$escapedConfFileName}";
$env = $_ENV;
// It seems that something changed (at FreeBSD 9.1?) that means that apache no longer
// has /usr/local/bin in the path by default. We add it here for fop because that is where
// Diablo Java(TM) SE Runtime Environment (build 1.6.0_07-b02)
// is installed.
if (array_key_exists('PATH', $env)) {
$env['PATH'].= ':/usr/local/bin';
} else {
$env['PATH'] = '/usr/local/bin';
}
// $env['PATH'].= ':/usr/local/bin';
// Fop tries to create a font cache at [user.home]/.fop/fop-fonts.cache
// see: https://github.com/apache/fop/blob/trunk/src/java/org/apache/fop/fonts/FontCache.java
// Because the apache-user doesn't have a home directory, this causes a fatal error unless we
// specify a new user.home value in FOP_OPTS. Note that for the cache to work
// you should create FOML/.fop and make it writable by the webserver user.
$env['FOP_OPTS'] = "";
$env['FOP_OPTS'].= "-Duser.home={$fomlDir}";
$env['FOP_OPTS'].= " -Xmx1024m"; // give the JVM extra memory - needed on small systems for complex documents
$outFile = Foml::TempName("stdout-");
$errFile = Foml::TempName("stderr-");
$descriptors = array(0=>array("pipe", "r"),
1=>array("file", $outFile, "w"),
2=>array("file", $errFile, "w")
);
// to run from the command line use something like this:
// setenv FOP_OPTS '-Duser.home=/usr/data/www/FOML -Xmx1024m'
// /usr/data/www/FOML/fop-1.0/fop '/var/tmp/xslfo-tsy5I1' '/var/tmp/pdf-iqe6y5' -c '/usr/data/www/FOML/fop.xconf'
//
// to run TTFReader
// Note that metrics files are optional and no longer required
// java -cp "lib/FOML/fop-1.1/build/fop.jar:lib/FOML/fop-1.1/lib/avalon-framework-4.2.0.jar:lib/FOML/fop-1.1/lib/commons-io-1.3.1.jar:lib/FOML/fop-1.1/lib/commons-logging-1.0.4.jar:lib/FOML/fop-1.1/lib/xmlgraphics-commons-1.4.jar" org.apache.fop.fonts.apps.TTFReader input.ttf output.xml
//
//print_r(array('cmd'=>$cmd,
//'cwd'=>$cwd,
//'env'=>$env)); exit;
$proc = proc_open($cmd, $descriptors, $pipes, $cwd, $env);
if (!is_resource($proc)) {
// TODO - raise exception here?
print "Failure opening process"; exit;
}
fclose($pipes[0]); // close stdin without sending anything
$exit = proc_close($proc);
if ($exit!=0) {
$stdout = file($outFile);
$stderr = file($errFile);
unlink($outFile);
unlink($errFile);
Dump("cmd={$cmd}");
Dump("result={$exit}");
Dump($stdout);
Dump($stderr);
Dump(htmlspecialchars($XslFo));
exit;
}
unlink($outFile);
unlink($errFile);
if (!Foml::$keepTempFiles) unlink($xslFoFileName);
return $pdfFileName;
}
static function RenderToFile($Template, $Args)
{
$xslFo = Foml::GenerateXslFo($Template, $Args);
$pdfFileName = Foml::XslFoToPdf($xslFo);
return $pdfFileName;
}
static function Render($Template, $Args=null, $Headers=null)
{
$pdfFileName = self::RenderToFile($Template, $Args);
$size = filesize($pdfFileName);
$pdfMimeType = Foml::$pdfMimeType;
if ($Headers) {
foreach ($Headers as $key=>$value) {
header("{$key}: {$value}");
}
}
header("Content-Length: {$size}");
header("Content-Type: {$pdfMimeType}");
$fileHandle = fopen($pdfFileName, "rb");
fpassthru($fileHandle);
fclose($fileHandle);
if (!Foml::$keepTempFiles) unlink($pdfFileName);
}
static function RenderInline($Template, $Args=null)
{
$headers = array("Content-Disposition"=>"inline");
Foml::Render($Template, $Args, $headers);
}
static function RenderAttachment($Template, $Filename, $Args=null)
{
$headers = array("Content-Disposition: attachment; filename=\"{$Filename}\"");
Foml::Render($Template, $Args, $headers);
}
static function FormatText($Text)
{
$xlsFo = "";
foreach (preg_split('/(\r\n)|(\n\r)|\n|\r/', $Text) as $line) {
$xlsFo.= "<fo:block>".self::XmlEntities($line)."</fo:block>";
}
return $xlsFo;
}
}
?>