Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Re: PDF to view in browser, not download
Bump! Can anyone help me with the problem above? I’ll paypal $20 to whoever comes up with a working but of code for it. Thanks, Phil
Offline
Re: PDF to view in browser, not download
This simple public-side plugin (code borrowed from publish.php) replaces the Content-Type of the served files by the url query mimetype, making the browsers display them, if possible:
register_callback('etc_file_embed', 'file_download');
function etc_file_embed($event, $step, $pre, $rs) {
$mimetype = strtolower(gps('mimetype'));
// pass the hand to textpattern file_download if mimetype is not valid
if(empty($mimetype) || !in_array($mimetype, array('pdf' /*add allowed types here*/))) return;
global $pretext, $file_error, $file_base_path, $filename, $id;
if (!isset($file_error)) {
$filename = sanitizeForFile($filename);
$fullpath = build_file_path($file_base_path,$filename);
if (is_file($fullpath)) {
// discard any error php messages
ob_clean();
$filesize = filesize($fullpath); $sent = 0;
header("Content-Type: application/$mimetype");
header("Content-Length: $filesize");
header('Content-Disposition: inline; filename="'.$filename.'"');
header('Cache-Control: private');
header("Content-Security-Policy: frame-ancestors 'self'");
header('X-Frame-Options: SAMEORIGIN');
@ini_set("zlib.output_compression", "Off");
@set_time_limit(0);
@ignore_user_abort(true);
if ($file = fopen($fullpath, 'rb')) {
while(!feof($file) and (connection_status()==0)) {
echo fread($file, 1024*64); $sent+=(1024*64);
ob_flush();
flush();
}
fclose($file);
// record download
if ((connection_status()==0) and !connection_aborted() ) {
safe_update("txp_file", "downloads=downloads+1", 'id='.intval($id));
log_hit('200');
} else {
$pretext['request_uri'] .= ($sent >= $filesize)
? '#aborted'
: "#aborted-at-".floor($sent*100/$filesize)."%";
log_hit('200');
}
exit(0);
}
} else {
$file_error = 404;
}
}
return;
}
Usage: put this where you want your pdf-file (with id=1) to be embedded (?mimetype=pdf addendum is important here). You might need to replace ?mimetype=pdf with &mimetype=pdf in messy URL mode:
<txp:file_download id="1" />
<embed src='<txp:file_download_link />?mimetype=pdf' type="application/pdf" height="600" width="100%"></embed>
</txp:file_download>
This works also for clean links, while the /path/files/file.pdf solution could fail.
Last edited by etc (2017-01-06 09:50:08)
Offline
#18 2011-11-25 19:41:14
- agritsogap
- Member
- From: Houston TX
- Registered: 2011-11-25
- Posts: 11
Re: PDF to view in browser, not download
philwareham wrote:
Bump! Can anyone help me with the problem above? I’ll paypal $20 to whoever comes up with a working but of code for it. Thanks, Phil
Hey, I just figured out a way to do what you are asking without going javascript… I don’t know if it is too late but here ya go…
All you have to do is upload the pdf to the file tab in textpattern and the use this code where ever you want the link <a href=“file path/file name.pdf”>Click here to view pdf</a>
For example: if you are using XAMPP then this is the code <a href=“http://localhost/site_name/files/example.pdf”>Text of the link</a>
It will open in a new tab when you click on the link. Hope this helps and I hope it made a lot more sense than the javascript stuff. Let me know!
Rose
Offline
Re: PDF to view in browser, not download
Hey, Phil! I wonder if you ever found an elegant solution to this. I’m pretty sure that Rose’s proposal would no longer work with recent versions of Textpattern because of the file access rules.
Offline
Re: PDF to view in browser, not download
Hi. I just let the browser’s prefs decide on PDF handling these days.
Offline
Re: PDF to view in browser, not download
philwareham wrote #290014:
Hi. I just let the browser’s prefs decide on PDF handling these days.
I’m not sure what you mean. Is there a way to configure Textpattern so that it doesn’t force PDF files to download?
Offline
Re: PDF to view in browser, not download
johnstephens wrote #295347:
Is there a way to configure Textpattern so that it doesn’t force PDF files to download?
Edit: Aow… sorry, for my answer below, the question is doesn’t force. So I’m missing the point.
The right one is Prevent downloading and is also worth a try.
I leave my previous answer here though:
Textpattern, probably not.
But I just find some .htaccess rules here, and especially Force downloading. Worth a try…
Last edited by CeBe (2015-10-09 19:11:15)
Offline
Re: PDF to view in browser, not download
Thank you @CeBe!
I added this to my 1) root .htaccess file, 2) .htaccess file in the files directory, and 3) both at the same time:
<FilesMatch "\.(pdf)$">
Header set Content-Type text/plain
</FilesMatch>
…And the PDF files still download with those rules.
I also swapped in these lines:
Header set Content-Type application/pdf
Header set Content-Disposition "inline"
Still the PDF files download instead of opening in my browser.
Since most sites open PDF files in the browser, unless the user brings up a contextual menu to “Save” the file when clicking a link, I thought it was something in Textpattern forcing them to download. Can any Textpattern developers confirm or refute this? Since these .htaccess hacks don’t seem to affect the behavior, is there any known way to do it?
Edit: Fix textile
Last edited by johnstephens (2015-12-22 20:54:51)
Offline
Re: PDF to view in browser, not download
I think it’s due to this line in publish.php which sets:
header('Content-Type: application/octet-stream');
for all file downloads passed through the file_download routine regardless of type…
TXP Builders – finely-crafted code, design and txp
Offline
Re: PDF to view in browser, not download
Thank you, @jakob! Is there any method to override this without editing publish.php, like a plugin or something?
I have barely enough wits to create the most basic plugin, but does output_file_download() have any kind of wiggle room if someone with PHP chops wanted to try?
Edit: I see that there the method includes callback_event('file_download'), and the Core callback list explicitly says it can intercept a file download. So I guess my question is, if such a plugin does not exist, is there dummy plugin code somewhere I can copy that shows how to use a callback from a plugin to do something like this?
Thanks again!
Last edited by johnstephens (2015-12-22 21:27:31)
Offline
Re: PDF to view in browser, not download
MisterX wrote #297698:
Have you installed flash player?
I test the site in one browser that includes Flash, but nothing on the site depends on Flash. I’m very cautious about Flash’s security vulnerabilities. Why?
Offline