Just another WordPress.com site

Archive for the ‘blog’ Category

Force a PDF to download

I recently needed to force a PDF to download using Apache. The default behaviour for most browsers is to try to open the PDF inside the browser itself. This is fine for a small PDF or for powerful machines – but a large PDF on even a modest machine can often lock the browser up. This needed fixing!

Little R&D of the Apache documents, you can get FilesMatch option which takes Regular Expressions. Initially I used something like this…

<files *.pdf=””></files><br />
<files *.pdf=””> ForceType application/pdf</files><br />
<files *.pdf=””> Header set Content-Disposition attachment</files><br />

This worked PERFECTLY – except some files had upper-case extensions and some had lower and I could see situations in the future where combinations of upper and lower case would be used too – just to piss me off! Because of this, not even this would work…

<filesmatch \.(pdf|pdf)=””></filesmatch>
<filesmatch \.(pdf|pdf)=””> ForceType application/pdf</filesmatch><filesmatch \.(pdf|pdf)=””> Header set Content-Disposition attachment</filesmatch>

That would match perfectly – as long as it was an EXACT match on upper OR lower case.
I was reaching the end of my patience – that is until I read the Using Character Classes on PerlDoc.
This showed me that I could force the RegEx (short for Regular Expressions) to match in a case-insensitive manner. This lead me to the following…

<filesmatch \.(?i:pdf)$=””></filesmatch>
<filesmatch \.(?i:pdf)$=””> ForceType application/pdf</filesmatch><filesmatch \.(?i:pdf)$=””> Header set Content-Disposition attachment</filesmatch>

However this only worked in proper browsers – and the bulk of the world are sadistic enough  to use Internet Explorer based ones. For some reason, if Internet Explorer see’s the content type “Application/PDF” it will simply open it up in the reader. The solution? Why not pretend its a bog standard Octet Stream, just like a Zip file? After all, that’s basically all it is; a binary file… A steam of bytes.

<FilesMatch “\.(?i:pdf)$”>
ForceType application/octetstream
Header set ContentDisposition attachment
FilesMatch>

And there you have it… A perfectly working modification to force all PDF files to download – this will work for any file extensions you chose to put into the FilesMatch argument!

Impotant Note :

You can put this code in either the htaccess or the vhost configuration for your server.
You can read more about FilesMatch at the Apache Document page.
Advertisements

some of the basic linux command

There are many common Linux commands that will be helpful to you, if you ever even use the command line interface in Linux. Most average users just use the graphical user interface instead which usually has many tools and front-ends to Linux common commands. This Linux tutorial on command commands will help even the average user in case X server crashes, fails, is not properly configured, etc. So continue reading for some of the more common Linux bash commands.

Some of the more common Linux shell commands are listed below for more information on each command you can always run man [command] and this will bring up the manpage for that command, you can also click on the commands listed for some common examples and syntax.
First before I list them any syntax in [] will need some kind of input from you normally, for example:
man [command] you will want to actually replace [command] with the shell command you want to read the man page for: man ls will give you the man page for the Linux shell command ls.

  • linux ls command – is used to list files on the filesystem.
  • file – command that will check the filetype, this will output to you what the file type is no matter what the extension is.
  • mkdir command – used to make directories on the filesystem.
  • cd – is used for changing into a different directory in the Linux shell
  • cp – is the Linux copy command, this shell command is used to copy files|directories from one location on the filesystem to another.
  • mv – the Linux terminal command to move files|directories. Like the cp command, but deletes the original source.
  • rm – shell command in Linux to remove files|directories.
  • Linux cat command- this command is used to print|view the contents of a file to the screen|terminal.
  • grep – command used to search|find contents of a file and print|view on your terminal|screen.
  • Linux more and less – commands that will allow you to read output of files, unlike cat that will output the entire file at once, even if it is too large for your terminal more and less will output only as many lines as the shell you are in can output, and allow you to scroll through the file contents.
  • chown – Linux command to change ownership of a file|directory.
  • Linux chmod – command that allows you to change mode of user access|permissions, basically set read, write, and execute permissions.
  • Linux ps – lists the current running processes on your Linux system
  • Linux kill and killall commands – used to kill|terminate running processes

Unpacking or uncompressing gz files under Linux and UNIX systems

Using gunzip command:

$ gunzip file.gz
$ ls file

Using gzip -d command:

$ gzip -d file.gz
$ ls file

If file extension is tar.gz, type the command:

$ tar -zxvf file.tar.gz

Please note that gunzip can currently decompress files created by gzip, zip, compress, compress -H or pack programs.

%d bloggers like this: