Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2012-02-29 23:58:10

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 590
Website

Open PDFs in new window while using TXP

I have a TXP site (Site A) with a custom field called “File ID on Site B” that lets me reference a file in another Textpattern database by typing in a file ID. Then a link like this is created:

http://www.example.com/site_b/file_download/13 (or whatever the file ID is)

This way there is no need to manage files on both sites; files can be maintained on one site and linked easily on the other.

However, I am typically linking to a PDF file. And I’m finding that the link format above (without the filename at the end) does not give the ability to open the PDF file within the browser; the “Save File” dialog opens every time. Unfortunately the desired behavior for this site is to have PDF files open in-browser.

On the old non-TXP version of this site, PDF files were referenced directly by file name, and they open in-browser just fine.

Is there a way to manage file downloads in this way and somehow reference them directly in a link, so that they open properly in-browser?

Thanks for any help!

Offline

#2 2012-03-01 00:06:24

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

Re: Open PDFs in new window while using TXP

If you don’t use the file_download tag that routes it through txp’s /file_download/ mechanism but instead build your links to the filename itself in the /files/ directory (and ensure your htaccess doesn’t prevent that), then the pdf will be handled by the browser according to the browser’s normal PDF settings.

Note: that you lose txp’s file download count facility, although you can achieve that with google analytics instead. See here.


TXP Builders – finely-crafted code, design and txp

Offline

#3 2012-03-01 01:01:36

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 590
Website

Re: Open PDFs in new window while using TXP

Thanks, Jakob. I’m wondering how to get the filename from the external website using only the ID. Possible? I suppose I could try a PHP script if needed.

Offline

#4 2012-03-01 07:50:22

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

Re: Open PDFs in new window while using TXP

I’ve never tried it but there was this alternative approach suggested. If I’ve understood that correctly, that would need to go in the site that is hosting the pdfs. The tag usage shown in that thread is also not the same as the request via URL use case you have but the principle of using txp:file_download and modifying its mime-type may be a springboard for making the same approach respond to another URL that only contains the ID and updates the download count, e.g. www.domain.com/pdf_download/13

Other very loose off the top of my head ideas:

  • maybe there’s a way of using rah_external_output on the site hosting the PDFs that will take input from an url containing the file ID and show you the file.
  • if the server’s are on the same host, Jukka has been quietly working on an as yet unreleased tag called rah_swap that swaps database connections on the fly so that you could access the ‘remote’ db from your other txp site.

TXP Builders – finely-crafted code, design and txp

Offline

#5 2012-03-01 11:08:45

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

Re: Open PDFs in new window while using TXP

jakob wrote:

if the server’s are on the same host, Jukka has been quietly working on an as yet unreleased tag called rah_swap that swaps database connections on the fly so that you could access the ‘remote’ db from your other txp site.

Technically can be on different hosts too. Not limited to local socket. True, rah_swap can ‘technically’ be used to return file downloads from a second Textpattern site. I.e.

<txp:rah_swap db="2ndSiteWithDLD">
	<txp:file_download id='<txp:custom_field name="file_id" />'>
		<txp:file_download_link>
			<txp:file_download_name />
		</txp:file_download_link>
	</txp:file_download>
</txp:rah_swap>

But you may run to some obstacles along the way. Textpattern’s design model is rather old when it comes to caching and handling data. Which is not necessarily a bad thing, but affects multi-database situations. Textpattern uses caching that is done on the application level and isn’t tied in to the storage driver or connected database. In other words, if you fetch a form named mytinyform from database A, that form will be cached and used even when accessing mytinyform from database B. The forms share same name, but are not same item, but in the eyes of Textpattern, they are (as of the name). Same type of internal application level caching model is applied to articles, users and so on which can lead to unavoidable conflicts.

Last edited by Gocom (2012-03-01 11:13:05)

Offline

#6 2012-03-01 19:15:59

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 590
Website

