1<?php
2/**
3 * ODT Plugin: Exports to ODT
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
7 * @author Aurelien Bompard <aurelien@bompard.org>
8 */
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12//require_once DOKU_PLUGIN . 'odt/helper/cssimport.php';
13//require_once DOKU_PLUGIN . 'odt/ODT/ODTDefaultStyles.php';
14
15// Central class for ODT export
16//require_once DOKU_PLUGIN . 'odt/ODT/ODTDocument.php';
17
18/**
19 * The Page Renderer (for PDF format)
20 *
21 * @package DokuWiki\Renderer\Page
22 */
23class renderer_plugin_odt_pagepdf extends renderer_plugin_odt_page {
24
25    /**
26     * Constructor. Loads helper plugins.
27     */
28    public function __construct() {
29        parent::__construct();
30    }
31
32    /**
33     * Returns the format produced by this renderer.
34     */
35    function getFormat(){
36        return "odt_pdf";
37    }
38
39    /**
40     * Initialize the rendering
41     */
42    function document_start() {
43        global $ID;
44
45        // Initialize the document
46        $this->document_setup();
47
48        // Create HTTP headers
49        $output_filename = str_replace(':','-',$ID).'.pdf';
50        $headers = array(
51            'Content-Type' => 'application/pdf',
52            'Cache-Control' => 'must-revalidate, no-transform, post-check=0, pre-check=0',
53            'Pragma' => 'public',
54            'Content-Disposition' => 'attachment; filename="'.$output_filename.'";',
55        );
56
57        // store the content type headers in metadata
58        p_set_metadata($ID,array('format' => array('odt_pagepdf' => $headers) ));
59    }
60
61    /**
62     * Closes the document
63     */
64    function document_end(){
65        parent::document_end();
66        $this->convert();
67    }
68
69    /**
70     * Convert exported ODT file if required.
71     * Supported formats: pdf
72     */
73    protected function convert () {
74        global $ID;
75
76        $format = $this->config->getConvertTo ();
77        if ($format == 'pdf') {
78            // Prepare temp directory
79            $temp_dir = $this->config->getParam('tmpdir');
80            $temp_dir = $temp_dir."/odt/".str_replace(':','-',$ID);
81            if (is_dir($temp_dir)) { io_rmdir($temp_dir,true); }
82            io_mkdir_p($temp_dir);
83
84            // Set source and dest file path
85            $file = $temp_dir.'/convert.odt';
86            $pdf_file = $temp_dir.'/convert.pdf';
87
88            // Prepare command line
89            $command = $this->config->getParam('convert_to_pdf');
90            $command = str_replace('%outdir%', $temp_dir, $command);
91            $command = str_replace('%sourcefile%', $file, $command);
92
93            // Convert file
94            io_saveFile($file, $this->doc);
95            exec ($command, $output, $result);
96            if ($result) {
97                $errormessage = '';
98                foreach ($output as $line) {
99                    $errormessage .= $this->_xmlEntities($line);
100                }
101                $message = $this->getLang('conversion_failed_msg');
102                $message = str_replace('%command%', $command, $message);
103                $message = str_replace('%errorcode%', $result, $message);
104                $message = str_replace('%errormessage%', $errormessage, $message);
105                $message = str_replace('%pageid%', $ID, $message);
106
107                $instructions = p_get_instructions($message);
108                $this->doc = p_render('xhtml', $instructions, $info);
109
110                $headers = array(
111                    'Content-Type' =>  'text/html; charset=utf-8',
112                );
113                p_set_metadata($ID,array('format' => array('odt_pagepdf' => $headers) ));
114            } else {
115                $this->doc = io_readFile($pdf_file, false);
116            }
117            io_rmdir($temp_dir,true);
118        }
119    }
120}
121
122//Setup VIM: ex: et ts=4 enc=utf-8 :
123