DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:pdfex

PDFex - export PDF via Action Plugin

Compatible with DokuWiki

2006-11-06 and later

plugin exporting a wiki page as PDF

Last updated on
2008-05-04
Provides
Action

This extension has not been updated in over 2 years. It may no longer be maintained or supported and may have compatibility issues.

Tagged with !discontinued, export, pdf

What does this plugin do?

On former :plugin:pdfdownload Chris told me about using an action plugin instead of my obfuscating the template's code ;-) So I did a quick hack, and already got something like a working version.

The plugin creates a PDF file in data/pdfex, which is named like the page you see. On each visit of a page, the plugin compares the timestamps of the PDF file (if exists) and the textfile with the page's content. When there is no PDF file or the page file is newer that the PDF file, a new PDF file is created.

The plugin can be taken as a piece of sample code how to export the content of a page into another format. All you have to do for this is to replace the function that calls the PDF converting routine with a function that converts into another format (LaTeX for example).

Note on 2013-01-15: because of job/time problems I rarely look at his page :-(. The plugin was written about 5 years ago, and I hardly remember anything from the code, so I can hardly offer any support. My own websites use plugin:dw2pdf instead.

How to make it usable

Since the plugin does not open a popup with the PDF file but stores the file on the server, you have to modify your template's main.php in order to

  1. Provide info for the plugin where to store the PDF file, and
  2. Pass the page content to the plugin so that it can be output as PDF.

In lib/tpl/<yourTemplate>/main.php you have to add a piece of code somewhere near the beginning. Somewhere after the beginning of the <body> section will do. This piece of code provides the URL for the PDF file.

<?php
    global $ID;
    $linkname = '';
    $pdfresFILE = 'data/pdfex/PDF_'. str_replace(':', '_', $ID) . '.pdf';
    $linkname = DOKU_BASE . "$pdfresFILE";
?>

At some place on the page where you think it will look nice ;-) you put a link to the PDF file. This may be done by code like:

<a href="<?php print $linkname; ?>"><?php print $lang['pdfex_link_title']; ?></a>

The second code must follow the code mentioned first. It may follow the first directly, so you have all changes to your template at one place.

The content of $lang['pdfex_link_title'] is defined in lang/en/lang.php, see below.

Files of the plugin

The action plugin is named pdfex, so all the files must be kept in lib/plugins/pdfex, The plugin consists of the files

The files I created are available as download from http://www.wernerflamme.net/lib/exe/fetch.php?media=comp:pdfex.tar.gz. You can use the plugin manager to install from this URL. In addition, you need to download HTML2FPDF and/or TCPDF and store it in subfolder html2fpdf and tcpdf, respectively.

On 2008-05-04, Karel Dusek mentioned that the variable $_SERVER['SERVER_HOME'] does not exist. This is a reason for not working, so I replaced this by dirname(__FILE__) . '/tcpdf/', – it will work now, I hope…


The content of the files is given here:

file: action.php

action.php
<?php
/**
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Werner Flamme <w.flamme@web.de>
 */
 
// must be run within DokuWiki
if(!defined('DOKU_INC'))
  die();
 
if (!defined('DOKU_PLUGIN'))
  define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
require_once(DOKU_PLUGIN . 'action.php');
 
if (!defined('DOKU_PDFEXDIR'))
  define('DOKU_PDFEXDIR', DOKU_INC . 'data/pdfex/');
 
global $conf; // added 2007-06-13
 
if ( !is_dir(DOKU_PDFEXDIR) ) {
/* maybe you have to add some code here:
    if (strlen($conf['dmode']) == 4)
        $mydmode = substr($conf['dmode'], 1);
    elseif (strlen($conf['dmode']) == 3)
        $mydmode = $conf['dmode'];
    else
        $mydmode = '664'; */
/* and you have to use $mydmode instead of $conf['dmode']:
    @mkdir(DOKU_PDFEXDIR, $mydmode);
/* this is due to a difference between the documentation and the real behaviour of mkdir() :-(
*/
    @mkdir(DOKU_PDFEXDIR, $conf['dmode']);
    if ($fh = @fopen(DOKU_PDFEXDIR . '.htaccess', $conf['fmode']) ) {
      @fwrite($fh, "order allow,deny\n");
      @fwrite($fh, "allow from all\n");
      @fclose($fh);
    } // if ($fh)
} // if ( !is_dir(DOKU_PDFEXDIR) )
 
if (!defined('NL'))
  define('NL',"\n");
 
class action_plugin_pdfex extends DokuWiki_Action_Plugin
{
 
  /**
   * Return some info
   */
  function getInfo(){
    return array(
      'author' => 'Werner Flamme',
      'email'  => 'w.flamme@web.de',
      'date'   => '2006-09-24',
      'name'   => 'PDF Export Plugin',
      'desc'   => 'Creates PDF files with the pages\' content',
      'url'    => 'http://www.dokuwiki.org/users:wflamme:pdfex',
    );
  } // function getInfo
 
