]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/pdf.php
fix pagename for lists
[SourceForge/phpwiki.git] / lib / pdf.php
1 <?php // -*-php-*-
2 rcs_id('$Id: pdf.php,v 1.11 2007-02-17 14:14:55 rurban Exp $');
3 /*
4  Copyright (C) 2003 Olivier PLATHEY
5  Copyright (C) 200? Don Sebà
6  Copyright (C) 2004,2006,2007 Reini Urban
7
8  This file is part of PhpWiki.
9
10  PhpWiki is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  PhpWiki is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with PhpWiki; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */ 
24 /*
25  * Credits:
26  * PDF functions taken from FPDF http://www.fpdf.org
27  * Edited for PHPWebthings by Don Sebà 
28  *   Feel free to edit , enhance the module, and please share it at http://www.phpdbform.com
29  *   Keep PHPWT COOL submit your modules/themes/mods, it will help to improve ! :)
30  * Changes for PhpWiki by Reini Urban
31  */
32
33 require_once('lib/fpdf.php');
34
35 // http://phpwiki.sourceforge.net/phpwiki/PhpWikiToDocBookAndPDF
36 // htmldoc or ghostscript + html2ps or docbook (dbdoclet, xsltproc, fop)
37 // http://www.easysw.com/htmldoc
38 //define("USE_EXTERNAL_HTML2PDF", "htmldoc --quiet --format pdf14 --jpeg --webpage --no-toc --no-title %s");
39
40 class PDF extends FPDF {
41     var $B = 0;
42     var $I = 0;
43     var $U = 0;
44     var $HREF = '';
45
46     function PDF ($orientation='P', $unit='mm', $format='A4') {
47         $this->FPDF($orientation,$unit,$format);
48         //$this->SetCompression(false);
49     }
50
51     // Simple HTML to PDF converter
52     function ConvertFromHTML($html) {
53         $html = str_replace("\n",' ',$html);
54         $a = preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
55         foreach($a as $i=>$e) {
56             if ($i % 2 == 0) {
57                 //Text
58                 if($this->HREF)
59                     $this->PutLink($this->HREF,$e);
60                 else
61                     $this->Write(5,$e);
62             } else {
63                 //Tag
64                 if ($e{0} == '/')
65                     $this->CloseTag(strtoupper(substr($e,1)));
66                 else {
67                     //Attributes
68                     $a2 = explode(' ',$e);
69                     $tag = strtoupper(array_shift($a2));
70                     $attr = array();
71                     foreach ($a2 as $v)
72                         if (ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3))
73                             $attr[strtoupper($a3[1])]=$a3[2];
74                     $this->OpenTag($tag,$attr);
75                 }
76             }
77         }
78     }
79
80     function Header() {
81         $this->SetY(-15);
82         $this->SetFont('Arial','',9);
83         //URL - space from side - space from top - width
84         if (!DEBUG) {
85           $imgurl = $GLOBALS['WikiTheme']->_findFile("images/logo.png"); // header and wikilogo
86           if ($imgurl)
87             $this->Image($imgurl,3,3);
88         }
89         //Line break
90         //$this->Ln(30);
91     }
92
93     function Footer() {
94         //global $cfg, $config, $lang;
95         //1.5cm below top
96         $this->SetY(-15);
97         //Arial italic 8
98         $this->SetFont('arial','I',8);
99     }
100
101     function OpenTag($tag,$attr) {
102         if($tag=='B' or $tag=='I' or $tag=='U')
103             $this->SetStyle($tag,true);
104         if($tag=='A')
105             $this->HREF=$attr['HREF'];
106         if($tag=='BR')
107             $this->Ln(5);
108     }
109
110     function CloseTag($tag) {
111         if($tag=='B' or $tag=='I' or $tag=='U')
112             $this->SetStyle($tag,false);
113         if($tag=='A')
114             $this->HREF='';
115     }
116     
117     //Wijzig stijl en selecteer lettertype
118     function SetStyle($tag,$enable) {
119         $this->$tag+=($enable ? 1 : -1);
120         $style='';
121         foreach(array('B','I','U') as $s)
122             if($this->$s > 0)
123                 $style .= $s;
124         $this->SetFont('',$style);
125     }
126
127     function PutLink($URL,$txt) {
128         // hyperlink as simple underlined text
129         $this->SetTextColor(0,0,255);
130         $this->SetStyle('U',true);
131         $this->Write(5,$txt,$URL);
132         $this->SetStyle('U',false);
133         $this->SetTextColor(0);
134     }
135 }
136
137 function ConvertAndDisplayPdfPageList (&$request, $pagelist) {
138     global $WikiTheme;
139     if (empty($request->_is_buffering_output))
140         $request->buffer_output(false/*'nocompress'*/);
141     $pagename = $request->getArg('pagename');
142     $dest = $request->getArg('dest');
143     $request->setArg('dest',false);
144     $request->setArg('format',false);
145     include_once("lib/display.php");
146
147     // Disable CACHE
148     //while ($page = $pagelist->next())
149     foreach ($pagelist->_pages as $page_handle) {
150         $WikiTheme->DUMP_MODE = true;
151         
152         $request->setArg('action','pdf'); // to omit cache headers
153         $request->setArg('pagename',$page_handle->getName()); // to omit cache headers
154         displayPage($request, new Template('htmldump', $request));
155         $html = ob_get_contents();
156         $WikiTheme->DUMP_MODE = false;
157         $request->discardOutput();
158         $request->buffer_output(false/*'nocompress'*/);
159     
160         // check hook for external converters
161         if (USE_EXTERNAL_HTML2PDF) {
162             // See http://phpwiki.sourceforge.net/phpwiki/PhpWikiToDocBookAndPDF
163             // htmldoc or ghostscript + html2ps or docbook (dbdoclet, xsltproc, fop)
164             require_once("lib/WikiPluginCached.php");
165             $cache = new WikiPluginCached;
166             $cache->newCache();
167             $tmpfile = $cache->tempnam('pdf').".html";
168             $fp = fopen($tmpfile, "wb");
169             fwrite($fp, $html);
170             fclose($fp);
171             $tmpfiles[] = $tmpfile;
172         } else {
173             // use fpdf:
174             if ($GLOBALS['LANG'] == 'ja') {
175                 include_once("lib/fpdf/japanese.php");
176                 $pdf = new PDF_Japanese;
177             } elseif ($GLOBALS['LANG'] == 'zh') {
178                 include_once("lib/fpdf/chinese.php");
179                 $pdf = new PDF_Chinese;
180             } else {
181                 $pdf = new PDF;
182             }
183             $pdf->Open();
184             $pdf->AddPage();
185             $pdf->ConvertFromHTML($html);
186         }
187     }
188     Header('Content-Type: application/pdf');
189     if (USE_EXTERNAL_HTML2PDF) {
190         passthru(EXTERNAL_HTML2PDF_PAGELIST." ".join(" ", $tmpfiles));
191         foreach($tmpfiles as $f)
192             unlink($f);
193     } else {
194         $pdf->Output($pagename.".pdf", $dest ? $dest : 'I');
195     }
196     if (!empty($errormsg)) {
197         $request->discardOutput();
198     }
199 }
200
201 /*
202  * main action handler: action=pdf
203  * TODO: Multiple pages (pages=names), recurse - all forward linked pages (recurse=1)
204  * TODO: inline cached content: /getimg.php? => image.png
205  *
206  * uses either an external exe, or the bad internal php library
207  */
208 function ConvertAndDisplayPdf (&$request) {
209     global $WikiTheme;
210     if (empty($request->_is_buffering_output))
211         $request->buffer_output(false/*'nocompress'*/);
212     $pagename = $request->getArg('pagename');
213     $dest = $request->getArg('dest');
214     Header('Content-Type: application/pdf');
215     // Disable CACHE
216
217     $WikiTheme->DUMP_MODE = true;
218     include_once("lib/display.php");
219     displayPage($request, new Template('htmldump', $request));
220     $html = ob_get_contents();
221     $WikiTheme->DUMP_MODE = false;
222     
223     // check hook for external converters
224     if (defined('USE_EXTERNAL_HTML2PDF')
225         and USE_EXTERNAL_HTML2PDF)
226     {   // See http://phpwiki.sourceforge.net/phpwiki/PhpWikiToDocBookAndPDF
227         // htmldoc or ghostscript + html2ps or docbook (dbdoclet, xsltproc, fop)
228         $request->discardOutput();
229         $request->buffer_output(false/*'nocompress'*/);
230         require_once("lib/WikiPluginCached.php");
231         $cache = new WikiPluginCached;
232         $cache->newCache();
233         $tmpfile = $cache->tempnam();
234         $fp = fopen($tmpfile, "wb");
235         fwrite($fp, $html);
236         fclose($fp);
237         passthru(sprintf(USE_EXTERNAL_HTML2PDF, $tmpfile));
238         unlink($tmpfile);
239     } else {
240         // use fpdf:
241         if ($GLOBALS['LANG'] == 'ja') {
242             include_once("lib/fpdf/japanese.php");
243             $pdf = new PDF_Japanese;
244         } elseif ($GLOBALS['LANG'] == 'zh') {
245             include_once("lib/fpdf/chinese.php");
246             $pdf = new PDF_Chinese;
247         } else {
248             $pdf = new PDF;
249         }
250         $pdf->Open();
251         $pdf->AddPage();
252         $pdf->ConvertFromHTML($html);
253         $request->discardOutput();
254         $request->buffer_output(false/*'nocompress'*/);
255         $pdf->Output($pagename.".pdf", $dest ? $dest : 'I');
256     }
257     if (!empty($errormsg)) {
258         $request->discardOutput();
259     }
260 }
261
262 // $Log: not supported by cvs2svn $
263 // Revision 1.10  2007/01/07 18:44:39  rurban
264 // Add ConvertAndDisplayPdfPageList
265 //
266 // Revision 1.9  2006/09/06 06:02:05  rurban
267 // omit actionbar from pdf
268 //
269 // Revision 1.8  2006/08/25 22:09:00  rurban
270 // print pdf header earlier
271 //
272 // Revision 1.7  2004/09/22 13:46:26  rurban
273 // centralize upload paths.
274 // major WikiPluginCached feature enhancement:
275 //   support _STATIC pages in uploads/ instead of dynamic getimg.php? subrequests.
276 //   mainly for debugging, cache problems and action=pdf
277 //
278 // Revision 1.6  2004/09/20 13:40:19  rurban
279 // define all config.ini settings, only the supported will be taken from -default.
280 // support USE_EXTERNAL_HTML2PDF renderer (htmldoc tested)
281 //
282 // Revision 1.5  2004/09/17 14:19:02  rurban
283 // default pdf dest: browser
284 //
285 // Revision 1.4  2004/06/14 11:31:37  rurban
286 // renamed global $Theme to $WikiTheme (gforge nameclash)
287 // inherit PageList default options from PageList
288 //   default sortby=pagename
289 // use options in PageList_Selectable (limit, sortby, ...)
290 // added action revert, with button at action=diff
291 // added option regex to WikiAdminSearchReplace
292 //
293 // Revision 1.3  2004/05/15 19:49:09  rurban
294 // moved action_pdf to lib/pdf.php
295 //
296
297 // Local Variables:
298 // mode: php
299 // tab-width: 8
300 // c-basic-offset: 4
301 // c-hanging-comment-ender-p: nil
302 // indent-tabs-mode: nil
303 // End:
304 ?>