File: pdf.php

package info (click to toggle)
phpwiki 1.3.14-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 15,716 kB
  • ctags: 23,548
  • sloc: php: 88,295; sql: 1,476; sh: 1,378; perl: 765; makefile: 602; awk: 28
file content (307 lines) | stat: -rwxr-xr-x 9,996 bytes parent folder | download
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php // -*-php-*-
rcs_id('$Id: pdf.php,v 1.11 2007/02/17 14:14:55 rurban Exp $');
/*
 Copyright (C) 2003 Olivier PLATHEY
 Copyright (C) 200? Don Seb
 Copyright (C) 2004,2006,2007 Reini Urban

 This file is part of PhpWiki.

 PhpWiki is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 PhpWiki is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with PhpWiki; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */ 
/*
 * Credits:
 * PDF functions taken from FPDF http://www.fpdf.org
 * Edited for PHPWebthings by Don Seb 
 *   Feel free to edit , enhance the module, and please share it at http://www.phpdbform.com
 *   Keep PHPWT COOL submit your modules/themes/mods, it will help to improve ! :)
 * Changes for PhpWiki by Reini Urban
 */

require_once('lib/fpdf.php');

// http://phpwiki.sourceforge.net/phpwiki/PhpWikiToDocBookAndPDF
// htmldoc or ghostscript + html2ps or docbook (dbdoclet, xsltproc, fop)
// http://www.easysw.com/htmldoc
//define("USE_EXTERNAL_HTML2PDF", "htmldoc --quiet --format pdf14 --jpeg --webpage --no-toc --no-title %s");

class PDF extends FPDF {
    var $B = 0;
    var $I = 0;
    var $U = 0;
    var $HREF = '';

    function PDF ($orientation='P', $unit='mm', $format='A4') {
        $this->FPDF($orientation,$unit,$format);
	//$this->SetCompression(false);
    }

    // Simple HTML to PDF converter
    function ConvertFromHTML($html) {
        $html = str_replace("\n",' ',$html);
        $a = preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
        foreach($a as $i=>$e) {
            if ($i % 2 == 0) {
                //Text
                if($this->HREF)
                    $this->PutLink($this->HREF,$e);
                else
                    $this->Write(5,$e);
            } else {
                //Tag
                if ($e{0} == '/')
                    $this->CloseTag(strtoupper(substr($e,1)));
                else {
                    //Attributes
                    $a2 = explode(' ',$e);
                    $tag = strtoupper(array_shift($a2));
                    $attr = array();
                    foreach ($a2 as $v)
                        if (ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3))
                            $attr[strtoupper($a3[1])]=$a3[2];
                    $this->OpenTag($tag,$attr);
                }
            }
        }
    }

    function Header() {
        $this->SetY(-15);
        $this->SetFont('Arial','',9);
	//URL - space from side - space from top - width
	if (!DEBUG) {
          $imgurl = $GLOBALS['WikiTheme']->_findFile("images/logo.png"); // header and wikilogo
          if ($imgurl)
            $this->Image($imgurl,3,3);
        }
        //Line break
        //$this->Ln(30);
    }

    function Footer() {
        //global $cfg, $config, $lang;
        //1.5cm below top
        $this->SetY(-15);
        //Arial italic 8
        $this->SetFont('arial','I',8);
    }

    function OpenTag($tag,$attr) {
        if($tag=='B' or $tag=='I' or $tag=='U')
            $this->SetStyle($tag,true);
        if($tag=='A')
            $this->HREF=$attr['HREF'];
        if($tag=='BR')
            $this->Ln(5);
    }

    function CloseTag($tag) {
        if($tag=='B' or $tag=='I' or $tag=='U')
            $this->SetStyle($tag,false);
        if($tag=='A')
            $this->HREF='';
    }
    
    //Wijzig stijl en selecteer lettertype
    function SetStyle($tag,$enable) {
        $this->$tag+=($enable ? 1 : -1);
        $style='';
        foreach(array('B','I','U') as $s)
            if($this->$s > 0)
                $style .= $s;
        $this->SetFont('',$style);
    }

    function PutLink($URL,$txt) {
        // hyperlink as simple underlined text
        $this->SetTextColor(0,0,255);
        $this->SetStyle('U',true);
        $this->Write(5,$txt,$URL);
        $this->SetStyle('U',false);
        $this->SetTextColor(0);
    }
}