  /**
   * Register the eventhandlers
   */
  function register(&$contr){
    $contr->register_hook('TPL_CONTENT_DISPLAY',
                          'BEFORE',
                          $this,
                          'handle_act_pdfex',
                           array());
  } // function register
 
 
  /**
    function to fuss around a bit and prepare something for conversion
  **/
  function handle_act_pdfex(&$event, $param)
  {
    global $ACT;
    global $ID;
    global $conf;
 
    require_once(dirname(__FILE__) . '/conf/default.php');
 
    if (strtolower($ACT) != 'show')
      return; // nothing to do for us
 
    // well now, let's look at the data and convert it into a pdf file:
    $filewiki = wikiFN($ID);
    $pdfresultfile = DOKU_PDFEXDIR . 'PDF_' . str_replace(':', '_', $ID) . '.pdf';
    $create_pdffile = true;
    if ( @file_exists($pdfresultfile) ) {
      $create_pdffile = ( filectime($filewiki) > filectime($pdfresultfile) );
      // if the wiki page is newer than the PDF, we have to recreate the PDF
    } // if ( @file_exists($pdfresultfile) )
    if ($create_pdffile === true) {
      $myhtml = $event->data;
      $paraArr = array( 'OUTPUT_NAME' => $pdfresultfile);
      $paraArr['PDF_AUTHOR'] = ($conf['pdfex_c_auth'] == 1) ? $conf['pdfex_author'] : $this->_getAuthorFromMeta($ID);
      switch ($conf['pdfex_method']) {
        case 'tcpdf':
          $paraArr['PDF_HEADER_STRING'] = "$ID";
          $paraArr['PDF_HEADER_TITLE']  = $conf['pdfex_title'];
          $myPDFsize = $this->_tcPDF($myhtml, $paraArr);
          break;
        case 'html2fpdf':
          $myhtml = utf8_decode($myhtml); // not needed for TCPDF
          $myPDFsize = $this->_html2fPDF($myhtml, $paraArr);
          break;
      } // switch (PDFEX_METHOD)
    } // if ($create_pdffile === true)
  } // function handle_act_pdfex
 
  /** 
    presently a dummy function
    @param $pageID page ID to get the author of
  **/
  function _getAuthorFromMeta($pageID)
  {
    return $conf['pdfex_author'];
  } // function _createtempfile
 
