]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/pdf.php
define all config.ini settings, only the supported will be taken from -default.
[SourceForge/phpwiki.git] / lib / pdf.php
1 <?php // -*-php-*-
2 rcs_id('$Id: pdf.php,v 1.6 2004-09-20 13:40:19 rurban Exp $');
3
4 // PDF functions taken from FPDF http://www.fpdf.org
5 // Edited for PHPWebthings by Don SebĂ  
6 //   Feel free to edit , enhance the module, and please share it at http://www.phpdbform.com
7 //   Keep PHPWT COOL submit your modules/themes/mods, it will help to improve ! :)
8 // Changes for PhpWiki by Reini Urban
9
10 require_once('lib/fpdf.php');
11
12 // http://phpwiki.sourceforge.net/phpwiki/PhpWikiToDocBookAndPDF
13 // htmldoc or ghostscript + html2ps or docbook (dbdoclet, xsltproc, fop)
14 // http://www.easysw.com/htmldoc
15 //define("USE_EXTERNAL_HTML2PDF", "htmldoc --quiet --format pdf14 --no-toc --no-title %s");
16
17 class PDF extends FPDF {
18     var $B = 0;
19     var $I = 0;
20     var $U = 0;
21     var $HREF = '';
22
23     function PDF ($orientation='P', $unit='mm', $format='A4') {
24         $this->FPDF($orientation,$unit,$format);
25         //$this->SetCompression(false);
26     }
27
28     // Simple HTML to PDF converter
29     function ConvertFromHTML($html) {
30         $html = str_replace("\n",' ',$html);
31         $a = preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
32         foreach($a as $i=>$e) {
33             if ($i % 2 == 0) {
34                 //Text
35                 if($this->HREF)
36                     $this->PutLink($this->HREF,$e);
37                 else
38                     $this->Write(5,$e);
39             } else {
40                 //Tag
41                 if ($e{0} == '/')
42                     $this->CloseTag(strtoupper(substr($e,1)));
43                 else {
44                     //Attributes
45                     $a2 = explode(' ',$e);
46                     $tag = strtoupper(array_shift($a2));
47                     $attr = array();
48                     foreach ($a2 as $v)
49                         if (ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3))
50                             $attr[strtoupper($a3[1])]=$a3[2];
51                     $this->OpenTag($tag,$attr);
52                 }
53             }
54         }
55     }
56
57     function Header() {
58         $this->SetY(-15);
59         $this->SetFont('Arial','',9);
60         //URL - space from side - space from top - width
61         if (!DEBUG) {
62           $imgurl = $GLOBALS['WikiTheme']->_findFile("images/logo.png"); // header and wikilogo
63           if ($imgurl)
64             $this->Image($imgurl,3,3);
65         }
66         //Line break
67         //$this->Ln(30);
68     }
69
70     function Footer() {
71         //global $cfg, $config, $lang;
72         //1.5cm below top
73         $this->SetY(-15);
74         //Arial italic 8
75         $this->SetFont('arial','I',8);
76     }
77
78     function OpenTag($tag,$attr) {
79         if($tag=='B' or $tag=='I' or $tag=='U')
80             $this->SetStyle($tag,true);
81         if($tag=='A')
82             $this->HREF=$attr['HREF'];
83         if($tag=='BR')
84             $this->Ln(5);
85     }
86
87     function CloseTag($tag) {
88         if($tag=='B' or $tag=='I' or $tag=='U')
89             $this->SetStyle($tag,false);
90         if($tag=='A')
91             $this->HREF='';
92     }
93     
94     //Wijzig stijl en selecteer lettertype
95     function SetStyle($tag,$enable) {
96         $this->$tag+=($enable ? 1 : -1);
97         $style='';
98         foreach(array('B','I','U') as $s)
99             if($this->$s > 0)
100                 $style .= $s;
101         $this->SetFont('',$style);
102     }
103
104     function PutLink($URL,$txt) {
105         // hyperlink as simple underlined text
106         $this->SetTextColor(0,0,255);
107         $this->SetStyle('U',true);
108         $this->Write(5,$txt,$URL);
109         $this->SetStyle('U',false);
110         $this->SetTextColor(0);
111     }
112 }
113
114 function ConvertAndDisplayPdf (&$request) {
115     if (empty($request->_is_buffering_output))
116         $request->buffer_output(false/*'nocompress'*/);
117     $pagename = $request->getArg('pagename');
118     $dest = $request->getArg('dest');
119
120     include_once("lib/display.php");
121     displayPage($request);
122     $html = ob_get_contents();
123     
124     // check hook for external converters
125     if (defined('USE_EXTERNAL_HTML2PDF')
126         and USE_EXTERNAL_HTML2PDF)
127     {   // See http://phpwiki.sourceforge.net/phpwiki/PhpWikiToDocBookAndPDF
128         // htmldoc or ghostscript + html2ps or docbook (dbdoclet, xsltproc, fop)
129         $request->discardOutput();
130         $request->buffer_output(false/*'nocompress'*/);
131         require_once("lib/WikiPluginCached.php");
132         $cache = new WikiPluginCached;
133         $cache->newCache();
134         $tmpfile = $cache->tempnam();
135         $fp = fopen($tmpfile, "wb");
136         fwrite($fp, $html);
137         fclose($fp);
138         Header('Content-Type: application/pdf');
139         passthru(sprintf(USE_EXTERNAL_HTML2PDF, $tmpfile));
140         unlink($tmpfile);
141     } else {
142         // use fpdf:
143         if ($GLOBALS['LANG'] == 'ja') {
144             include_once("lib/fpdf/japanese.php");
145             $pdf = new PDF_Japanese;
146         } elseif ($GLOBALS['LANG'] == 'zh') {
147             include_once("lib/fpdf/chinese.php");
148             $pdf = new PDF_Chinese;
149         } else {
150             $pdf = new PDF;
151         }
152         $pdf->Open();
153         $pdf->AddPage();
154         $pdf->ConvertFromHTML($html);
155         $request->discardOutput();
156         $request->buffer_output(false/*'nocompress'*/);
157         $pdf->Output($pagename.".pdf", $dest ? $dest : 'I');
158     }
159     if (!empty($errormsg)) {
160         $request->discardOutput();
161     }
162 }
163
164 // $Log: not supported by cvs2svn $
165 // Revision 1.5  2004/09/17 14:19:02  rurban
166 // default pdf dest: browser
167 //
168 // Revision 1.4  2004/06/14 11:31:37  rurban
169 // renamed global $Theme to $WikiTheme (gforge nameclash)
170 // inherit PageList default options from PageList
171 //   default sortby=pagename
172 // use options in PageList_Selectable (limit, sortby, ...)
173 // added action revert, with button at action=diff
174 // added option regex to WikiAdminSearchReplace
175 //
176 // Revision 1.3  2004/05/15 19:49:09  rurban
177 // moved action_pdf to lib/pdf.php
178 //
179
180 // Local Variables:
181 // mode: php
182 // tab-width: 8
183 // c-basic-offset: 4
184 // c-hanging-comment-ender-p: nil
185 // indent-tabs-mode: nil
186 // End:
187 ?>