Extension:Embed Document

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manualManual:Extensions
Crystal Clear action run.png
Embed Document

Release status:Extension status experimental

ImplementationTemplate:Extension#type Tag
DescriptionTemplate:Extension#description This extension will show a document (*.pdf, *.doc, *.wav, ...) file inside a MediaWiki page.
Author(s)Template:Extension#username SLohse, DrTrigon
Latest versionTemplate:Extension#version 0.13
MediaWikiTemplate:Extension#mediawiki 1.9 - 1.11 (newer versions should work, too, with perhaps minimal code adjustments - t.b.d.)
Database changesTemplate:Extension#needs-updatephp No
LicenseTemplate:Extension#license CC BY 3.0 (just mention this source)
Download see below
ExampleTemplate:Extension#example <embed_document> /my/path/myfilename.pdf</embed_document>

Translate the Embed Document extension if it is available at translatewiki.net

Check usage and version matrix.

Will show a document like *.pdf, *.doc, *.wav embedded inside a MediaWiki page.


  <embed_document width="50%" height="300">/my/path/myfilename.pdf</embed_document>
  • Note: The path has to be in your wiki's root directory
  • Todo: Want to get rid of this. Aim is to just specify the filename as it has been uploaded to the Wiki.


  1. Copy the code from below into extensions/embed_document.php
  2. Add
    into LocalSettings.php


 * MediaWiki Embed Document extension
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'Embed Document',
        'author' => array('SLohse', 'DrTrigon'),
        'version' => '0.13',
        'url' => 'http://www.mediawiki.org/wiki/Extension:Embed_Document',
        'description' => 'Allows embedding *.pdf, *.doc and *.wav files on a wiki page',
$wgExtensionFunctions[] = 'registerEmbedDocumentHandler';
function registerEmbedDocumentHandler() {
         global $wgParser;
         $wgParser->setHook( 'embed_document', 'embedDocumentHandler' );
function embedDocumentHandler( $input, $argv ) {
    $allowedchars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
                          'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
                          'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
                          'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                          '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                          '_', '/', '.', '-', ':');
    $w = '100%';
    $h = '680';
    foreach( $argv as $name => $value )
        if ($name == 'width')  $w = $value;
        if ($name == 'height') $h = $value;
    if( str_replace($allowedchars, '', $input) == '' ) {
       # note that the following line does not have to run $input through htmlspecialchars($input) because the 'if' condition above already
       # is intended to filter out illegal or dangerous characters that could create a XSS vulnerability
       return "<iframe width=" . $w . " height=" . $h . " src=" . $input . " frameborder=0 framebordercolor=#00000></iframe>";
    } else {
       return "<font color=#aa0000>Error: invalid character sequence between <code>&amp;lt;embed_document&amp;gt;...&amp;lt;/embed_document&amp;gt;</code> markers, allowed
are only<ul><li>a...z</li><li>A...Z</li><li>0...9</li><li>_</li><li>/</li><li>.</li><li>-</li><li>:</li></ul></font>";

User Suggestions[edit]

User:CharlesC: Create a Template for PDF Embedding[edit]

If you are using mediawiki version 1.12 (works only here, because Extension:Filepath and Extension:TagParser are needed), it would probably be a good idea to create a template for simple document embedding.

  1. follow Creating template for handling PDF files
  2. open the page Template:Pdf_embed in you wiki, edit and insert this code:
<div style="color:#000000; border:solid 1px #A8A8A8; padding:0.0em 0.em 0.0em 0.em; background-color:#FFFFFF;font-size:80%; vertical-align:middle; width:50%">
{| width="100%"
|align="right" | {{pdf|{{{1}}}|{{{2}}}}}
| {{#tag:embed_document|{{filepath:{{{1}}}}}|height="450"}}

Now you can use this template to add a pretty looking box (width 50%, height 450) with link and embedded PDF file to your wiki pages. And there is no need to specify the full path anymore!

{{pdf_embed|Example PDF document.pdf|Example PDF document}}

A good idea! However, computing the filename on the server should probably be moved over into the PHP code sometime.

See Also[edit]