DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:dw2pdf

dw2pdf plugin

Compatible with DokuWiki

  • 2013-12-08 "Binky" yes
  • 2013-05-10 "Weatherwax" yes
  • 2012-10-13 "Adora Belle" yes
  • 2012-01-25 "Angua" yes

plugin Export DokuWiki content to PDF

Last updated on
2014-01-06
Provides
Action, Render
Repository
Source
Conflicts with
inlineeditor, subjectindex

Similar to bookcreator, html2pdf, pdfex, xslfo

Tagged with export, pdf

Needed for bookcreator

Download and Installation

Download and install the plugin using the Plugin Manager using the URL given above. Refer to Plugins on how to install plugins manually.

:!: The plugin can't be installed through the plugin manager on pre-Angua releases, because it's too big and the plugin manager will fail while downloading. Please install it manually and make sure the installed directory is named dw2pdf.

Recent Changes

Overview

The goal of this plugin was to provide a simple, ready to go PDF converter that almost faithfully replicates the screen view of your wiki pages (i.e. doesn't convert them to a print-document format like the latex plugin). It also bundles the necessary fonts and libraries (hence the 12M size) so that you should not need to do any configuration to get it up and running as quickly as possible.

Features

  • Uses the mPDF PHP library.
  • Handles different languages (it bundles the large, but relatively complete Unicode font).
  • Easy customizing through headers, footers and CSS
  • Automatic index of bookmarks
  • Use with the BookCreator plugin to create a PDF from only the pages selected.

Installation

To use the plugin, you simply need to call the page passing it a “do=export_pdf” PHP argument. Some templates let the plugin extend the pagetools automatically. For other templates you need to implement this yourself to add a button or similar somewhere in your template design.

Dokuwiki template

Since 2013-12-08 Binky release this plugin add self the export button to the page tools. When required, you can disable it via the Configuration Manager.

When your template is almost equal to the 'dokuwiki' template your template can be whitelisted as well, so that it will show the export button in the pagetools. Please file your whitelist request in the issue tracker.

Pre-Binky releases

Insert:

  <form class="button" method="get" action="<?php wl($ID)?>">
               <div class="no">
               <button type="submit" class="button">
               <img src="<?php echo DOKU_BASE?>lib/images/fileicons/pdf.png" alt="PDF Export" />
              </button>
                <input type="hidden" name="do" value="export_pdf" />
                <input type="hidden" name="rev" value="<?php global $REV; echo $REV?>" />
                <input type="hidden" name="id" value="<?php echo $ID?>" />
             </div>
    </form>

in lib/tpl/dokuwiki/main.php

after

<!-- 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>');
                        ?>

DW2pdf-Icon will be on the floating menu.

Default template

Use the following to add another button in the upper or bottom button row of the default template1)

<form class="button" method="get" action="<?php wl($ID)?>">
  <div class="no">
    <input type="submit" value="Export to PDF" class="button" />
    <input type="hidden" name="do" value="export_pdf" />
    <input type="hidden" name="rev" value="<?php global $REV; echo $REV?>" />
    <input type="hidden" name="id" value="<?php echo $ID?>" />
  </div>
</form>

Or use this for a simple 16×16 icon somewhere in your template:

  <a href="<?php global $REV,$ID; echo exportlink($ID, 'pdf', "rev=$REV")?>">
    <img src="<?php echo DOKU_BASE?>lib/images/fileicons/pdf.png" alt="PDF Export" />
  </a>

You can mix the 2 previous methods, and get a button with an image inside, with this code:

<form class="button" method="get" action="<?php wl($ID)?>">
  <div class="no">
    <button type="submit" class="button">
      <img src="<?php echo DOKU_BASE?>lib/images/fileicons/pdf.png" alt="PDF Export" />
      Export to PDF
    </button>
    <input type="hidden" name="do" value="export_pdf" />
    <input type="hidden" name="rev" value="<?php global $REV; echo $REV?>" />
    <input type="hidden" name="id" value="<?php echo $ID?>" />
  </div>
</form>

Monobook Template

When using the template ”monobook for DokuWiki”, to have an Export PDF tab, in /user/tabs.php, use this code

