I had a problem with my current site as to how to display PDF files without depending on the PDF reader plugins installed on the browser. I've been googling around to find some ways and the only thing that I think is most acceptable is using flash plugin to embed it. But then as of now, flash plugin is not allowed to render PDF on SWF so the only remaining way is to covert the PDF files to SWF.




Application for Converting - PDF2SWF
In converting, I find pdf2swf application by swftools org that can be used to convert pdf files to swf using the command line. The good thing about this application is you can bundle it up and create an SWF file that includes reader like UI for your PDF.

Installation
For windows:

It's pretty straightforward and as easy as downloading the native installer for windows on their site and installing it.
After installing and download, add the installation path of the swftools to your environment variable PATH. To do it, you can just open:

My Computer > Properties > Advanced > Environment Variables

and look for PATH and add the following at the end ";C:\Program Files\SWFTools".

For Unix:

You can use
sudo apt-get pdf2swf

Though there are some distros that could not find the said package, so it may not work. What you can do is install it manually:

NOTE: Thanks for Mikhail for this nice tutorial. I just updated some packages though.

First let's download all the files that we will be needing:

wget http://swftools.org/swftools-0.9.1.tar.gz
tar -zvxf swftools-0.9.1.tar.gz
wget http://www.ijg.org/files/jpegsrc.v7.tar.gz
tar -zvxf jpegsrc.v7.tar.gz
wget http://download.savannah.gnu.org/releases-noredirect/freetype/freetype-2.3.12.tar.gz
tar -zvxf freetype-2.3.12.tar.gz

The other libraries are used by swftools especifically pdf2swf. That is why we are installing the other libraries too for dependency reason. Now we need to install it in the following order. Let's start with jpegsrc.
Note: Folder name may differ as to what I experience, so you may easily replace the folder name on part of the change directory (cd)

cd jpegsrc.v7
sudo ./configure
sudo make
sudo make install

Then the freetype:
cd freetype-2.3.12
rm -f config.cache
sudo ranlib /usr/local/lib/libjpeg.a
sudo ldconfig /usr/local/lib
sudo LDFLAGS="-L/usr/local/lib" CPPFLAGS="-I/usr/local/include" ./configure
sudo make
sudo make install

Well this is optional (since we are just using pdf2swf), but you may want to install it anyway. If you want to be able to convert pdf files into text you have to install xpdf.

sudo apt-get install xpdf-reader

Then now let's intstall swftools:

cd swftools-0.9.1
sudo LDFLAGS="-L/usr/local/lib" CPPFLAGS="-I/usr/local/include" ./configure
sudo make
sudo make install

Using the Application

Now to test it, you can simply use it in your command line (either by PHP or server side script/code) by typing

pdf2swf

To convert you can use this basic syntax:

pdf2swf [original_pdf_file] -o [new_swf_file]

To add the viewer you can choose from their predefined swf readers (For windows it is normally located at C:\Program Files\SWFTools\viewers) and combine it using this syntax:

swfcombine -o [new_swf_with_viewer] [viewer_file] viewport=[original_swf_file]

For other parameters on using it you can view the documentation.

Technically this is also useful for other purposes other than web developing so you may use it for other things. Thanks to swftools for providing such application.

No comments :

Post a Comment