HTML2FPDF for Mediawiki
From Meta, a Wikimedia project coordination wiki
(Redirected from HTML2FPDF and Mediawiki)
Contents |
[edit] Introduction
By using HTML2FPDF it is possible to generate PDF pages interactively on the Mediawiki site without the need for extra plugins. Only PHP is required.
[edit] Instructions
To do so, you need to:
- Download HTML2FPDF (According to the documentation this includes a modified FPDF (PHP class for generating PDFs) )
- Get updated html2fpdf.php. (valid for 3.0.2b), reported in HTML2PDF request
- For ease of use, install both in the 'includes' directory of the wiki (I did not investigate in detail how to install them in the extensions directory)
- Update the index.php of your Mediawiki with the following (this code implements the parsing of a 'pdf=yes' setting in the URL:
- somewhere near the top, as shown:
if ($wgRequest->getVal( 'printable' ) == 'yes') {
$wgOut->setPrintable();
}
if ($wgRequest->getVal( 'pdf' ) == 'yes') {
require_once('html2fpdf.php');
// activate Output-Buffer:
ob_start();
}
-
- at the end:
if ($wgRequest->getVal( 'pdf' ) == 'yes') {
// activate Output-Buffer:
// Output-Buffer in variable:
$html=ob_get_contents();
//these 4 lines where added for utf-8 compatibility:
Global $wgOutputEncoding;
if(strcasecmp($wgOutputEncoding, 'utf-8')==0) {
$html=utf8_decode($html);
}
// delete Output-Buffer
ob_end_clean();
header("Content-type: application/pdf");
$pdf = new HTML2FPDF();
$pdf->DisplayPreferences('HideWindowUI');
$pdf->AddPage();
$pdf->setBasePath("./../wi");
$pdf->WriteHTML($html);
$pdf->Output('doc.pdf','I');
}
- Add a pdf button to your page menus (edit the SkinTemplate.php of the include folder, some context shown).
$content_actions['printable'] = array(
'class' => ($action == 'printable') ? 'selected' : false,
'text' => wfMsg('printable'),
'href' => $wgTitle->getLocalUrl( 'printable=yes' )
);
$content_actions['pdf'] = array(
'class' => ($action == 'pdf') ? 'selected' : false,
'text' => wfMsg('pdf'),
'href' => $wgTitle->getLocalUrl( 'printable=yes&pdf=yes' )
);
- In more recent versions of MediaWiki the above code section may not work. So in the case that you cannot find a line like '$content_actions['printable']' in the code, look instead for a line like 'wfProfileOut( "$fname-live" );'. From what I understand, the code section before this function call sets up the 'history' button/tab for a wiki page. Each section ended with 'wfProfileOut' sets up a different button/tab and placing your code after it will display your pdf button after the corresponding button/tab. I also had to make another small change to the button text to make it display cleanly, so my code section looked like this:
$content_actions['pdf'] = array( 'class' => ($action == 'pdf') ? 'selected' : false, 'text' => 'pdf', 'href' => $wgTitle->getLocalUrl( 'printable=yes&pdf=yes' ) );
In Mediawiki 1.6.x I had to change
'href' => $wgTitle->getLocalUrl( 'printable=yes&pdf=yes' )
to
'href' => $this->mTitle->getLocalUrl( 'printable=yes&pdf=yes' )
- Define Mediawiki:pdf (to provide the text for the button)
- Select the Monobook skin. In the monobook skin, the printable version is implicit since the use of the MEDIA setting permits configuration through the CSS (try 'print preview' in your browser). However, for PDF, you need this extra option.
- For the other skins, some other operations are needed.
- In Skin.php,
- find the 'printableLink' function, duplicate it and name the duplication 'pdfLink'. In 'pdfLink' add '&pdf=yes' after 'printable=yes'.
- In the pageTitle function (pageTitleLinks in MediaWiki v1.5b+), add a line like this just before the disclaimer link definition:
- In Skin.php,
$s .= ' | ' . $this->pdfLink();
I believe that this was all that was needed (there might be some problem with the wikiprintable.css regarding #content though).
[edit] Observations
HTML2PDF is maintained by 1 guy and is not really complete yet. I think that it is good to have everything in PHP though. I updated the class to get some level of implementation for cascaded CSS and to understand the 'display : none' property, important for the 'printable version'.
[edit] Initial author of this page
This page initially written by Mario DE WEERD.