Extension:Pdf Export

Here is a simple special page you can add to do pdf export. It works with the open source htmldoc package. From a UI standpoint it plugs in just like SpecialCite.php, and appears as a link in the toolbox. When you click the link, the current page is rendered minus all the navigation stuff and written to a temp file. The temp file is passed through Htmldoc which sends the pdf back to the browser.

The only prerequisite is installing Htmldoc. Put the SpecialPdf.php in your extensions folder, and invoke it from LocalSettings.php: require_once("extensions/SpecialPdf.php");

Then you just need this fairly simple code, which works with 1.6.7, 1.7 and 1.9.0. Obviously the /tmp reference shows its mostly been tried on Linux, but you could easily fix this for Windows too...

 'Pdf',	'author' =>' Thomas Hempel',	'description' => 'prints a page as pdf',	'url' => 'http://www.netapp.com' );

$wgHooks['SkinTemplateBuildNavUrlsNav_urlsAfterPermalink'][] = 'wfSpecialPdfNav'; $wgHooks['MonoBookTemplateToolboxEnd'][] = 'wfSpecialPdfToolbox';

function wfSpecialPdf { global $IP, $wgMessageCache;

$wgMessageCache->addMessages(		array( 'pdfprint' => 'PdfPrint' , 'pdf_print_link' => 'Print as PDF'));

class SpecialPdf extends SpecialPage { var $title; var $article; var $html; var $parserOptions; var $bhtml;

function SpecialPdf { SpecialPage::SpecialPage( 'PdfPrint' ); }		function execute( $par ) { global $wgRequest; global $wgOut; global $wgUser; global $wgParser; global $wgScriptPath; global $wgServer;

$page = isset( $par ) ? $par : $wgRequest->getText( 'page' ); $title = Title::newFromText( $page ); $article = new Article ($title); $wgOut->setPrintable; $wgOut->disable; $parserOptions = ParserOptions::newFromUser( $wgUser ); $parserOptions->setEditSection( false ); $parserOptions->setTidy(true); $wgParser->mShowToc = false; $parserOutput = $wgParser->parse( $article->preSaveTransform( $article->getContent ) ."\n\n",					$title, $parserOptions );

$bhtml = $parserOutput->getText; $bhtml = utf8_decode($bhtml);

$bhtml = str_replace ($wgScriptPath, $wgServer . $wgScriptPath, $bhtml); $bhtml = str_replace ('/w/',$wgServer . '/w/', $bhtml);

$html = "  ". $page. "  " . $bhtml. " ";			// make a temporary directory with an unique name $mytemp = "/tmp/f" .time. "-" .rand. ".html"; $article_f = fopen($mytemp,'w'); fwrite($article_f, $html); fclose($article_f); putenv("HTMLDOC_NOCGI=1"); # Write the content type to the client... header("Content-Type: application/pdf"); header(sprintf('Content-Disposition: attachment; filename="%s.pdf"', $page)); flush;

# if the page is on a HTTPS server and contains images that are on the HTTPS server AND also reachable with HTTP # uncomment the next line #system("perl -pi -e 's/img src=\"https:\/\//img src=\"http:\/\//g' '$mytemp'");

# Run HTMLDOC to provide the PDF file to the user... passthru("htmldoc -t pdf14 --charset iso-8859-1 --color --quiet --jpeg --webpage '$mytemp'"); unlink ($mytemp);

}	}       SpecialPage::addPage (new SpecialPdf); }

function wfSpecialPdfNav( &$skintemplate, &$nav_urls, &$oldid, &$revid ) { $nav_urls['pdfprint'] = array(			'text' => wfMsg( 'pdf_print_link' ),			'href' => $skintemplate->makeSpecialUrl( 'PdfPrint', "page=". wfUrlencode( "{$skintemplate->thispage}" ) )  		); return true; }

function wfSpecialPdfToolbox( &$monobook ) { if ( isset( $monobook->data['nav_urls']['pdfprint'] ) ) if ( $monobook->data['nav_urls']['pdfprint']['href'] == '' ) { ?>msg( 'pdf_print_link' ); ?>data['nav_urls']['pdfprint']['href'] ) ?>">msg( 'pdf_print_link' ); ?>