  /**
    transforms HTML-Code (without CSS) into a PDF file via TCPDF
 
    @param $htmlcode    HTML-Code to be transformed
    @param $aParam      Array with output parameters
 
  **/
  function _tcPDF($htmlcode, $aParam)
  {
    global $conf;
    require_once(dirname(__FILE__) . '/tcpdf/tcpdf.php');
    include_once(dirname(__FILE__) . '/tcpdf/config/lang/eng.php');
    $defltParam = array(
      'K_PATH_MAIN'           => dirname(__FILE__) . '/tcpdf/',
      'K_PATH_URL'            => 'http://' . $_SERVER['SERVER_NAME'] . '/',
      'K_PATH_TCPDF'          => dirname(__FILE__) . '/tcpdf/',
      'DOC_TITLE'             => 'Document Title',
      'DOC_SUBJECT'           => 'Document Description',
      'DOC_KEYWORDS'          => 'Document keywords',
      'HTML_IS_UNICODE'       => true,
      'HTML_HAS_ENCODING'     => 'UTF-8',
      'PDF_PAGE_ORIENTATION'  => 'portrait',
      'PDF_PAGE_FORMAT'       => 'A4',
      'PDF_CREATOR'           => 'TCPDF (http://tcpdf.sf.net)',
      'PDF_AUTHOR'            => 'anonymous',
      'PDF_HEADER_TITLE'      => 'Title in header line',
      'PDF_HEADER_STRING'     => "rest of\nheader line",
      'PDF_HEADER_LOGO'       => 'logo_example.png',
      'PDF_HEADER_LOGO_WIDTH' => 20,
      'PDF_UNIT'              => 'mm',
      'PDF_MARGIN_HEADER'     =>  5,
      'PDF_MARGIN_FOOTER'     => 10,
      'PDF_MARGIN_TOP'        => 27,
      'PDF_MARGIN_BOTTOM'     => 25,
      'PDF_MARGIN_LEFT'       => 15,
      'PDF_MARGIN_RIGHT'      => 15,
      'PDF_FONT_NAME_MAIN'    => 'FreeSans',
      'PDF_FONT_SIZE_MAIN'    => 10,
      'PDF_FONT_NAME_DATA'    => 'FreeSerif',
      'PDF_FONT_SIZE_DATA'    =>  8,
      'PDF_IMAGE_SCALE_RATIO' =>  4,
      'HEAD_MAGNIFICATION'    =>  1.1,
      'K_CELL_HEIGHT_RATIO'   =>  1.25,
      'K_TITLE_MAGNIFICATION' =>  1.3,
      'K_SMALL_RATIO'         =>  (2/3),
      'OUTPUT_NAME'           => 'default.pdf',
      'OUTPUT_DEST'           => 'F'
    );
    $defltParam['FPDF_FONTPATH']    = $defltParam['K_PATH_TCPDF']  . 'fonts/';
    $defltParam['K_PATH_CACHE']     = $defltParam['K_PATH_MAIN']   . 'cache/';
    $defltParam['K_PATH_URL_CACHE'] = $defltParam['K_PATH_URL']    . 'cache/';
    $defltParam['K_PATH_IMAGES']    = $defltParam['K_PATH_TCPDF']  . 'images/';
    $defltParam['K_BLANK_IMAGE']    = $defltParam['K_PATH_IMAGES'] . '_blank.png';
    $allowedParms = array_keys($defltParam);
 
    foreach ($aParam as $pName => $pValue) {
        if (in_array(strtoupper($pName), $allowedParms))
            $defltParam[$pName] = $pValue;
    } // foreach ($aParam as $pName => $pValue)
 
    define('FPDF_FONTPATH',       $defltParam['FPDF_FONTPATH']);
    define('K_PATH_IMAGES',       $defltParam['K_PATH_IMAGES']);
    define('K_BLANK_IMAGE',       $defltParam['K_BLANK_IMAGE']);
    define('K_CELL_HEIGHT_RATIO', $defltParam['K_CELL_HEIGHT_RATIO']);
    define('K_PATH_CACHE',        $defltParam['K_PATH_CACHE']);
    define('K_PATH_URL_CACHE',    $defltParam['K_PATH_URL_CACHE']);
    define('K_SMALL_RATIO',       $defltParam['K_SMALL_RATIO']);
    $pdf = new TCPDF(   $defltParam['PDF_PAGE_ORIENTATION'],
                        $defltParam['PDF_UNIT'],
                        $defltParam['PDF_PAGE_FORMAT'],
                        $defltParam['HTML_IS_UNICODE'],
                        $defltParam['HTML_HAS_ENCODING']
                    );
    // set document information
    $pdf->SetCreator(   $defltParam['PDF_CREATOR']);
    $pdf->SetAuthor(    $defltParam['PDF_AUTHOR']);
    $pdf->SetTitle(     $defltParam['DOC_TITLE']);
    $pdf->SetSubject(   $defltParam['DOC_SUBJECT']);
    $pdf->SetKeywords(  $defltParam['DOC_KEYWORDS']);
    $pdf->SetHeaderData($defltParam['PDF_HEADER_LOGO'],
                        $defltParam['PDF_HEADER_LOGO_WIDTH'],
                        $defltParam['PDF_HEADER_TITLE'],
                        $defltParam['PDF_HEADER_STRING']
                       );
    //set margins
    $pdf->SetMargins(   $defltParam['PDF_MARGIN_LEFT'],
                        $defltParam['PDF_MARGIN_TOP'],
                        $defltParam['PDF_MARGIN_RIGHT']
                    );
    //set auto page breaks
    $pdf->SetAutoPageBreak(TRUE, $defltParam['PDF_MARGIN_BOTTOM']);
    $pdf->SetHeaderMargin($defltParam['PDF_MARGIN_HEADER']);
    $pdf->SetFooterMargin($defltParam['PDF_MARGIN_FOOTER']);
    //set image scale factor
    $pdf->setImageScale($defltParam['PDF_IMAGE_SCALE_RATIO']);
 
    $pdf->setHeaderFont( array( $defltParam['PDF_FONT_NAME_MAIN'],
                                '',
                                $defltParam['PDF_FONT_SIZE_MAIN']
                              )
                        );
    $pdf->setFooterFont( array( $defltParam['PDF_FONT_NAME_DATA'],
                                '',
                                $defltParam['PDF_FONT_SIZE_DATA']
                              )
                        );
 
    $pdf->setLanguageArray($l); //set language items
 
    //initialize document
    $pdf->AliasNbPages();
 
    $repl_more = array('href', 'src', 'action');
    foreach ($repl_more as $to_replace) {
        $repl_searchfor = '|' . $to_replace . '="/~(.*?)/(.*?)"|';
        $repl_replwith  = $to_replace . '="file:///home/$1/public_html/$2"';
        $htmlcode = preg_replace($repl_searchfor, $repl_replwith, $htmlcode);
        $repl_searchfor = '|' . $to_replace . '="/(.*?)"|';
        $repl_replwith = $to_replace . '="' . DOKU_INC . '$1"';
        $htmlcode = preg_replace($repl_searchfor, $repl_replwith, $htmlcode);
    } // foreach ($repl_more as $to_replace)
    $htmlcode = str_replace('file:///', '/', $htmlcode);
    $repl_searchfor = '/src="' . preg_quote(DOKU_INC, '/') . 'lib\/exe\/fetch.php?(.*?)media=(.+?)"/';
    $matches = array();
    while ( preg_match($repl_searchfor, $htmlcode, $matches) == 1 ) {
      $htmlcode = str_replace($matches[0], ('src="'. DOKU_INC . 'data/media/' .
                  str_replace(':', '/', $matches[2]) . '"'), $htmlcode);
    } // while ( preg_match($repl_searchfor, $htmlcode, $matches) )
 
    $pdf->AddPage();
    $pdf->WriteHTML($htmlcode);
    $pdf->Output(   $defltParam['OUTPUT_NAME'],
                    $defltParam['OUTPUT_DEST']
                );
    chmod($defltParam['OUTPUT_NAME'], $conf['fmode']);
    return filesize($defltParam['OUTPUT_NAME']);
  } // function _tcPDF
 