Re: Open PDFs in new window while using TXP

I’m thinking rah_external_output creating XML with full file URL on Site B, and smd_xml parsing / displaying it on Site A.

Note to self: Make sure .htaccess doesn’t interfere…

A lot of trouble just to get PDFs to open in-browser. This is cracking me up. :-) Thanks for the help! Very interesting about rah_swap.

Last edited by maruchan (2012-03-01 19:33:24)

Offline

#7 2012-03-01 19:54:05

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

Re: Open PDFs in new window while using TXP

let us know how you get on :-)


TXP Builders – finely-crafted code, design and txp

Offline

#8 2012-03-01 23:38:56

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 590
Website

Re: Open PDFs in new window while using TXP

OK, thanks to some help from Stef (I had no idea that txp:variable could work as a container), here’s a working rah_external_output to smd_xml system for grabbing a direct URL to a file from another TXP site with only the file ID:

Install rah_external_output and adi_gps on “mothership” site with all the files in it. Create new snippet: “fileurl”. Set Content-Type to “text/xml”. Snippet has this inside (update: thanks to Jakob, new code below that does not use file_download, thus avoiding the PDF-always-opens-save-dialog problem):

<txp:adi_gps />
<file>
<fileurl><txp:site_url />files/<txp:article_custom limit="1"><txp:file_download_list id='<txp:variable name="fid" />'><txp:file_download_name /></txp:file_download_list></txp:article_custom></fileurl>
</file>

So, if you go to http://www.example.com/?fid=3&rah_external_output=fileurl you will get this XML, served as text/xml:

<file>
<fileurl>http://www.example.com/file_download/3/filename_here.pdf</fileurl>
</file>

Great, so now our XML is all set up. Now we go to the other website and make sure we have a custom field called “external_file_id” representing the file ID number on the mothership, and we’re ready to parse.

<txp:if_custom_field name="external_file_id">
  <p>

    <txp:variable name="externalurl">http://www.example.com/?fid=<txp:custom_field name='external_file_id' />&rah_external_output=fileurl</txp:variable>

    <txp:smd_xml data='<txp:variable name="externalurl" />' record="file" fields="fileurl">
      <a href="{fileurl}" title="Download file">File Link</a> (PDF download)
    </txp:smd_xml>

  </p>
</txp:if_custom_field>

And that’s it. But the funny part is that I’m back to the original problem: PDFs still don’t open in the browser. On the previous version of the site, in a different CMS, on the same server, they do open in the browser. :-/ Frustrating. Ideas appreciated while I run around and test things!

Last edited by maruchan (2012-03-02 00:10:22)

Offline

#9 2012-03-01 23:49:56

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 590
Website

Re: Open PDFs in new window while using TXP

Hmm, one is a relative link to the PDF file; the other is an absolute link to the PDF on a different server. I wonder if that makes a difference in whether PDFs are opened in-browser or not.

Offline

#10 2012-03-01 23:52:07

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

Re: Open PDFs in new window while using TXP

How about making the first snippet construct a direct link to the file like this?

<txp:adi_gps />
<file>
<fileurl><txp:site_url />files/<txp:article_custom limit="1"><txp:file_download_list id='<txp:variable name="fid" />'><txp:file_download_name /></txp:file_download_list></txp:article_custom></fileurl>
</file>

TXP Builders – finely-crafted code, design and txp

Offline

#11 2012-03-02 00:06:05

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 590
Website

Re: Open PDFs in new window while using TXP

Thanks, Jakob. I completely missed that it was still using file_download! I think that might work, but for the moment I think smd_xml is caching the old feed…need to reset it…

Offline

#12 2012-03-02 00:08:59

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 590
Website

Re: Open PDFs in new window while using TXP

That was it! Thanks for the help; problem solved. :-) I’ll update the example above with your code, Jakob.

Last edited by maruchan (2012-03-02 00:09:17)

Offline

Board footer

Powered by FluxBB