DokuWiki

It's better when it's simple

User Tools

Site Tools


tips:pdfexport

Export DokuWiki Pages to PDFs

Several methods exist to convert your wiki pages to PDFs. See also the full list of plugins tagged 'PDF'.

Plugins that export directly to PDF:

Other ways to generate PDFs:

  • HTML to PDF converter: HTMLDOC; or
  • Generate ODT documents from your wiki using the ODT plugin, as LibreOffice, OpenOffice, etc. provide means to export to PDF (with working hyperlinks, tables, images and more).

Common changes

In order to export your pages to PDF you'll need to add a button to your wiki. Regardless which PDF converter you use, you need to implement the following changes:

  • Add an Export to PDF button by editing lib/tpl/default/main.php like so:
        <div class="bar" id="bar_top">
          <div class="bar-left" id="bar_topleft">
            <?php tpl_button('edit')?>
            <?php tpl_button('history')?>
            <?php print html_btn('exportpdf',$ID,'',array('do' => 'export_pdf')) ?>  <!-- inserted line -->
          </div> 

    (If you want the button to appear in the bottom bar use id=“bar_bottom”)

  • Add a new string to inc/lang/en/lang.php (or whatever language you use):
    $lang['btn_exportpdf'] = 'Export to PDF'; 
  • In order to display a small PDF icon instead of the above “Export to PDF”, do the following in inc/html.php

Search for this line

$ret .= '<input type="submit" value="'.htmlspecialchars($label).'" class="button" ';

and replace with this.

//This allows the insertion of an icon in the menu bar
if ($name=="exportpdf"){
$ret .= '<input type="image" src="../lib/images/fileicons/pdf.png" value="'.htmlspecialchars($label).'" ';
}else{
  $ret .= '<input type="submit" value="'.htmlspecialchars($label).'" class="button" ';
}

In order to have an export-link below the “Back to top”-link do the following:

  • Add a new string (different from above) to inc/lang/de/lang.php (or whatever language you use):
    $lang['btn_export_pdf'] = 'Als PDF exportieren'; 
  • Add the following line in lib/tpl/dokuwiki/main.php
    <!-- PAGE ACTIONS -->
                <div id="dokuwiki__pagetools">
                    <h3 class="a11y"><?php echo $lang['page_tools']; ?></h3>
                    <div class="tools">
                        <ul>
                            <?php
                                tpl_action('edit',      1, 'li', 0, '<span>', '</span>');
                                tpl_action('revert',    1, 'li', 0, '<span>', '</span>');
                                tpl_action('revisions', 1, 'li', 0, '<span>', '</span>');
                                tpl_action('backlink',  1, 'li', 0, '<span>', '</span>');
                                tpl_action('subscribe', 1, 'li', 0, '<span>', '</span>');
                                tpl_action('top',       1, 'li', 0, '<span>', '</span>');
                                tpl_action('export_pdf',1, 'li', 0, '<span>', '</span>');  <!-- inserted line -->
                            ?>
                        </ul>
                    </div>
                </div>
  • Edit the function tpl_get_action($type) in file inc/template.php and add the following condition:
            case 'media':
                $params['ns'] = getNS($ID);
                break;
    		case 'export_pdf':        <!-- inserted line -->
    			break;            <!-- inserted line -->
            default:
                return '[unknown %s type]';
                break;
        }
        return compact('accesskey', 'type', 'id', 'method', 'params');
    }
  • Create two new export_pdf css-actions for the image in lib/tpl/dokuwiki/css/pagetools.css and add them at the end of the file
    #dokuwiki__pagetools ul li a.export_pdf {
        background-position: right -270px;
    }
    #dokuwiki__pagetools ul li a.export_pdf:before {
        margin-top: -270px;
    }
    #dokuwiki__pagetools ul li a.export_pdf:hover,
    #dokuwiki__pagetools ul li a.export_pdf:active,
    #dokuwiki__pagetools ul li a.export_pdf:focus {
        background-position: right -315px;
    }

Issues & Questions

Q: Where I will find required file /lib/plugins/pdf/renderer.php (and rest of files from /lib/plugins/pdf/) ?

A: If you follow the guide htmldoc then it wont complain about these missing files, as the system will not try to start a “pdf” plugin

html2ps PHP Script

Using the html2ps PHP script requires only little changes and additions to the DokuWiki source code.

  • Install html2ps. (To run the php script you'll need ghostscript or libpdf - install them first!)
  • Add the Export to PDF button as described above.
  • In the function act_export, in inc/actions.php, add this:
      if ($act == 'export_pdf')
      {
     header("Location: " . DOKU_BASE.'html2ps/html2ps.php?URL='.urlencode(wl($ID, '', true)).'&pixels=1024&media=A4&ps2pdf=2&output=0&cssmedia=screen&renderimages=1&scalepoints=1&leftmargin=10&rightmargin=10&topmargin=10&bottommargin=10');
        exit;
      } 

Variations

If you want to remove the wiki structures and send the raw html page to the pdf exporter, edit the above so that

  • urlencode(wl($ID, '', true))

    urlencode(wl($ID, '', true).'&do=export_html')

You may also wish to vary other options for the html2ps command, these can be found in the documentation on the html2ps site. I add renderlinks=1 and method=fastps (for building using ghostscript) to mine.

html2ps discussion

If your Wiki uses HTTPS, then get the patch for html2ps.

Issues

This appears to only work with pages that allow anonymous read access.

GIF images are not embedded into the generated PDF, instead only the first frame is shown.




*Is-someone knows where I can found the html2ps.php file ?. It's not provided with the package. Thanks. The file is in the Demo directory. Use the index file and the single URL convert to test your website before. My Website is Strict XHTML but looks horrible in PDF. Best regards from Germany.

tips/pdfexport.txt · Last modified: 2015-05-06 18:02 by 12.13.14.15

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki