1<?php
2 /**
3 * Html2Pdf Plugin: Conversion from html to pdf.
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Daniel Stonier <d.stonier@gmail.com>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
12
13require_once (DOKU_PLUGIN . 'action.php');
14
15class action_plugin_html2pdf extends DokuWiki_Action_Plugin
16{
17    /**
18     * Constructor.
19     */
20    function action_plugin_html2pdf(){}
21
22    /**
23     * return some info
24     */
25    function getInfo(){
26        return confToHash(dirname(__FILE__).'/info.txt');
27    }
28
29    /**
30     * Register the events
31     */
32    function register(&$controller) {
33        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert',array());
34    }
35
36
37    function convert(&$event, $param) {
38        global $ACT;
39		global $REV;
40		global $ID;
41		global $conf;
42
43		//$ID = $param[0];
44		if ( $ACT == 'export_pdf' ) {
45		    $event->preventDefault();
46		    $html = header('Content-Type: text/html; charset=utf-8');
47		    $html .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.DOKU_LF;
48		    $html .= ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'.DOKU_LF;
49		    $html .= '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.$conf['lang'].'"'.DOKU_LF;
50		    $html .= ' lang="'.$conf['lang'].'" dir="'.$lang['direction'].'">'.DOKU_LF;
51		    $html .= '<head>'.DOKU_LF;
52		    $html .= '  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'.DOKU_LF;
53		    $html .= '  <title>'.$ID.'</title>'.DOKU_LF;
54		    // load stylesheets but skip the rest of the usual junk generated by tpl_metaheaders
55		    $html .= '  <link rel="stylesheet" media="all" type="text/css" href="/doku/lib/exe/css.php?s=all"/>'.DOKU_LF;
56		    $html .= '  <link rel="stylesheet" media="screen" type="text/css" href="/doku/lib/exe/css.php"/>'.DOKU_LF;
57		    $html .= '</head>'.DOKU_LF;
58		    $html .= '<body style="background:none;">'.DOKU_LF;
59		    $html .= '<div class="dokuwiki">'.DOKU_LF;
60		    $html .= p_wiki_xhtml($ID,$REV,false);
61		    $html .= '</div>'.DOKU_LF;
62		    $html .= '</body>'.DOKU_LF;
63		    $html .= '</html>'.DOKU_LF;
64		    // Replace images with dereferenced, gettable pictures
65		    $pattern = '/<a.*?detail.php\/(.*?)\?.*?width="(.*?)" height="(.*?)".*?<\/a>/';
66		    while ( preg_match($pattern,$html,$matches ) )
67		    {
68		        $link = preg_replace('/:/','/',$matches[1]);
69		        // $matches[0] is the whole matching line
70		        $width = $matches[2];
71		        $height = $matches[3];
72		        //$replace = '<img src="http://snorriheim.dnsdojo.com/doku/data/media/'.$link.'" class="media" alt="dokuwiki image" width="'.$width.'" height="'.$height.'" />';
73		        $replace = '<img src="'.DOKU_URL.'data/media/'.$link.'" class="media" alt="dokuwiki image" width="'.$width.'" height="'.$height.'" />';
74		        $html = preg_replace($pattern,$replace,$html);
75		    }
76		    // Dereference wiki links
77		    $html = str_replace('href="/','href="http://'.$_SERVER['HTTP_HOST'].'/',$html);
78		    $fout = fopen("lib/plugins/html2pdf/tmp/export_pdf.html",'w');
79		    fwrite($fout,$html);
80		    fclose($fout);
81		    // Ghostscript method is method=fastps, FPDF method is method=fpdf
82		    // pdflib method is method=pdflib
83		    header("Location: " .DOKU_URL.'/lib/plugins/html2pdf/html2ps/demo/html2ps.php?URL='.DOKU_URL.'lib/plugins/html2pdf/tmp/export_pdf.html&pixels=1024&media=A4&method=fpdf&output=0&pdfversion=1.3&cssmedia=screen&renderlinks=1&renderimages=1&scalepoints=1&leftmargin=10&rightmargin=10&topmargin=10&bottommargin=10');
84		    //header("Location: " .'../../html2ps/demo/html2ps.php?URL=http://'.$_SERVER['HTTP_HOST'].getBaseURL().'lib/plugins/html2pdf/tmp/export_pdf.html&pixels=1024&media=A4&method=fpdf&output=0&pdfversion=1.3&cssmedia=screen&renderlinks=1&renderimages=1&scalepoints=1&leftmargin=10&rightmargin=10&topmargin=10&bottommargin=10');
85		    die();
86		}
87    }
88}
89?>
90