//PDF plugin: export tab
if (file_exists(DOKU_PLUGIN."dw2pdf/action.php") &&
    !plugin_isdisabled("dw2pdf")){
    $_monobook_tabs["tab-export-pdf"]["text"] = $lang["monobook_tab_exportpdf"];
    $_monobook_tabs["tab-export-pdf"]["href"] = wl(getID(), array("do" => "export_pdf"), false, "&");
}

Add translation to file /lib/tpl/monobook/lang/en/lang.php

$lang["monobook_tab_exportpdf"] = "Export as PDF";

Typo Template

When using the typo template, add this code in lib/tpl/typo/tpl_functions.php just before print ' </ul>' . DOKU_LF;

print '<li>'.tpl_link(wl($ID,'do=export_pdf'), 'Export PDF', 'class="action export_pdf" rel="nofollow"',1).'</li>'.DOKU_LF

How-to auto-add export button in your template

When your template has implemented the trigger for the TEMPLATE_PAGETOOLS_DISPLAY event the dw2pdf plugin can extend your templates pagetools.

In the addbutton() method of action.php a case should be added for the template, with the html of your button. Depending on the layout of your template you like to improve the style.css and the button images as well.

Configuration

The plugin provides a few configuration settings that can be configured in the Configuration Manager.

Output Control

The output option controls if PDF should be opened within the Browser (when a PDF plugin is installed) or should always be downloaded.

Caching

By default, generated PDFs are cached. This includes embedded images. This means the ACLs for embedded images will not be rechecked when a user requests a cached PDF. If this is a problem, you should disable caching.

Template

Chose which PDF template (see below) should be used by default. The template can be overridden with the tpl request variable.

Bookmarks

By default the plugin generates PDF bookmarks for each headline in the source page. You can lower the number here to include only higher level headlines in the bookmarks. Set it 0 to disable bookmarks all together.

Plugin Styles

The plugin automatically uses print.css and pdf.css files of installed 3rd party plugins to style plugin content. Often plugins do not provide such styles or you may actually prefer their screen styles. In that case list the names of these plugins here.

Show export button

Let you disable the toolbar button when it was added automatically to your pagetools.

Plugin Compatibility

To work with the include plugin you have to add dw2pdf to its action_plugin_include supportedModes member variable

var $supportedModes = array('xhtml', 'metadata','dw2pdf'); 

Otherwise you may have problems with caching.

PDF Templates

Templates define the design of the created PDF files and are a good way to easily customize them to your Corporate Identity.

To create a new template, just create a new folder within the plugin's tpl folder and put your header, footers and style definitions in it. See lib/plugins/dw2pdf/tpl/default for an example.

Headers and Footers

The following files can be created and will be used to set headers and footers on odd or even pages. Special headers/footers can be used on the first page of a document. If a file is does not exist the next more generic one will be tried. Eg. if You don't differ between even and odd pages, just the header.html is used.

  • header_odd.html – Header for odd pages
  • header_even.html – Header for even pages
  • header_first.html – Header for the first page
  • header.html – Header for all pages
  • footer_odd.html – Footer for odd pages
  • footer_even.html – Footer for even pages
  • footer_first.html – Footer for the first page
  • footer.html – Footer for all pages
  • citation.html – Citationbox to be printed after each article

