Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#13 2010-02-05 14:07:05

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

Re: PDF to view in browser, not download

philwareham wrote:

(if so how to I find the URL)?

The URI can be found from the “View”-link (rightmost column in the list view).

Because the URI will contain GET/POST attribute, you might need to add it as POST parameter (and use slash or domain as “MyPageWithSQL”).

Offline

#14 2010-02-05 15:12:28

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

Re: PDF to view in browser, not download

Because the URI will contain GET/POST attribute, you might need to add it as POST parameter (and use slash or domain as “MyPageWithSQL”).

Erm sorry, how is that done exactly? I have little javascript knowledge but don’t want to give up this close to getting this working. The URI is as follows…

http://www.mysite.com/?rah_external_output=pdf-viewcounter

Offline

#15 2010-02-22 17:52:31

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

Re: PDF to view in browser, not download

Hmmm, I’m revisiting this as I’d really like to get it working. To summarise so far, the problem is that PDFs uploaded to the CMS file area are automatically set to download to a users’ desktop when linked through the system – perfectly understandable for other files but in the case of PDFs the user might simply want to view them in-browser instead if they have the required plugin.

I could simply hard code the URL links to these PDFs into pages which solves that problem but then the number of times it is downloaded (or viewed in this case) is not tracked in textpattern. I know my client is going to want to see the number of times PDFs are viewed so it would be nice and tidy to track it in the system.

Jukka suggested the following code which I have loaded into a rah_external_output snippet called ‘pdf-viewcounter’ as follows…

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

The hard coded URL links to PDFs then have a class and id number of the file added (in this example it is file #303 in the system)…

<a href="/link-to-pdf-file.pdf" class="pdf-viewcounter" id="303" ...

And the following (currently unworking) bit of jquery code is at the foot of the page…

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

Anyone got an idea on this?

Last edited by philwareham (2010-02-22 17:53:27)

Offline

#16 2010-03-07 18:42:29

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

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

#17 2011-10-17 12:48:00

etc
Developer
Registered: 2010-11-11
Posts: 4,529
Website GitHub

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

#19 2015-04-15 19:52:49

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 997
Website

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

#20 2015-04-15 22:08:22

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

Re: PDF to view in browser, not download

Hi. I just let the browser’s prefs decide on PDF handling these days.

Offline

#21 2015-10-02 20:20:35

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 997
Website

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

#22 2015-10-09 19:03:06

CeBe
Plugin Author
From: Caen - Fr
Registered: 2010-06-25
Posts: 345
Website

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

#23 2015-12-22 20:53:39

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 997
Website

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

#24 2015-12-22 21:13:55

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,178
Website

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

Board footer

Powered by FluxBB