Extension:Mpdf

MediaWiki extensions manual
OOjs UI icon advanced.svg
Mpdf
Release status: stable
Implementation Parser function
Description Converts current page to PDF and sends it to the browser
Author(s) Pavel Astakhov (pastakhovtalk)
Latest version 1.0 (2020-04-21)
MediaWiki 1.25+
PHP 5.4+
Database changes No
License GNU General Public License 2.0 or later
Download
Example http://test.foxway.org/
$wgMpdfSimpleOutput, $wgMpdfTab, $wgMpdfToolboxLink
Translate the Mpdf extension if it is available at translatewiki.net
Check usage and version matrix.

The Mpdf extension lets you export the printable version of a page as a PDF file. To do so it uses the mPDF PHP library (version 6.1), which does HTML-to-PDF conversion.

Advantages of this extension:

  • no need to install additional components
  • the generated PDF uses the same CSS styles as for rendering the printable version of the page
  • small size of the PDF file
  • supports UTF-8

RequirementsEdit

In order to run this extension, your PHP installation must include the following modules:

  • gd
  • mbstring
  • zlib

DownloadEdit

StandardEdit

First, download the main Mpdf code from here, extract it, and place it as a directory called Mpdf in your /extensions directory.

Then, download the mPDF code from here, extract it, and place it as a directory called mpdf in your extensions/Mpdf/ folder.

Using Git and ComposerEdit

Within the /extensions directory, call a command like the following:

git clone https://gerrit.wikimedia.org/r/mediawiki/extensions/Mpdf.git

Then, you have two options:

  • (Preferred option) Add the line "extensions/Mpdf/composer.json" to the "composer.local.json" file in the root directory of your wiki like e.g.:
{
	"extra": {
		"merge-plugin": {
			"include": [
				"extensions/Mpdf/composer.json"
			]
		}
	}
}
  • Within the newly-created /Mpdf directory, run composer install --no-dev - or, when updating, composer update --no-dev

InstallationEdit

Once you have downloaded all the code, you will have an /mpdf directory (containing the mPDF library) within either $IP/extensions/Mpdf/ or $IP/vendor/mpdf/, depending on how it was downloaded. In either case, make sure that the following subfolders of that directory have write permission for the web server:

  • /ttfontdata/
  • /tmp/
  • /graph_cache/

Then, add the following line to LocalSettings.php:

wfLoadExtension( 'Mpdf' );

ConfigurationEdit

The following variables can be set in LocalSettings.php after the inclusion of the extension:

$wgMpdfSimpleOutput
If set to true, makes the PDF output simpler by including only the contents of the page, and not anything from the skin itself (defaults to false)
$wgMpdfTab
If set to true, adds an action/tab to each page, linking to "action=mpdf" (defaults to false)
$wgMpdfToolboxLink
If set to true, adds a link to "action=mpdf" to the toolbox, usually found in the sidebar (defaults to false)

UsageEdit

The PDF creation is initialised by the mpdf action, so a page can be downloaded as a PDF file directly from a link such as:

https://www.example.org/wiki/index.php?title=Main_Page&action=mpdf

A more generic way of creating the link is to use the "fullurl" parser function and the FULLPAGENAMEE built in variable as in the following example.

[{{fullurl:{{FULLPAGENAMEE}}|action=mpdf}} download this page as a PDF file]

This link can then be added to a template which can be transcluded into any page for downloading as a PDF file.

If you use other skins than Vector and run into problems (e. g. empty PDF documents being created), try to force the Vector skin:

https://www.example.org/wiki/index.php?title=Main_Page&useskin=vector&action=mpdf

CustomizationEdit

Use parser function {{#mpdftags: ... }} to add tags specified in documentation of mpdf class.

Examples
{{#mpdftags: 
pageheader name="MyHeader1" content-left="" content-center="" content-right="My document" header-style="font-family: serif; font-size: 10pt; font-weight: bold; color: #000000;" line="on" |
 pagefooter name="MyFooter1" content-left="{DATE j-m-Y}" content-center="{PAGENO}/{nbpg}" content-right="My document" footer-style="font-family: serif; font-size: 8pt; font-weight: bold; font-style: italic; color: #000000;"
}}

{{#mpdftags:
setpageheader name="MyHeader1" value="on" show-this-page="1" |
setpagefooter name="MyFooter1" value="on"
}}

Raw HTML outputEdit

You can download the file as a single HTML document by setting format=html in the query-string.

Example
https://www.example.org/wiki/index.php?title=Main_Page&action=mpdf&format=html

See alsoEdit