Jump to content

Extension:Mpdf

From mediawiki.org
MediaWiki extensions manual
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.1
MediaWiki >= 1.35.0
Database changes No
License GNU General Public License 2.0 or later
Download
  • $wgMpdfTab
  • $wgMpdfSimpleOutput
  • $wgMpdfToolboxLink
Quarterly downloads 26 (Ranked 107th)
Translate the Mpdf extension if it is available at translatewiki.net

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

Requirements

[edit]

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

  • gd
  • mbstring
  • zlib

Download

[edit]

Standard

[edit]

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 Composer

[edit]

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

Installation

[edit]

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

Configuration

[edit]

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)

Usage

[edit]

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

Customization

[edit]

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 output

[edit]

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 also

[edit]