/[drupal]/contributions/modules/drutex/drutex_pdf.inc
ViewVC logotype

Contents of /contributions/modules/drutex/drutex_pdf.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.18 - (show annotations) (download) (as text)
Wed Apr 25 14:46:28 2007 UTC (2 years, 7 months ago) by darthsteven
Branch: MAIN
CVS Tags: DRUPAL-5--1-1, DRUPAL-5--1-0, DRUPAL-5--1-3, DRUPAL-5--1-2, HEAD
Branch point for: DRUPAL-5--2, DRUPAL-5, DRUPAL-6--1
Changes since 1.17: +6 -6 lines
File MIME type: text/x-php
Changed css file adding to use drupal_add_css
Various space removals from string concatenation
1 <?php
2 // $Id: drutex_pdf.inc,v 1.17 2006/09/06 04:58:51 dfg Exp $
3
4 /**
5 * @file
6 * PDF filter: Convert node's content to LaTeX/PDF.
7 *
8 * This involves decent HTML-to-LaTeX parsing.
9 */
10
11 /**
12 * Implementation of subhook_info().
13 */
14 function drutex_pdf_info($format = -1) {
15 return (object) array(
16 'title' => t('PDF Generator'),
17 'description' => t('Allows to generate pdf-versions of a node (with decent html2latex features).'),
18 'toggle' => true,
19 'weight' => 6
20 );
21 }
22
23 /**
24 * Implementation of subhook_defaults().
25 */
26 function drutex_pdf_defaults() {
27 $D['drutex_pdf_active'] = true;
28
29 /* relative to drutex/templates/pdf */
30 $D['drutex_template_pdf'] = 'plain.tex';
31
32 $D['drutex_pattern_pdflatex'] = 'cd [TMP_DIR]; TEXINPUTS="[DRUTEX_DIR]//:" pdflatex -interaction=batchmode [NAME].tex'."\n".
33 'cp [TMP_DIR]/[NAME].pdf [IMG_DIR]/[NAME].pdf';
34 return $D;
35 }
36
37 /**
38 * Implementation of subhook_filter_settings().
39 */
40 function drutex_pdf_filter_settings($format = -1) {
41 /* conversion methods */
42 $conversions = array('dvipng' => 'dvipng',
43 'imagemagick' => 'ImageMagick (convert)',
44 'custom' => 'custom');
45
46 $form["drutex_template_pdf_$format"] = array(
47 '#type' => 'select',
48 '#title' => t('Template'),
49 '#options' => _drutex_pdf_get_templates(),
50 '#default_value' => drutex_var_get("drutex_template_pdf_$format")
51 );
52
53 /* Conversion Command */
54
55 /* description for <conversion command> */
56 $description = t('* Every line is executed in sequence, but in its own shell (so path changes aren\'t inherited).') .'<br />'
57 . t('Placeholders available:<br />
58 [NAME] - name of the tex/pdf<br />
59 [TMP_DIR] - temporary dir (automatically cleaned)<br />
60 [IMG_DIR] - directory where the images/pdfs are saved<br />
61 [DRUTEX_DIR] - full path to DruTeX base dir');
62
63
64 $form["drutex_pattern_pdflatex_$format"] = array(
65 '#type' => 'textarea',
66 '#title' => t('Conversion Command'),
67 '#rows' => 3,
68 '#cols' => 50,
69 '#default_value' => drutex_var_get("drutex_pattern_pdflatex_$format"),
70 '#description' => $description
71 );
72
73 return $form;
74 }
75
76 /**
77 * Goto wrapper for drutex_node2pdf().
78 *
79 * Creates the pdf file, if not already done, and redirects the user to this file.
80 *
81 * @param $nid
82 * The node id.
83 */
84 function drutex_node2pdf_wrapper($nid) {
85 $node = node_load($nid);
86 $pdf_dir = drutex_var_get("drutex_dir_images_{$node->format}");
87
88 if (!is_file("$pdf_dir/{$node->nid}.pdf")) {
89 drutex_node2pdf($nid);
90 }
91
92 drupal_goto("$pdf_dir/{$node->nid}.pdf");
93 }
94
95 /**
96 * The node2pdf function.
97 *
98 * Uses node2latex and passes the output to pdfLaTeX.
99 * So the pdf file is stored in the image directory.
100 *
101 * @param $nid
102 * The node id.
103 */
104 function drutex_node2pdf($nid) {
105 $node = node_load($nid);
106
107 drutex_node2latex($nid);
108
109 $image_dir = drutex_var_get("drutex_dir_images_{$node->format}");
110
111 /* create temporary dir */
112 $temporary_dir = _drutex_create_temporary_dir();
113
114 copy("$image_dir/{$node->nid}.tex", "$temporary_dir/{$node->nid}.tex");
115 _drutex_copy_dir("modules/drutex/templates/pdf", $temporary_dir);
116
117
118 /*
119 * Prepare and execute the conversion commands
120 */
121
122 $pattern_convert = drutex_var_get("drutex_pattern_pdflatex_{$node->format}");
123
124 /* replacement array */
125 $map = array(
126 '[NAME]' => $node->nid, '[TMP_DIR]' => $temporary_dir, '[IMG_DIR]' => $image_dir,
127 '[DRUTEX_DIR]' => drutex_var_get('drutex_dir')
128 );
129
130 /* conversion commands (seperated by \n) */
131 $cmd_convert = str_replace(array_keys($map), $map, $pattern_convert);
132
133 /* invoke all conversion commands */
134 $commands = explode("\n", $cmd_convert);
135
136 foreach ($commands as $cmd) {
137 $cmd = trim($cmd);
138
139 if ($cmd) {
140 exec($cmd, $cmd_output, $cmd_retval);
141
142 if (drutex_var_get("drutex_debug_$format")) {
143 $level = ($cmd_retval == 0) ? WATCHDOG_NOTICE : WATCHDOG_WARNING;
144 watchdog('DruTeX', "Command: $cmd<br />Return value: $cmd_retval", $level);
145 }
146 }
147 }
148
149 /* remove temporary dir */
150 if (!drutex_var_get("drutex_debug_$format")) {
151 _drutex_delete_dir($temporary_dir);
152 }
153 else {
154 watchdog('DruTeX', "Temporary directory for pdf-generation was $temporary_dir.", WATCHDOG_NOTICE);
155 }
156 }
157
158 /**
159 * Goto wrapper for drutex_node2latex().
160 *
161 * Creates the tex file, if not already done, and redirects the user to this file.
162 *
163 * @param $nid
164 * The node id.
165 */
166 function drutex_node2latex_wrapper($nid) {
167 $node = node_load($nid);
168 $pdf_dir = drutex_var_get("drutex_dir_images_{$node->format}");
169
170 if (!is_file("$pdf_dir/{$node->nid}.tex")) {
171 drutex_node2latex($nid);
172 }
173
174 drupal_goto("$pdf_dir/{$node->nid}.tex");
175 }
176
177 /**
178 * The node2latex function.
179 *
180 * Wraps the node's content in a LaTeX template, with some preprocessing.
181 *
182 * @param $nid
183 * The node id.
184 * @return
185 * None, but a tex file was put to disk!
186 */
187 function drutex_node2latex($nid) {
188 $node = node_load($nid);
189
190 /* get the pdf template file path (it's a tex file) */
191 $pdf_template_file = drutex_var_get('drutex_dir')
192 .'/templates/pdf/'. drutex_var_get("drutex_template_pdf_{$node->format}");
193
194 /* read this file into buffer */
195 $handle = fopen($pdf_template_file, 'r');
196 $pdf_template_content = fread($handle, filesize($pdf_template_file));
197 fclose($handle);
198
199 /* convert html elements in the node's content to latex */
200 $content = drutex_html2latex($node);
201
202 /* replace the template keys by the actual values */
203 $map = array(
204 'DRUTEX_NODE_TITLE' => $node->title,
205 'DRUTEX_NODE_URL' => url('node/'. $node->nid, null, null, true),
206 'DRUTEX_DIR' => drutex_var_get('drutex_dir'),
207 'DRUTEX_NODE_BODY' => $content
208 );
209
210 $text = str_replace(array_keys($map), $map, $pdf_template_content);
211
212 /* save generated TeX file */
213 $dir = drutex_var_get("drutex_dir_images_{$node->format}");
214 file_save_data($text, "$dir/{$node->nid}.tex", FILE_EXISTS_REPLACE);
215 }
216
217 /**
218 * A simple html2latex / security function.
219 *
220 * @param $node
221 * A node object.
222 * @return
223 * Latex code / security restricted.
224 */
225 function drutex_html2latex(&$node) {
226 if (drutex_submodule_is_active('security', $node->format)) {
227 if (drutex_security($node->body, $format) == false) {
228 return 'Unallowed command detected!';
229 }
230 }
231
232 $text = $node->body;
233
234 /* include the rules file */
235 $drutex_path = drupal_get_path('module', 'drutex');
236 include_once("$drutex_path/drutex_pdf.rules");
237
238 /* get entities */
239 $A = drutex_submodule_invoke_all('drutex2latex', $node, "format=$format");
240 $B = module_invoke_all('drutex2latex', $node);
241 $entities = array_merge($A, $B);
242 usort($entities, '_drutex_weight_sort_cmp');
243
244 /* apply rules */
245 foreach ($entities as $entity) {
246 list($pattern, $replacement) = _drutex_create_regex($entity, $format);
247 $text = preg_replace($pattern, $replacement, $text);
248 }
249
250 return $text;
251 }
252
253 /**
254 * Implementation of hook_nodeapi().
255 */
256 function drutex_pdf_nodeapi(&$node, $op, $arg) {
257 if (true) {
258 switch ($op) {
259 case 'validate':
260 break;
261
262 case 'load':
263 break;
264
265 case 'insert':
266 break;
267
268 case 'update':
269 case 'delete':
270 $pdf_dir = drutex_var_get("drutex_dir_images_{$node->format}");
271
272 if (is_file("$pdf_dir/{$node->nid}.pdf")) {
273 unlink("$pdf_dir/{$node->nid}.pdf");
274 }
275
276 if (is_file("$pdf_dir/{$node->nid}.tex")) {
277 unlink("$pdf_dir/{$node->nid}.tex");
278 }
279
280 break;
281 }
282 }
283 }
284
285 /**
286 * Implementation of hook_perm().
287 */
288 function drutex_pdf_perm() {
289 return array('access pdf files', 'access latex source');
290 }
291
292 /**
293 * Get a list with templates for drutex_pdf.
294 */
295 function _drutex_pdf_get_templates() {
296 $dir = drupal_get_path('module', 'drutex') .'/templates/pdf';
297
298 $A = file_scan_directory($dir, '.*\.tex');
299 $B = array();
300
301 foreach ($A as $key => $val) {
302 $key = substr($key, strlen($dir)+1);
303 $B[$key] = $key;
304 }
305
306 return $B;
307 }

  ViewVC Help
Powered by ViewVC 1.1.2