The suggested solution to embed the PDF in a frame has the issue that it may not work if the client system has their browser set to not show PDF files in-browser. So this may instead force a download of the PDF or cause other oddities.
However I took the above concept and tackled it from a different side, using the Google Viewer application to do the preview for me. (Google allows the embedding of this, see docs.google.com/viewer for information).
Using this, I can provide previews of not just PDF but also DOC, XLS, PPT, etc.
For those interested here is how it was implemented:
In /application/views/files/file_details_content.php :
<?php
if (substr($file->getFilename(), -3) == 'pdf' || substr($file->getFilename(), -3) == 'doc' || substr($file->getFilename(), -3) == 'xls' || substr($file->getFilename(), -3) == 'ppt' || substr($file->getFilename(), -4) == 'docx' || substr($file->getFilename(), -4) == 'pptx' || substr($file->getFilename(), -4) == 'xlsx'){
echo'<div>';
if($file->getType() != ProjectFiles::TYPE_WEBLINK){
// WAS: $urlpdf=get_url('files', 'download_image', array('id' => $file->getId(), 'inline' => true, 'modtime' => $modtime));
// GG: Instead, need to use a sandbox URL, so that the non-logged in site (google) can access the document using a user token....
$urlpdf= get_sandbox_url("feed", "display_content", array("id" => $file->getId(), "user_id" => logged_user()->getId(), "token" => logged_user()->getTwistedToken()));
}else{
$urlpdf=$file->getUrl();
}
// WAS: echo "<iframe src=".$urlpdf." width='100%' height='900px' frameborder=0 align='center'></iframe>";
// GG: Try google embed: (encode some of the url characters)
$urlpdf = str_replace('&', '%26', $urlpdf);
$urlpdf = str_replace('?', '%3F', $urlpdf);
$urlpdf = str_replace('=', '%3D', $urlpdf);
$urlpdf = str_replace('/', '%2F', $urlpdf);
$urlpdf = str_replace(':', '%3A', $urlpdf);
echo "<iframe src=[blocked]docs.google.com/viewer?embedded=true&url=".$urlpdf." width='100%' height='900px' frameborder=0 align='center'></iframe>";
echo '</div>';
}
?>
I also needed to change
/application/controllers/FileController.class.php :
Edited in function display_content(), to allow proper downloading of PDF etc files :
if ($file->getTypeString() == 'text/html') {
header("Expires: " . gmdate("D, d M Y H:i:s", mktime(date("H") + 2, date("i"), date("s"), date("m"), date("d"), date("Y"))) . " GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Content-Type: " . $file->getTypeString() . $charset);
header("Content-Length: " . (string) strlen($html_content));
print($html_content);
} else {
download_from_repository($file->getLastRevision()->getRepositoryId(), $file->getTypeString(), $file->getFilename(), !$inline);
}
The "print($html_content)" didn't support sending the PDF data, so if not text then it uses download_from_repository instead.
Note that this embed does use the "login via token" feature that seems to exist for the RSS feed support... as such, it contains the same security issue that has been discussed on these forums for the token URLs - if you are not accessing your site via https then there could be over-the-wire leakage of a URL that would allow viewing content without login.