You can use all HTML that is understood by mpdf (See http://mpdf1.com/manual/index.php?tid=256)

If you reference image files, be sure to prefix them with the @TPLBASE@ parameter (See Replacements below).

Replacements

The following replacement patterns can be used within the header and footer files.

  • @PAGE@ – current page number in the PDF
  • @PAGES@ – number of all pages in the PDF
  • @TITLE@ – The article's title
  • @WIKI@ – The wiki's title
  • @WIKIURL@URL to the wiki
  • @DATE@ – time when the PDF was created (might be in the past if cached)
  • @BASE@ – the wiki base directory
  • @TPLBASE@ – the PDF template base directory (use to reference images)

Remark about Bookcreator: The page depended replacements are only for citation.html updated for every page. In the headers and footers the ID of the bookmanager page of the Bookcreator is applied.

  • @ID@ – The article's pageID
  • @PAGEURL@URL to the article
  • @UPDATE@ – Time of the last update of the article
  • @QRCODE@ – QR code image pointing to the original page url

Styles

Custom stylings can be provided in the following file of your template:

  • style.css

You can use all the CSS that is understood by mpdf (See http://mpdf1.com/manual/index.php?tid=34)

For developers

If you're a plugin developer and want to make your syntax plugin compatible with the PDF export, here are a few tips:

  • provide a useful print.css and PDF export will probably work out of the box
  • if you want to use special stylings for PDF export provide a pdf.css
  • your HTML and CSS should be simple enough to be understood by mpdf (see documentation))

If you want to provide custom HTML for the PDF export you should check if the renderer is a renderer_plugin_dw2pdf when rendering xhtml.

function render($mode, &$renderer, $data){
    if($mode == 'xhtml') {
       if(is_a($renderer,'renderer_plugin_dw2pdf')){
           // this is the PDF export, render simple HTML here
       }else{
           // this is normal XHTML for Browsers, be fancy here
       }
       return true;
    }
    return false;
}

By default, dw2pdf will fetch embedded images via HTTP. If you want to embed local image files without going through HTTP, you can give their location by writing an img tag using the dw2pdf pseudo protocol:

$R->doc .= '<img src="dw2pdf:///full/path/to/some/image.png">';

Issues and Requests

Please report issues and requests in the issue tracker linked at the top.

Sites using this plugin

Mpdf Workarounds

This isn't a bug caused by dw2pdf so I don't want to submit to the dw2pdf bug tracker, but I have noticed that mpdf can't handle the following CSS (at least for divs):

background-repeat: no-repeat;
background-image: url(http://somelocation);

The symptom is that it won't display the picture in the PDF. Googling turned up nothing - but a workaround is to put your image in the middle of a large transparent PNG (like 1000+). It will still “repeat” you just won't see it. This issue was apparent using the note plugin. — nadamsnadams

2012/11/18 03:25

Linked Images

Not all image format variations are successfully embedded in the PDF, even if they are properly displayed in Dokuwiki. If you get missing images in your output, try stripping the Alpha channel of your (e.g. PNG) images.

Another source for this error is relative pathnames. If you use bookcreator plugin, and some images have relative pathnames (like {{image.png}} or {{.:image.png}}), then these images will show up in screen without problem, but they probably won't show up in PDF.

Some code for bookcreator work is actually inside this dw2pdf plugin, and is wrong for relative paths. You can fix this issue applying this patch to file <dokuwiki>/lib/plugins/dw2pdf/action.php about line 132. It better be like this:

            // loop over all pages
            $idbackup = $ID;
            $cnt = count($list);
            for($n=0; $n<$cnt; $n++){
                $page = $list[$n];
 
                $ID = cleanID($page);
                $html .= p_cached_output(wikiFN($page,$REV),'dw2pdf',$page);
                $html .= $this->page_depend_replacements($template['cite'], cleanID($page));
                if ($n < ($cnt - 1)){
                    $html .= '<pagebreak />';
                }
            }
            $ID = $idbackup;

See 1320 for the causes of this bug. The include plugin is an example of this issue handled correctly; see file <dokuwiki>/lib/plugins/include/helper.php about line 260.

landscape layout for "dw2pdf"

Password protection of generated documents

To set fixed password for generated documents, edit:

dokuwiki/lib/plugins/dw2pdf/mpdf/config.php

and add following lines:

$this->setUserRights();
$this->SetProtection(array('print','copy', 'modify'),'password','password');

where password is your password :) (filips)

iPhone/iPad

when use with iPhone/iPad disable the Setting: Seiten mit gzip komprimiert ausliefern otherwise this plugin will not generate PDF-Files on iPhone/iPad.

1) In /lib/tpl/default/main.php, there are two divs that contain some buttons: <div class=“bar” id=“bar__top”> and <div class=“bar” id=“bar__bottom”>. Just insert the HTML code inside one of the two divs (left or right)
plugin/dw2pdf.txt · Last modified: 2014/03/30 11:44 by 78.247.32.88