  /**
    transforms HTML-Code (without CSS) into a PDF file via HTML2FPDF
 
    @param $htmlcode    HTML-Code to be transformed
    @param $aParam      Array with output parameters
 
  **/
  function _html2fPDF($htmlcode, $aParam)
  {
    global $conf;
    define('FPDF_FONTPATH', (dirname(__FILE__) . '/html2fpdf/font/') );
    require_once(dirname(__FILE__) . '/html2fpdf/html2fpdf.php');
 
    $defltParam = array(
      'PDF_PAGE_ORIENTATION'  => 'portrait',
      'PDF_PAGE_FORMAT'       => 'A4',
      'PDF_UNIT'              => 'mm',
      'PDF_AUTHOR'            => 'anonymous',
      'OUTPUT_NAME'           => 'default.pdf',
      'OUTPUT_DEST'           => 'F'
                       );
    $allowedParms = array_keys($defltParam);
 
    foreach ($aParam as $pName => $pValue) {
        if (in_array(strtoupper($pName), $allowedParms))
            $defltParam[$pName] = $pValue;
    } // foreach ($aParam as $pName => $pValue)
    $pdf = new HTML2FPDF( $defltParam['PDF_PAGE_ORIENTATION'],
                          $defltParam['PDF_UNIT'],
                          $defltParam['PDF_PAGE_FORMAT']
                        );
    $pdf->Open();
    $pdf->AddPage();
    $pdf->ShowNOIMG_GIF(false);
    $pdf->WriteHTML($htmlcode);
    $pdf->Output(   $defltParam['OUTPUT_NAME'],
                    $defltParam['OUTPUT_DEST']
                );
    chmod($defltParam['OUTPUT_NAME'], $conf['fmode']);
    return filesize($defltParam['OUTPUT_NAME']);
  } // function _html2fPDF($htmlcode, $aParam)
 
} // class action_plugin_pdfex
 
//Setup VIM: ex: et ts=4 enc=utf-8 :

file: conf/default.php

default.php
<?php
/**
 * Options for the pdfex plugin
 *
 * @author    Werner Flamme <w.flamme@web.de>
 */
 
// the first parameter is set to 1 what means that $conf['pdfex_author'] is used inside the PDF
// as the author's name. As soon as I understand how to access the pages' metadata, there will be
// another valid option to use the "real" author of that page; $conf['pdfex_author'] will be used
// as a fallback solution
$conf['pdfex_c_auth'] = 1;               // use constant author?
$conf['pdfex_author'] = 'Werner Flamme'; // Name of author of current page - e. g. YOUR name :-)
 
// what do we want as the page title?
$conf['pdfex_title']  = $conf['title'];  // Title line of the PDF
 
// do we want to use tcpdf or html2fpdf?
$conf['pdfex_method'] = 'html2fpdf';     // at the moment, you may use 'tcpdf' or 'html2fpdf'
 
//Setup VIM: ex: et ts=2 enc=utf-8 :

file: conf/metadata.php

conf/metadata.php
<?php
/**
 * Metadata for configuration manager plugin
 * Additions for the pdfex plugin
 *
 * @author    Werner Flamme <w.flamme@web.de>
 */
 
$meta['pdfex_c_auth'] = array('onoff');
$meta['pdfex_author'] = array('string');
$meta['pdfex_title']  = array('string');
$meta['pdfex_method'] = array('multichoice', '_choices' => array('html2fpdf', 'tcpdf') );
 
//Setup VIM: ex: et ts=2 enc=utf-8 :

file: lang/en/lang.php

lang/en/lang.php
<?php
/**
 * English language file
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Werner Flamme <w.flamme@web.de>
 */
 
 
// shown in the title attribute of the download link of the file
$lang['pdfex_link_title'] = 'download as PDF';
 
// shown in the alt attribute of the icon to download the file
$lang['pdfex_link_alt']   = 'icon for download as PDF';
 
//Setup VIM: ex: et ts=2 enc=utf-8 :

file: lang/en/settings.php

lang/en/settings.php
<?php
/**
 * English language file
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Werner Flamme <w.flamme@web.de>
 */
 
$lang['pdfex_c_auth'] = 'use constant author';
$lang['pdfex_author'] = 'the author\'s default name';
$lang['pdfex_title']  = 'Title line of the PDF';
$lang['pdfex_method'] = 'html2fpdf or tcpdf';
 
//Setup VIM: ex: et ts=2 enc=utf-8 :

and all the rest...

Well, I do not give all the files of HTML2FPDF or TCPDF here :-). Both have advantages and disadvantages, but I do not want to discuss them here. Maybe I analyze them once and will put them together – but this is a big task for me, though both are based on FPDF

But before I start such a complex work, I will rather take all the configuration from the _tcPDF and _html2fPDF functions into conf/default.php. The code given here is only a quick shot, I took the code from pdfdownload and put the action hook around ;-)

I hope I get some feedback to this way of realization of my PDF export too. I just left some space underneath ;-)

Discussion

Excellent - I'll give this a go. I'm finding some problems with the ACH template though when editing some pages, I'll check out those pages for answers — Peter Cobcroft 2006-09-23 05:38

The pdfex plugin works pretty much out-of-the-box, but I'm not so sure if an action plugin is a good idea for a small server. When pdfex has to regenerate the .pdf file, tcpdf can take as much as two minutes while html2fpdf can take around 30 seconds. I had to increase the max_execution_time in my /etc/php.ini in order to get DokuWiki to display pages without PHP timing out. — bpoint 2006-11-01 11:20

B., you are right, the creation of the PDF page slows down the server :-( However, I do not see that this is caused by the implementation via action plugin (what was not my own idea anyway ;-)) – see pdfdownload how I did it before. Producing “constant” PDF files that “belong” to a certain page reduces the speed only once per changed page. When you have a wiki that is mostly read (and less edited) you do not have the delay on each page again and again.
I think the only way to have the pages be created faster is using external tools as mentioned on pdfexport. I know the problem – but my web host does not allow me to use Ghostscript or other binaries on the machine… — Werner Flamme 2006-11-01 13:57 CET

I am right now using DOMpdf instead of TCPDF or HTML2FPDF, and it works well. It is faster (I do not use pdflib, btw), and supports basic CSS rendering. By telling it to use its own CSS file (I simply use the /lib/exe/css.php?print=1 one) you can customize the output a bit. Last thing, to have images displayed, you either have to rewrap the output properly, or to figure how to use things like preg_replace() on HTML lines that have line breaks inside of them (\n) else some images may fail to display. I'll post the code here whenever I consider it cleaned up ^^

Thank you, 85.176.214.153 :-). I am not sure that DOMpdf will not work on all webhosters since the requirements reads “PHP 5.0.0+ with the DOM extension enabled. Note that the domxml PECL extenstion conflicts with the DOM extension and must be disabled.” That's something you cannot be sure about when you buy webspace only :-( (like I did for www.wernerflamme.name). I have no influence on the PHP modules provided by my web host. But thanks for the hint, and thanks in advance for the clean code :-)Werner Flamme 2006-12-14 12:01 CET

Hi! I've just installed pdfex with html2fpdf inside it, but I get the following error:
Fatal error: Call to undefined method HTML2FPDF::DisplayPreferences() in /opt/lampp/htdocs/dokuwiki/lib/plugins/pdfex/html2fpdf/html2fpdf.php on line 155
Is it a version problem? I'm sorry I'm not a PHP expert… — Vicente 2007-01-08

Hi Vicente, I do not know where this problem comes from. In my code, I find an assignment ($this->DisplayPreferences = '') at the line you give. DisplayPreferences is used as a property, not as a method. And it is declared as a property at the start of the modified class FPDF. The timestamp inside my version of the file fpdf.class.php is 2003-12-30; the html2fpdf.class.php has only a version tag - 3.0beta. — Werner Flamme 2007-01-08 16:06 CET

Hi all, I came across the same error message as Vincente, I changed line 155 in html2fpdf.php to $this->DisplayPreferences = '' but then I got the following message

Parse error: syntax error, unexpected ',' in C:\xampp\htdocs\dokuwiki\lib\plugins\pdfex\html2fpdf\html2fpdf.php on line 1323 \\

Sadly I have no Idea about php… can anyone else help?
I came across some thing else, the html2fpdf ZIP file (as of 2007-06-13 html2fpdf-3.0.2b.zip) I downloaded had a the file named html2fpdf.php whereas your code above mentions html2fpdf.class.php. I renamed the file, but am not sure if there are other reasons. :-? Sebastian 2007-06-13 16:21 CET

