Download those PDFs!

Wow, I really like these 512x512 icons in Snow Leopard...This post is strictly for web developers/server administrators. The rest of you can resume your daily activities, confident that nothing that was even remotely relevant to you transpired here.

PDFs. Web browsers. Both are a daily, or at least frequent, part of the lives of most computer users. But not all web browsers are created equal when it comes to the matter of handling PDFs. Some browsers (say, the ones developed by commercial OS makers) take the approach of trying to manage everything for the user. They include PDF readers that are embedded right into the browser, and PDFs load almost like another web page. Other browsers (most notably Firefox) treat PDFs as downloadable files, and when the user clicks a link to one, the file gets downloaded to their hard drive to be opened in a “helper app” — usually Adobe Reader.

Most website owners prefer the latter approach, and I suspect most users do as well. PDFs in general are not much like web pages, and it does not seem logical that they should be viewed within a web browser. Generally when people are accessing a PDF, it’s because they want to download the document to their computer to be used offline or to print. It is illogical for these actions to take place in a web browser. Sure, savvy users can right-click (or on Mac, Ctrl-click) and select “Save linked file as…” or some such nonsense from the contextual menu. But a lot of Windows users don’t even know their mouse has a right button, a lot of Mac users have no idea that you can press keys and click the mouse button simultaneously to perform special actions, and a lot of both would be confused by this entire process.

So we come to the matter of web developers (such as myself) trying to find ways to force the web browser to download the file instead of loading it directly. A trick I have used often is to link not to the file directly, but to a special PHP script that reads the file into the server’s memory, changes various aspects of the response (such as the MIME type), and then streams the content out to the browser. This is especially useful when you want to restrict access to files, say only to logged-in users, or only to users who have entered a special passcode. The PHP script is perfect for that, because it allows you to execute some code before sending the file to the browser. It even lets you store your files in a directory on the server that web browsers cannot access directly, ensuring (more or less… this article isn’t about hacking) the security of your files.

But what if your files aren’t in a protected area? What if you don’t want to bother with the mucky-muck of the PHP wrapper — you just want to link directly to the (browser-accessible) file, but you still want to force the download? Well, if you’re using Apache, you’re in luck. I found this great explanation of a small block of code you need to add to your httpd.conf file to achieve the same effect.

Ultimately, what you want to do is change the MIME type of the response. Browsers that are inclined to load PDFs internally perform this magic by seeking out files that are sent with the application/pdf MIME type. But there’s a very handy, “generic” MIME type for binary files, which all browsers treat as files to be downloaded rather than displayed directly: application/octet-stream. It may sound like a group of 8 singers standing in a small river, but it really just means… a generic binary file.

Here’s the complete block of code to put into your httpd.conf file, or into the appropriate virtual host configuration, however it’s stored on your particular server. I placed the code just within the virtual host configuration for the client in question, so the change applies only to their site, and not to any others that are also running on the server:

<FilesMatch "\.(?i:pdf)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>

If you’re not the server admin, you should also be able to put this in a .htaccess file in your site’s root directory, but I haven’t actually tested that to see if it works.

Pocket Sysadmin

I’m leaving on a jet plane, don’t know when I’ll be back again. External link

Actually, that’s not true: I will be back on Sunday. But the point is, I’m going on a trip… and more importantly, I’m not taking my laptop. Gasp! Can it be true? I’ll answer that for you: yes, it’s true.

OK, enough with this absurdly bad writing. On to the matter at hand: traveling sans laptop. Since I began freelancing in mid-2008, it’s been a given that I would always have my laptop with me. Not because I am a workaholic (not true) or because I’m an Internet junkie (true), but primarily because I needed to have a way to monitor and troubleshoot web server performance in case any of my clients had technical problems while I was gone.

But for this trip, I’ve decided I want to make it a real vacation. I want to remove the temptation to work when I really shouldn’t. I need a break. But I still need to be accessible if an emergency arises, and I need to be able to do something about it. Accessibility has been a non-issue ever since I got a cell phone: just call me. Or, since I got an iPhone: email, text, or call me (preferably in that order). But heretofore, the best I could do with a phone/iPhone was become aware of a problem. I still needed a full-fledged computer to actually do anything productive.

Over the past couple of weeks I’ve been preparing my iPhone to become an all-in-one tool for managing my business on the road. That meant setting up all of the diagnostic and troubleshooting tools I could, to ensure that I can adequately monitor the performance of the web servers I’m responsible for, and fix any problems that come up. Here are the tools I’m using to make that possible:

iStat

iStat

iStat is a very polished little utility for monitoring system performance. Its main feature is that it provides detailed stats on your iPhone itself: battery capacity, memory usage, storage available, IP address, uptime, running processes, and MAC address. But what’s really cool about it is that there are remote monitoring tools that allow you to monitor your Mac from your iPhone, or, more importantly for me, you can monitor a Linux, BSD or Solaris server. It requires a fair bit of command-line mucky-muck to set up (including compiling from source), but in a matter of minutes I had multiple Linux servers set up, sending their performance data to my iPhone wherever I go. With iStat, I can be proactive in monitoring server performance.

TouchTerm SSH

TouchTerm SSH

So, great. iStat lets me see how my servers are doing. But what if there’s a problem? That’s where TouchTerm SSH comes in. When I work with servers from my computer, the main application I use is Terminal. The SSH protocol allows me to connect securely to my servers with a command-line interface, where I can do anything I need to do: check running processes, modify configurations, restart services, etc. TouchTerm SSH is a fully-functional SSH terminal on the iPhone. With it, anything that I can do via SSH from my computer, I can now do with the iPhone. I just installed it today, so I haven’t completely put it through its paces, but I am sure that before long this will be one of the most indispensable apps I have installed on my iPhone. Even more than Ramp Champ.

Slicehost

Slicehost Pro

This one’s a bit more specialized, obviously, but since Slicehost’s iPhone app was one of the main selling points for me to go with them in the first place, it’s worth acknowledging.

Slicehost is a VPS hosting company based in St. Louis. They offer great service at unbeatable prices. Running a VPS is not for the feint of heart, but if you’re not afraid of taking full responsibility for your own server, Slicehost is the way to go. Their simple web-based admin interface makes it a snap to set up your own server with one of any number of Linux distros (Ubuntu, Debian, Gentoo, CentOS, Fedora, Arch or Red Hat), and once it’s running, to monitor its performance and reboot if necessary.

The iPhone app’s functionality is pretty limited, but it has the one critical function I need: if the slice becomes unresponsive, you can reboot it. Sure, you can do that from their mobile website too, but it’s always fun to have another app on your home screen.