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="index.php?([^"]*)#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!

STAY UP TO DATE

Sign up today to stay informed with industry news & trends