Sebastian, now I took a closer look at the code of html2fpdf.php and fpdf.php as the latter is included in the html2fpdf package. Be careful not to include the original fpdf.php from FPDF, since HTML2FPDF needs a modified version. Maybe you have to change a bit of HTML2FPDFs code here, for example change class HTML2FPDF extends FPDF to class HTML2FPDF extends myFPDF in html2fpdf.php and the beginning of fpdf.php should read
if(!class_exists('myFPDF'))
{
define('FPDF_VERSION','1.52');
 
class myFPDF
{

then.

In version 3.0.2beta, line 155 changed from $this->DisplayPreferences = '' to $this->DisplayPreferences('');, and this refers to the method defined in fpdf.php on line 1530.
I did a fresh install of DokuWiki (stable, 2006-11-06), included only the ACH template and the pdfex plugin, and the PDF is created without any error message - after wrestling with the permissions for the data/pdfex directory, which was created with permission 000: I think I read on the mailing list about a PHP bug in function mkdir() that I use to create the data/pdfex output directory. If you get an error message telling you that the PDF file could not be created, please change the code in action.php as follows:
if ( !is_dir(DOKU_PDFEXDIR) ) {
// next lines are inserted
    if (strlen($conf['dmode']) == 4)
        $mydmode = substr($conf['dmode'], 1);
    elseif (strlen($conf['dmode']) == 3)
        $mydmode = $conf['dmode'];
    else
        $mydmode = '664';
//insertion ends here
    @mkdir(DOKU_PDFEXDIR, $mydmode);
    if ($fh = @fopen(DOKU_PDFEXDIR . '.htaccess', $conf['fmode']) ) {
      @fwrite($fh, "order allow,deny\n");
      @fwrite($fh, "allow from all\n");
      @fclose($fh);
    } // if ($fh)
} // if ( !is_dir(DOKU_PDFEXDIR) )
After that modification, the directory is created with '664' permissions. Following the PHP documentation, the parameter must be '0664', but the function does not follow the document :-( (using PHP 5.2.3 here). I hope that plugin will work for you now! — Werner Flamme 2007-06-13 19:22 CEST
I think this code should also work with MediaWiki - ;) cheers, g

Hello again. Some months later, I had the time to solve the DisplayPreferences question, and I did. The problem was that in my LAMP installation (XAMPP 1.5.4), the fpdf.php was included with the package here: /opt/lampp/lib/php/fpdf.php . html2fpdf was using it instead of the one that is included in the package. You can solve it removing it or simply changing it with the modified one included in html2fpdf package. Again, thank you very much Werner for your plugin :-D . — Vicente 2007-07-11 12:10

Hello Vicente, that was what I wrote to Sebastian in the first paragraph of my reply: I thought there may be another version of FPDF interfering… Nice to hear that your problem is solved now :-)Werner Flamme 2007-07-11 13:16 CEST

Hello. This plugin works also with monobook template, but I've no images appear on my PDF pages, is it normal? Thanks. Remi 2007-11-08 16:44 CEST

Hello Remi, I am sorry so say so but the code will not transform pictures into PDF. I tried a long time to change this, but only on some PHP versions I was successful. All newer versions refuse to convert the pics :-( The code to read picture data is nearly the same for the underlying HTML2FPDF and tcPDF, so the problem may be solved via changes in FPDF, since both rely upon this code… — Werner Flamme 2007-11-08 17:31 CET

Hello. I've tried this plugin but clicking 'download as PDF' link would display these: Access forbidden! You don't have permission to access the requested object. It is either read-protected or not readable by the server. Error 403 localhost 03/26/08 16:00:21 Apache/2.2.8 (Win32) DAV/2 mod_ssl/2.2.8 OpenSSL/0.9.8g mod_autoindex_color PHP/5.2.5 what do I need to do? Aaron 2008-26-03 04:07 PHI (GMT +8)

Aaron, in this case your webserver is the culprit ;-) - you have to tell him that he has to allow access to DOKU_PDFEXDIR. By default, this is data/pdfex. You can put a file '.htaccess' inside this directory with the content (2 lines)
Order Allow,Deny
Allow From All

In my case, this helps :-) It may be needed, because in the data directory, the .htaccess file denies direct access to the files there. — Werner Flamme 2008-03-26 CET

Thanks very much Werner.. It works okay now.. :) Aaron 2008-27-03 05:17 PHI (GMT +8)

Hi, I set up my DokuWiki so that the data directory is not under the web root for security reasons. I wasn't sure if it was the best way so I wanted to share it and see if there is a better way. I did the changes to main.php mentioned in pdfexport by adding a print html_btn. I found that this will eventually call the DOKU_INC/inc/actions.php. So in the function act_export I added the following code in. -Bryant 2008-03-04 05:20 EST (GMT -5)

if ($act == 'export_pdf') {
  global $conf;
  global $ID;
  require_once(DOKU_INC.'/conf/local.php');
  $linkname = '';
  $pdfesFILE = $conf['savedir'] .'/pdfex/PDF_'.str_replace(':', '_', $ID) . '.pdf';
  header("Content-type: application/pdf");
  readfile("$pdfesFILE");
  exit;
}
Bryant, thank you very much! — Werner Flamme 2008-04-11

