Joomla PDF links and sh404SEF

One our clients pointed out that the links being generated in the PDFs in Joomla! were relative and not absolute. The links start with “index.php?…blah…blah…” when they should have the domain name in front of the link – and they should also be SEFed. I’ve come up with a pretty good solution. It may not be the best one out there, but it works and it works well.

So if you have this problem, this is what you should do. Copy the block of code below into a function called fixLinks() in this file, /libraries/joomla/documents/pdf/pdf.php

//Replace src links
$base   = JURI::base();
$buffer = $this->getBuffer();

$regex  = '#href="/blog?(_^="]*)#m';
$buffer = preg_replace_callback( $regex, array('plgSystemSEF', 'route'), $buffer );

$protocols = '[a-zA-Z0-9]+:'; //To check for all unknown protocals (a protocol must contain at least one alpahnumeric fillowed by :
$regex     = '#(src|href)="(?!/|'.$protocols.'|#|')([^"]*)"#m';
$buffer    = preg_replace($regex, "$1="$base$2"", $buffer);
$regex     = '#(onclick="window.open('/)(?!/|'.$protocols.'|#)([^/]+[^']*?')#m';
$buffer    = preg_replace($regex, '$1'.$base.'$2', $buffer);

// ONMOUSEOVER / ONMOUSEOUT
$regex 		= '#(onmouseover|onmouseout)="this.src=([']+)(?!/|'.$protocols.'|#|')([^"]+)"#m';
$buffer 	= preg_replace($regex, '$1="this.src=$2'. $base .'$3$4"', $buffer);

// Background image
$regex 		= '#url(['"]?(?!/|'.$protocols.'|#)([^)'"]+)['"]?)#m';
$buffer 	= preg_replace($regex, 'url(''. $base .'$1$2')', $buffer);

// OBJECT <param name="xx", value="yy">
$regex 		= '#<param name="(movie|src|url)" value="(?!/|'.$protocols.'|#|')([^"]*)"#m';
$buffer 	= preg_replace($regex, '<param name="$1" value="'. $base .'$2"', $buffer);

// OBJECT <param value="xx", name="yy">
$regex 		= '#<param value="(?!/|'.$protocols.'|#|')([^"]*)" name="(movie|src|url)"#m';
$buffer 	= preg_replace($regex, '<param value="'. $base .'$1" name="$2"', $buffer);

$this->setBuffer($buffer);

Also, add this into the file /plugins/system/sef.php on line 101:

if(JRequest::getWord('format') == 'pdf')
	$route = str_replace(JURI::root(true), '', JURI::root()).trim($route, '/');

This should not affect those who are using standard Joomla! SEF, although I have not had a chance to test it yet. This fix could also work if the problem happens with other SEF extensions.

Enjoy!

The Core Team
Editorial Staff Members at 'corePHP'
Editorial staff for the Core Technology Blog for 'corePHP' - news, views insights and advice for e-commerce, marketing technology , web design and development.

4 thoughts on “Joomla PDF links and sh404SEF

  1. Deimos

    I’ve done the change to the files…

    Nothing changed on the side, (i purged the sh404sef cache)..

    In the file sef.php the lines around 101 looks like:

    function route( &$matches )
    {
    $original = $matches[0];
    $url = $matches[1];

    $url = str_replace(‘&’,’&’,$url);
    if(JRequest::getWord(‘format’) == ‘pdf’)
    $route = str_replace(JURI::root(true), ”, JURI::root()).trim($route, ‘/’);

    $route = JRoute::_(‘index.php?’.$url);
    return ‘href=”‘.$route;
    }

    Any ideas why it wont work??

    I use Joomla! 1.5.12 + last version of “sh404SEF” + CB & VM

    1. Rafael Corral

      It also depends how you have your plugins organized. Look and play around with the order of the sef plugins, you should only have the Joomla sef plugin and sh404sef plugin one of the two needs to before the other one, and I think Joomla sef is supposed to be before.

  2. Deimos

    I tried to install sh404sef on a clean joomla install, no change..

    It’s the “/plugins/system/sef.php” as it should? (posted above)

    Where can I check the order of the SEF pluging?

    1. Rafael Corral

      The order of the SEF plugins is under the “Plugins Manager”, there you can filter by system plugins.

Comments are closed.