Extension:Pdf Export Dompdf
From MediaWiki.org
|
Pdf Export Dompdf Release status: beta |
|
|---|---|
| Description | Converts current page to PDF and sends to browser, using dompdf (a html to pdf converter written in php5) |
| Author(s) | Andreas Hagmann (AhTalk) |
| MediaWiki | tested with 1.12, might work with others too |
| License | No license specified |
| Download | Code |
Contents |
[edit] Overview
This extension is a modified version of the Pdf_Export Extension. See Extension:Pdf_Export for details.
I modified it because I wanted to do pdf-export on my shared hosting server. It was impossible to install htmldoc on it because I dont't have root access.
Searching the web, I found dompdf a html to pdf converter written in php5.
[edit] Installation
- Install the original Pdf Export like on Extension:Pdf_Export
- Overwrite $IP/extensions/PdfExport/PdfExport.php with the Code below
- Install dompdf
- you can download it from http://www.digitaljunkies.ca/dompdf
- untar dompdf into $IP/extensions/dompdf (details and config at [1])
- add require_once("extensions/PdfExport/PdfExport.php"); to your LocalSettings.php
[edit] Code
Put the following into $IP/extensions/PdfExport/PdfExport.php:
<?php //modified by ah, 20.01.2008 //using dompdf if ( !defined( 'MEDIAWIKI' ) ) { ?> <p>This is the PdfExport extension. To enable it, put </p> <pre>require_once("$IP/extensions/PdfExport/PdfExport.php");</pre> <p>at the bottom of your LocalSettings.php.</p> <?php exit(1); } # HACK WARNING! if you get warnings about headers uncomment this #ob_start(); require_once ("$IP/includes/SpecialPage.php"); # Internationalisation file require_once( 'PdfExport.i18n.php' ); $wgExtensionFunctions[] = 'wfSpecialPdf'; $wgExtensionCredits['specialpage'][] = array( 'name' => 'PdfExport mod. by ah', 'author' =>' Thomas Hempel', 'version' => '1.1.11238 (14-September-2007) ', 'description' => 'renders a page as pdf using pdflib and dompdf', 'url' => 'http://www.mediawiki.org/wiki/Extension:Pdf_Export' ); # Add special page. $wgSpecialPages['PdfPrint'] = array('SpecialPdf'); $wgHooks['SkinTemplateBuildNavUrlsNav_urlsAfterPermalink'][] = 'wfSpecialPdfNav'; $wgHooks['MonoBookTemplateToolboxEnd'][] = 'wfSpecialPdfToolbox'; class SpecialPdf extends SpecialPage { var $title; var $article; var $html; var $parserOptions; var $bhtml; function SpecialPdf() { parent::__construct( 'PdfPrint' ); } function execute( $par ) { global $wgRequest; global $wgOut; global $wgUser; global $wgParser; global $wgScriptPath; global $wgServer; global $IP; $returnStatus = 0; $baseTmpPath = "/tmp"; $os = getenv("SERVER_SOFTWARE"); $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(); $cats = ""; $cat = $parserOutput->getCategoryLinks(); foreach ($cat as $c) { $cats .= "<li>" . utf8_decode ($c) . "</li>"; } if ($cats!="") $cats = "<ul>$cats</ul>"; $author = $article->getUserText(); $date = date ("d.n.Y", strtotime($article->getTimestamp()) ); $now = date ("d.n.Y"); $info = "<table style='width: 100%; border: 1px solid black;'><tr>"; $info .= "<td>Autor: " . $author . "<br />Datum: " . $date . "<br />gespeichert: " . $now . "</td>"; $info .= "<td>$cats</td></tr></table>"; $bhtml = utf8_decode($bhtml); $bhtml = str_replace ($wgScriptPath, $wgServer . $wgScriptPath, $bhtml); $bhtml = str_replace ('/w/',$wgServer . '/w/', $bhtml); $css = "<style>body {padding: 30px;}</style>"; $header_footer = "<script type=\"text/php\"> if ( isset(\$pdf) ) { \$font = Font_Metrics::get_font(\"verdana\"); \$size = 10; \$color = array(0,0,0); \$text_height = Font_Metrics::get_font_height(\$font, \$size); \$foot = \$pdf->open_object(); \$w = \$pdf->get_width(); \$h = \$pdf->get_height(); // Draw a line along the bottom \$y = \$h - 2 * \$text_height - 24; \$pdf->line(16, \$y, \$w - 16, \$y, \$color, 1); \$y += \$text_height; \$text = \"$author, $date\"; \$pdf->text(16, \$y, \$text, \$font, \$size, \$color); \$pdf->close_object(); \$pdf->add_object(\$foot, \"all\"); \$text = \"Seite {PAGE_NUM}/{PAGE_COUNT}\"; \$width = Font_Metrics::get_text_width(\"Page 10/20\", \$font, \$size); \$pdf->page_text(\$w-5 - \$width, \$y, \$text, \$font, \$size, \$color); } </script> "; $html = "<html><head><title>" . utf8_decode($page) . "</title>$css</head><body>$info<h1>" . utf8_decode($title->getText()) . "</h1>" . $bhtml . "$header_footer</body></html>"; //removed lines //added by ah, using dompdf $html = str_replace ("src=\"/images", "src=\"images", $html); $html = str_replace ('<img src="/skins/common/images/magnify-clip.png" width="15" height="11" alt="" />', "", $html); require_once("$IP/extensions/dompdf/dompdf_config.inc.php"); $dompdf = new DOMPDF(); $dompdf->set_base_path("$IP/"); $dompdf->set_paper ("a4"); $dompdf->load_html($html); $dompdf->render(); $dompdf->stream(utf8_decode($page) . ".pdf", array('Attachment'=>0)); } } function wfSpecialPdf() { global $IP, $wgMessageCache; # Add messages global $wgPdfExportMessages; foreach( $wgPdfExportMessages as $lang => $messages ) { $wgMessageCache->addMessages( $messages, $lang ); } } 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'] == '' ) { ?><li id="t-ispdf"><?php echo $monobook->msg( 'pdf_print_link' ); ?></li><?php } else { ?><li id="t-pdf"><?php ?><a href="<?php echo htmlspecialchars( $monobook->data['nav_urls']['pdfprint']['href'] ) ?>"><?php echo $monobook->msg( 'pdf_print_link' ); ?></a><?php ?></li><?php } return true; }
[edit] See also
- Extension:Collection - allows to to build collections from a number of pages. Collections can be edited, persisted and retrieved as PDF. No need to install dompdf or htmldoc.