function ConvertAndDisplayPdfPageList (&$request, $pagelist) {
    global $WikiTheme;
    if (empty($request->_is_buffering_output))
        $request->buffer_output(false/*'nocompress'*/);
    $pagename = $request->getArg('pagename');
    $dest = $request->getArg('dest');
    $request->setArg('dest',false);
    $request->setArg('format',false);
    include_once("lib/display.php");

    // Disable CACHE
    //while ($page = $pagelist->next())
    foreach ($pagelist->_pages as $page_handle) {
	$WikiTheme->DUMP_MODE = true;
	
        $request->setArg('action','pdf'); // to omit cache headers
        $request->setArg('pagename',$page_handle->getName()); // to omit cache headers
	displayPage($request, new Template('htmldump', $request));
        $html = ob_get_contents();
	$WikiTheme->DUMP_MODE = false;
	$request->discardOutput();
	$request->buffer_output(false/*'nocompress'*/);
    
	// check hook for external converters
	if (USE_EXTERNAL_HTML2PDF) {
	    // See http://phpwiki.sourceforge.net/phpwiki/PhpWikiToDocBookAndPDF
	    // htmldoc or ghostscript + html2ps or docbook (dbdoclet, xsltproc, fop)
	    require_once("lib/WikiPluginCached.php");
	    $cache = new WikiPluginCached;
	    $cache->newCache();
	    $tmpfile = $cache->tempnam('pdf').".html";
	    $fp = fopen($tmpfile, "wb");
	    fwrite($fp, $html);
	    fclose($fp);
	    $tmpfiles[] = $tmpfile;
	} else {
	    // use fpdf:
	    if ($GLOBALS['LANG'] == 'ja') {
		include_once("lib/fpdf/japanese.php");
		$pdf = new PDF_Japanese;
	    } elseif ($GLOBALS['LANG'] == 'zh') {
		include_once("lib/fpdf/chinese.php");
		$pdf = new PDF_Chinese;
	    } else {
		$pdf = new PDF;
	    }
	    $pdf->Open();
	    $pdf->AddPage();
	    $pdf->ConvertFromHTML($html);
	}
    }
    Header('Content-Type: application/pdf');
    if (USE_EXTERNAL_HTML2PDF) {
	passthru(EXTERNAL_HTML2PDF_PAGELIST." ".join(" ", $tmpfiles));
	foreach($tmpfiles as $f)
	    unlink($f);
    } else {
        $pdf->Output($pagename.".pdf", $dest ? $dest : 'I');
    }
    if (!empty($errormsg)) {
        $request->discardOutput();
    }
}

/*
 * main action handler: action=pdf
 * TODO: Multiple pages (pages=names), recurse - all forward linked pages (recurse=1)
 * TODO: inline cached content: /getimg.php? => image.png
 *
 * uses either an external exe, or the bad internal php library
 */
function ConvertAndDisplayPdf (&$request) {
    global $WikiTheme;
    if (empty($request->_is_buffering_output))
        $request->buffer_output(false/*'nocompress'*/);
    $pagename = $request->getArg('pagename');
    $dest = $request->getArg('dest');
    Header('Content-Type: application/pdf');
    // Disable CACHE

    $WikiTheme->DUMP_MODE = true;
    include_once("lib/display.php");
    displayPage($request, new Template('htmldump', $request));
    $html = ob_get_contents();
    $WikiTheme->DUMP_MODE = false;
    
    // check hook for external converters
    if (defined('USE_EXTERNAL_HTML2PDF')
        and USE_EXTERNAL_HTML2PDF)
    {   // See http://phpwiki.sourceforge.net/phpwiki/PhpWikiToDocBookAndPDF
        // htmldoc or ghostscript + html2ps or docbook (dbdoclet, xsltproc, fop)
        $request->discardOutput();
        $request->buffer_output(false/*'nocompress'*/);
        require_once("lib/WikiPluginCached.php");
        $cache = new WikiPluginCached;
        $cache->newCache();
        $tmpfile = $cache->tempnam();
        $fp = fopen($tmpfile, "wb");
        fwrite($fp, $html);
        fclose($fp);
        passthru(sprintf(USE_EXTERNAL_HTML2PDF, $tmpfile));
        unlink($tmpfile);
    } else {
        // use fpdf:
        if ($GLOBALS['LANG'] == 'ja') {
            include_once("lib/fpdf/japanese.php");
            $pdf = new PDF_Japanese;
        } elseif ($GLOBALS['LANG'] == 'zh') {
            include_once("lib/fpdf/chinese.php");
            $pdf = new PDF_Chinese;
        } else {
            $pdf = new PDF;
        }
        $pdf->Open();
        $pdf->AddPage();
        $pdf->ConvertFromHTML($html);
        $request->discardOutput();
        $request->buffer_output(false/*'nocompress'*/);
        $pdf->Output($pagename.".pdf", $dest ? $dest : 'I');
    }
    if (!empty($errormsg)) {
        $request->discardOutput();
    }
}

// $Log: pdf.php,v $
// Revision 1.11  2007/02/17 14:14:55  rurban
// fix pagename for lists
//
// Revision 1.10  2007/01/07 18:44:39  rurban
// Add ConvertAndDisplayPdfPageList
//
// Revision 1.9  2006/09/06 06:02:05  rurban
// omit actionbar from pdf
//
// Revision 1.8  2006/08/25 22:09:00  rurban
// print pdf header earlier
//
// Revision 1.7  2004/09/22 13:46:26  rurban
// centralize upload paths.
// major WikiPluginCached feature enhancement:
//   support _STATIC pages in uploads/ instead of dynamic getimg.php? subrequests.
//   mainly for debugging, cache problems and action=pdf
//
// Revision 1.6  2004/09/20 13:40:19  rurban
// define all config.ini settings, only the supported will be taken from -default.
// support USE_EXTERNAL_HTML2PDF renderer (htmldoc tested)
//
// Revision 1.5  2004/09/17 14:19:02  rurban
// default pdf dest: browser
//
// Revision 1.4  2004/06/14 11:31:37  rurban
// renamed global $Theme to $WikiTheme (gforge nameclash)
// inherit PageList default options from PageList
//   default sortby=pagename
// use options in PageList_Selectable (limit, sortby, ...)
// added action revert, with button at action=diff
// added option regex to WikiAdminSearchReplace
//
// Revision 1.3  2004/05/15 19:49:09  rurban
// moved action_pdf to lib/pdf.php
//

// Local Variables:
// mode: php
// tab-width: 8
// c-basic-offset: 4
// c-hanging-comment-ender-p: nil
// indent-tabs-mode: nil
// End:
?>