Hi, like Bryant I've set up the DokuWiki (RC1) with the file storage outside the web root. Some small changes in the action.php and I can use this plugin with html2pdf now. Due to the looking of the output I wanted to try tcpdf as the export method. But as soon as I'm opening my DokuWiki I get Following message: 'TCPDF error: Missing or incorrect image file: /tmp/jpg5Ws7AT'

Does this refer to the file storage outside of the DokuWiki path?

Did anyone experience the same error?

Thanks - Bastian 2008/04/11 15:13 (GMT +1)

Bastian, yes, I think tcpdf tells absolute paths. So the file may be “missing” when the open_basedir restriction is set in php.ini. Since I am no expert in using tcpdf, I cannot tell you how to work around this. It may even be the problem that was reported by Remi on 2007-11-08 – some versions of PHP just refuse to work with pictures in the way FPDF does :-(Werner Flamme 2008-04-11

This additional header code will make nice PDF file name for export_pdf:

  header('Content-Disposition: attachment; filename="'.str_replace(':','-',$ID).'.pdf";');

Also, is it possible to have a pdfex option to NOT pre-generate and store the PDF file(s). This would help reduce the extra page load time and storage requirement. — OTU 2008-05-01

When using tcpdf I get an error for any wiki pages containing images. it works fine otherwise. the errors are:

Warning: getimagesize(/var/www//usr/share/dokuwiki/wiki/lib/exe/fetch.php?w=&h=&cache=cache&media=start:dokuwiki-128.png) 
[function.getimagesize]: failed to open stream: No such file or directory in /usr/share/dokuwiki/lib/plugins/pdfex/tcpdf/tcpdf.php on line 3171

Warning: fopen(/var/www//usr/share/dokuwiki/wiki/lib/exe/fetch.php?w=&h=&cache=cache&media=start:dokuwiki-128.png) 
[function.fopen]: failed to open stream: No such file or directory in /usr/share/dokuwiki/lib/plugins/pdfex/tcpdf/tcpdf.php 
on line 3354 TCPDF error: Can't open image file: /var/www//usr/share/dokuwiki/wiki/lib/exe/fetch.php?w=&h=&cache=cache&media=start:dokuwiki-128.png

Obviously the problem is in the path, but how can I fix this? I have DokuWiki installed under /user/share/dokuwiki, but /var/www/ is the Apache DocumentRoot. ⇒ SME 2008-05-30

Hi Sean, you installed DokuWiki outside the document root. You have to edit the first str_replace() inside the while ( preg_match($repl_searchfor, $htmlcode, $matches) == 1 ) loop to have src= point to the correct directory, i.e. without DOKU_INC inside. HTH, Werner Flamme 2008-06-04 09:08 CEST.
Hi Werner, I got the same error, but your workaround doesn't help.
Warning: getimagesize(/web/site/web/dokuwiki/dokuwiki/lib/exe/fetch.php?w=&amp;h=&amp;cache=cache&amp;media=miscellaneous:vnc_server1.png)
[function.getimagesize]: failed to open stream: No such file or directory in /web/site/dokuwiki/lib/plugins/pdfex/tcpdf/tcpdf.php on line 3230

2008-06-10 13:20 CEST

Hi, whoever you are ;-), in my tcpdf.php, line 3230 is a comment. Maybe you can tell me what you have at this place, maybe “end of function…” or s.th. like that? The only getimagesize in my tcpdf.php is in line 3045. And what is the correct path to the picture? What is the content of DOKU_INC? In the $defltParam array, did you replace the value for the array element K_PATH_TCPDF (see my comment beginning with “On 2008-05-04,” just above the file: action.php? HTH, Werner Flamme 2008/06/10 20:18 CEST
PDFex - last version from this site
tcPDF - 3.0.015 from 06/06/2008
DokuWiki: /anwendungen/web/dokuwiki
PDFex: /anwendungen/web/dokuwiki/lib/plugins/pdfex
tcPDF: /anwendungen/web/dokuwiki/lib/plugins/pdfex/tcpdf
The URL anwendungen.domain.de is pointing on /anwendungen/web
The DokuWiki is anwendungen.domain.de/dokuwiki
The Store for the pictures is /anwendungen/web/dokuwiki/data/media/miscellaneous
If I open a wikisite with images I get an error, without images everything is fine.
Error: Warning:getimagesize(/anwendungen/web/anwendungen/web/dokuwiki/dokuwiki/lib/exe/fetch.php?w=&amp;h=&amp;cache=cache&amp;media=miscellaneous:vnc_server1.png) »» [function.getimagesize]: failed to open stream: No such file or directory in /anwendungen/web/dokuwiki/lib/plugins/pdfex/tcpdf/tcpdf.php on line 3230
imagecreatefrompng(/anwendungen/web/anwendungen/web/dokuwiki/dokuwiki/lib/exe/fetch.php?w=&amp;h=&amp;cache=cache&amp;media=miscellaneous:vnc_server1.png) »» [function.imagecreatefrompng]: failed to open stream: No such file or directory in /anwendungen/web/dokuwiki/lib/plugins/pdfex/tcpdf/tcpdf.php on line 3269
In line 3230 in the file tcpdf.php the following command is written: list($pixw, $pixh) = getimagesize($file);
This line is a part of the function Image
The variable $file has the following content:
The main logo: /anwendungen/web/dokuwiki/lib/plugins/pdfex/tcpdf/images/logo.jpg
The Path for the images from dokiwiki: /anwendungen/web/anwendungen/web/dokuwiki/dokuwiki/lib/exe/fetch.php?w=&h=cache=cache&media=miscellaneous:vnc_server1.png
Hans 2008-06-17 14:22 CEST
Hi Werner
I've got then same Errors (PDFex from 2008-05-04, TCPDF 3.0.012_PHP4 from 2008-05-30, DokuWiki 2007-06-26b):
Warning: getimagesize(/home/dani/public_html/home/dani/public_html/menschewiki/menschewiki/lib/exe/fetch.php?w=80&amp;h=60&amp;cache=cache&amp;media=wiki:software:800px-not-aus_betaetiger.jpg)
Warning: imagecreatefromjpeg(/home/dani/public_html/home/dani/public_html/menschewiki/menschewiki/lib/exe/fetch.php?w=80&amp;h=60&amp;cache=cache&amp;media=wiki:software:800px-not-aus_betaetiger.jpg) …
Then I changed the K_PATH_MAIN to dirname(FILE) . '/tcpdf/', the K_PATH_TCPDF was already set to this value. But after changing Action.php I got the same errors as before.
The real Path to the Picture is /home/dani/public_html/menschewiki/lib/exe/fetch.php, but for some reason the path grows up to
/home/dani/public_html/home/dani/public_html/menschewiki/menschewiki/lib/exe/fetch.php
As Hans wrote, without Pictures everything is fine. Thank you for fixing, but also for creating this useful tool.
Dani 2008-07-21 13:23 CEST
I've updated now to DokuWiki 2008-05-05 and TCPDF 4.0.010 [from 2008-07-22]. Now, if there are Pictures in the requested Page, the Plugin 'swallows' the whole content. The only Message is: 'TCPDF error: Empty font family'. With 'html2fpdf' it works still fine. Dani 2008-08-05 19:11 CEST



Hi, I tried switching to tcpdf and now I'm getting “TCPDF error: Wrong page number on setPage() function.”
I'm sure this is due to a change in the most recent version tcpdf so I'm wondering if you have a preferred version of tcpdf I should use? — Ty 2008-10-16 11:41 PDT


Just wondering if anyone have integrated print.css into this plugin? if you have, could you share how? thanks in advance. — 2008-11-02 02:22 est



Remy, 2009-03-09 19:33
When a page does not exist yet, or when a page is being deleted, the plugin prints an error message. Besides that, I would like to delete PDF files of pages which does not exist anymore. Therefore, I altered the code (action.php) a bit:

    // well now, let's look at the data and convert it into a pdf file:
    $filewiki = wikiFN($ID);
 
    $pdfresultfile = DOKU_PDFEXDIR . 'PDF_' . str_replace(':', '_', $ID) . '.pdf';
 
    // Just return if page does not exist yet or is being deleted.
    if ( ! @file_exists($filewiki) ) {
 
        // Delete old PDF file.
        if ( @file_exists($pdfresultfile) )
                @unlink($pdfresultfile);
 
        return;
    }
 
    $create_pdffile = true;
    if ( @file_exists($pdfresultfile) ) {

Josef, 2009/04/15

Multilanguage support Sorry if this sound obvious, but I'm not a PHP guru. I just tried this code:

    <?php
      global $ID;
      $linkname = '';
      $pdfresFILE = 'data/pdfex/PDF_'. str_replace(':', '_', $ID) . '.pdf';
      $linkname = DOKU_BASE . "$pdfresFILE";
      $linktitle = $lang['pdfex_link_title'];
    ?>
    <a href="<?php print $linkname; ?>"><img alt="<?php print $linktitle; ?>" title="<?php print $linktitle; ?>" src="<?php print DOKU_BASE?>/lib/images/fileicons/pdf.png"></a>

However, everything inside alt and title is empty. I guess you have to include lang.php, but how? Even with your original code, it isn't working :-( Next question: if I create a 'de' folder inside lang folder, then I assume that once I get the pdfex_link_title working, then if I change the wiki language to German, it will be translated to what I put under lang/de, right?

plugin/pdfex.txt · Last modified: 2013-01-15 20:15 by 2001:4dd0:ff00:8eb9:221:5cff:fe41:b3e1