2 rcs_id('$Id: pdf.php,v 1.10 2007-01-07 18:44:39 rurban Exp $');
4 Copyright (C) 2003 Olivier PLATHEY
5 Copyright (C) 200? Don Sebà
6 Copyright (C) 2004 Reini Urban
8 This file is part of PhpWiki.
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.
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.
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
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
33 require_once('lib/fpdf.php');
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");
40 class PDF extends FPDF {
46 function PDF ($orientation='P', $unit='mm', $format='A4') {
47 $this->FPDF($orientation,$unit,$format);
48 //$this->SetCompression(false);
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) {
59 $this->PutLink($this->HREF,$e);
65 $this->CloseTag(strtoupper(substr($e,1)));
68 $a2 = explode(' ',$e);
69 $tag = strtoupper(array_shift($a2));
72 if (ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3))
73 $attr[strtoupper($a3[1])]=$a3[2];
74 $this->OpenTag($tag,$attr);
82 $this->SetFont('Arial','',9);
83 //URL - space from side - space from top - width
85 $imgurl = $GLOBALS['WikiTheme']->_findFile("images/logo.png"); // header and wikilogo
87 $this->Image($imgurl,3,3);
94 //global $cfg, $config, $lang;
98 $this->SetFont('arial','I',8);
101 function OpenTag($tag,$attr) {
102 if($tag=='B' or $tag=='I' or $tag=='U')
103 $this->SetStyle($tag,true);
105 $this->HREF=$attr['HREF'];
110 function CloseTag($tag) {
111 if($tag=='B' or $tag=='I' or $tag=='U')
112 $this->SetStyle($tag,false);
117 //Wijzig stijl en selecteer lettertype
118 function SetStyle($tag,$enable) {
119 $this->$tag+=($enable ? 1 : -1);
121 foreach(array('B','I','U') as $s)
124 $this->SetFont('',$style);
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);
137 function ConvertAndDisplayPdfPageList (&$request, $pagelist) {
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");
148 //while ($page = $pagelist->next())
149 foreach ($pagelist->_pages as $page_handle) {
150 $WikiTheme->DUMP_MODE = true;
152 $request->setArg('action','pdf'); // to omit cache headers
153 displayPage($request, new Template('htmldump', $request));
154 $html = ob_get_contents();
155 $WikiTheme->DUMP_MODE = false;
156 $request->discardOutput();
157 $request->buffer_output(false/*'nocompress'*/);
159 // check hook for external converters
160 if (USE_EXTERNAL_HTML2PDF) {
161 // See http://phpwiki.sourceforge.net/phpwiki/PhpWikiToDocBookAndPDF
162 // htmldoc or ghostscript + html2ps or docbook (dbdoclet, xsltproc, fop)
163 require_once("lib/WikiPluginCached.php");
164 $cache = new WikiPluginCached;
166 $tmpfile = $cache->tempnam('pdf').".html";
167 $fp = fopen($tmpfile, "wb");
170 $tmpfiles[] = $tmpfile;
173 if ($GLOBALS['LANG'] == 'ja') {
174 include_once("lib/fpdf/japanese.php");
175 $pdf = new PDF_Japanese;
176 } elseif ($GLOBALS['LANG'] == 'zh') {
177 include_once("lib/fpdf/chinese.php");
178 $pdf = new PDF_Chinese;
184 $pdf->ConvertFromHTML($html);
187 Header('Content-Type: application/pdf');
188 if (USE_EXTERNAL_HTML2PDF) {
189 passthru(EXTERNAL_HTML2PDF_PAGELIST." ".join(" ", $tmpfiles));
190 foreach($tmpfiles as $f)
193 $pdf->Output($pagename.".pdf", $dest ? $dest : 'I');
195 if (!empty($errormsg)) {
196 $request->discardOutput();
201 * main action handler: action=pdf
202 * TODO: Multiple pages (pages=names), recurse - all forward linked pages (recurse=1)
203 * TODO: inline cached content: /getimg.php? => image.png
205 * uses either an external exe, or the bad internal php library
207 function ConvertAndDisplayPdf (&$request) {
209 if (empty($request->_is_buffering_output))
210 $request->buffer_output(false/*'nocompress'*/);
211 $pagename = $request->getArg('pagename');
212 $dest = $request->getArg('dest');
213 Header('Content-Type: application/pdf');
216 $WikiTheme->DUMP_MODE = true;
217 include_once("lib/display.php");
218 displayPage($request, new Template('htmldump', $request));
219 $html = ob_get_contents();
220 $WikiTheme->DUMP_MODE = false;
222 // check hook for external converters
223 if (defined('USE_EXTERNAL_HTML2PDF')
224 and USE_EXTERNAL_HTML2PDF)
225 { // See http://phpwiki.sourceforge.net/phpwiki/PhpWikiToDocBookAndPDF
226 // htmldoc or ghostscript + html2ps or docbook (dbdoclet, xsltproc, fop)
227 $request->discardOutput();
228 $request->buffer_output(false/*'nocompress'*/);
229 require_once("lib/WikiPluginCached.php");
230 $cache = new WikiPluginCached;
232 $tmpfile = $cache->tempnam();
233 $fp = fopen($tmpfile, "wb");
236 passthru(sprintf(USE_EXTERNAL_HTML2PDF, $tmpfile));
240 if ($GLOBALS['LANG'] == 'ja') {
241 include_once("lib/fpdf/japanese.php");
242 $pdf = new PDF_Japanese;
243 } elseif ($GLOBALS['LANG'] == 'zh') {
244 include_once("lib/fpdf/chinese.php");
245 $pdf = new PDF_Chinese;
251 $pdf->ConvertFromHTML($html);
252 $request->discardOutput();
253 $request->buffer_output(false/*'nocompress'*/);
254 $pdf->Output($pagename.".pdf", $dest ? $dest : 'I');
256 if (!empty($errormsg)) {
257 $request->discardOutput();
261 // $Log: not supported by cvs2svn $
262 // Revision 1.9 2006/09/06 06:02:05 rurban
263 // omit actionbar from pdf
265 // Revision 1.8 2006/08/25 22:09:00 rurban
266 // print pdf header earlier
268 // Revision 1.7 2004/09/22 13:46:26 rurban
269 // centralize upload paths.
270 // major WikiPluginCached feature enhancement:
271 // support _STATIC pages in uploads/ instead of dynamic getimg.php? subrequests.
272 // mainly for debugging, cache problems and action=pdf
274 // Revision 1.6 2004/09/20 13:40:19 rurban
275 // define all config.ini settings, only the supported will be taken from -default.
276 // support USE_EXTERNAL_HTML2PDF renderer (htmldoc tested)
278 // Revision 1.5 2004/09/17 14:19:02 rurban
279 // default pdf dest: browser
281 // Revision 1.4 2004/06/14 11:31:37 rurban
282 // renamed global $Theme to $WikiTheme (gforge nameclash)
283 // inherit PageList default options from PageList
284 // default sortby=pagename
285 // use options in PageList_Selectable (limit, sortby, ...)
286 // added action revert, with button at action=diff
287 // added option regex to WikiAdminSearchReplace
289 // Revision 1.3 2004/05/15 19:49:09 rurban
290 // moved action_pdf to lib/pdf.php
297 // c-hanging-comment-ender-p: nil
298 // indent-tabs-mode: nil