Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2010-02-04 13:32:02

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

PDF to view in browser, not download

Hi,

Quick question (hopefully): Is there a way to get a PDF file, loaded into the CMS files section, to default to displaying in the browser instead of downloading to your desktop like other filetypes? I could simply put the URLs of the files in manually to my pages, but would ideally like to track the downloads (views) of these files within textpattern.

Cheers,
Phil

Offline

#2 2010-02-04 13:56:31

trenc
Plugin Author
From: ⛵️, currently Göteborg, SE
Registered: 2008-02-27
Posts: 573
Website GitHub

Re: PDF to view in browser, not download

This does only work with a flash pdf viewer as you can see on scribd.com f.i.

Offline

#3 2010-02-04 17:01:18

ax
Plugin Author
From: Germany
Registered: 2009-08-19
Posts: 165

Re: PDF to view in browser, not download

Personally, I like issuu.com better than scribd.com for flash preview of PDF files.

Offline

#4 2010-02-04 17:15:05

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: PDF to view in browser, not download

Sorry, maybe I did not make it clear in the OP – I’m quite happy for the user to have the acrobat plugin installed as a requirement – I just want the default behaviour of file URLs to change from downloading the file to viewing in-browser for any PDF file types.

Anyway, not a big deal, I just though it would be nice to track PDF views from within textpattern – I can always track them through Google Analytics instead by using hard coded URLs.

Offline

#5 2010-02-04 18:39:34

PascalL
Member
From: Switzerland
Registered: 2009-03-09
Posts: 132
Website

Re: PDF to view in browser, not download

I just want the default behaviour of file URLs to change from downloading the file to viewing in-browser for any PDF file types.

This is usually a preference set by the user in his browser of choice, and it’s a good thing we, webdevelopers, can’t change user’s preferences :D

Offline

#6 2010-02-04 18:43:00

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: PDF to view in browser, not download

Yep, but in this case the textpattern file download overrides user’s preference anyway and downloads the PDF to their desktop regardless of whether the user wants to view it within the browser or not, so your argument is not really valid here.

Offline

#7 2010-02-04 19:08:52

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: PDF to view in browser, not download

PascalL wrote:

This is usually a preference set by the user in his browser of choice, and it’s a good thing we, webdevelopers, can’t change user’s preferences :D

Yes. If we did I would murder the planet for making me use Adobe’s reader and nasty plugin :D My deadly stare is so deadly. Only rock and plastic bags survive.

philwareham wrote:

Anyway, not a big deal, I just though it would be nice to track PDF views from within textpattern – I can always track them through Google Analytics instead by using hard coded URLs.

You could bundle the link with some javascript and sql, same way that Google tracking code does. The counting sql is just line of code. After that you just require a javascript that requests a page where that code is located:

<txp:php>
	safe_update(
		'txp_file',
		'downloads=downloads+1',
		"id='".doSlash(ps('file'))."'"
	);
</txp:php>

The javascript could possibly be done with jQuery’s Ajax API:

$("a.download_link").click(
	function() {
		var id = $(this).attr('title');
		$.post("MyPageWithSQL", { file: id } );
	}
);

Link example:

<a class="download_link" href="file.pdf" title="PlaceFileIDhere">
	Read my awesome pdf to get some kex.
</a>

Totally untested example. Prolly doesn’t work (atleast not the javascript) :D.

Last edited by Gocom (2010-02-04 19:27:12)

Offline

#8 2010-02-04 19:15:28

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: PDF to view in browser, not download

Lol. I think I’ll leave things as they are with hard coded links for PDFs, that way if the user selects to view via plugin they can, if they don’t want to it simply downloads. That way everyone’s happy and Jukka does not end up murdering the planet. Win win situation.

Last edited by philwareham (2010-02-04 19:15:51)

Offline

#9 2010-02-04 19:24:35

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: PDF to view in browser, not download

philwareham wrote:

Lol. I think I’ll leave things as they are with hard coded links for PDFs, that way if the user selects to view via plugin they can, if they don’t want to it simply downloads. That way everyone’s happy and Jukka does not end up murdering the planet. Win win situation.

Heh. Updated my above post with an alternate example(ish).

Offline

#10 2010-02-04 20:33:01

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: PDF to view in browser, not download

Thanks Jukka, that is exactly what I’m after (reason being that the client is going to want to see how many user views his PDFs generate), though I can’t get the code above to work – would the php have to be within a textpattern page? I’ve got it just as a separate php file on the server labelled ‘pdf-viewcounter.php’ at the moment as follows…

<?php
safe_update(
'txp_file',
'downloads=downloads+1',
"id='".doSlash(ps('file'))."'"
);
?>

And the javascript in the textpattern page (which uses jquery) as…

$("a.pdf-viewcounter").click(
	function() {
		var id = $(this).attr('title');
		$.post("http://www.mysite.com/pdf-viewcounter.php", { file: id } );
	}
);

Cheers,
Phil

Last edited by philwareham (2010-02-04 20:34:05)

Offline

#11 2010-02-04 21:11:22

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: PDF to view in browser, not download

philwareham wrote:

would the php have to be within a textpattern page?

Yes, Textpattern’s library has to be loaded, if it is used. And that code uses the lib.

If you don’t want to use a page template and waste a section, you can place the script into rah_external_output.

Offline

#12 2010-02-05 10:19:32

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: PDF to view in browser, not download

Hi again Jukka,
Sorry to be dim, but I’ve installed rah_external_output and pasted the php code there with a name of ‘pdf-viewcounter’, but how to I call it from the existing line…

$.post("MyPageWithSQL", { file: id } );

Do I replace MyPageWithSQL with <txp:rah_external_output name=‘pdf-viewcounter’ /> or use a URL (if so how to I find the URL)?

Offline

Board footer

Powered by FluxBB