Google Answers Logo
View Question
 
Q: XLS to PDF Conversion using PHP/Ghostscript ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: XLS to PDF Conversion using PHP/Ghostscript
Category: Computers > Programming
Asked by: g8z-ga
List Price: $10.00
Posted: 17 Mar 2003 01:45 PST
Expires: 16 Apr 2003 02:45 PDT
Question ID: 177241
Hello,

I need to be able to convert an Excel .XLS file to PDF, but it must be
done via scripting (e.g. PHP, JSP, etc) on a remotely-hosted website.
For example, a user uploads a .XLS file to the server via a standard
HTML form, then when "Submit" is clicked, a PHP (or other language)
script is called, which converts the file to a PDF. The PDF will then
be saved to a database, but this I can handle - I just need to know
how to do the conversion.

Here's what is available to me: PHP, Perl, JSP, Python, Java Servlets,
Ghostscript version 6. The server is Linux, running Apache.

Constraints: I do not have root privileges on the server, so I cannot
install commercial software that I buy, and since it's running Linux I
would assume that MS Office is NOT installed on the server, although I
may be able to get some freeware like OpenOffice installed. If this
conversion is impossible without having Excel installed on the server,
I may be able to persuade my web host to install it.

I know that there are a lot of programs out there that allow this type
of conversion, but they're expensive, or they are things that must be
downloaded and installed, which I probably wouldn't be able to do
given my limited server permissions.

The ideal solution would involve only PHP or JSP commands +
Ghostscript or utilities built into Linux.

Thanks,
Darren
Answer  
Subject: Re: XLS to PDF Conversion using PHP/Ghostscript
Answered By: eiffel-ga on 17 Mar 2003 03:26 PST
Rated:5 out of 5 stars
 
Hi g8z,

You can convert XLS files to PDF on Linux by installing OpenOffice
with a PDF writer as the default printer driver.

Then, you can call OpenOffice (from PHP) using the "-p" command-line
parameter, which will cause it to load a designated file and print it.

For example, if your file was "accounts.xls" you would call the
following command:

   soffice -p accounts.xls

OpenOffice would load the "accounts.xls" file and "print" it to the
PDF writer, which would be configured to save the PDF document to the
desired filename.

GhostScript is a suitable PDF writer.

The OpenOffice setup guide describes how to install and configure
printer drivers using the "spadmin" utility, and discusses the use of
ghostscript as a PDF writer:
"Open Office Setup Guide - Appendix"
http://www.openoffice.org/docs/setup_guide/appendix.html

You can call OpenOffice from PHP by using the backtick execution
operator, or the "exec" function. You may also need to use PHP to move
and/or rename the resulting PDF files:

PHP: Program Execution Functions
http://www.php.net/manual/en/ref.exec.php

PHP: Filesystem: Rename
http://www.php.net/manual/en/function.rename.php

OpenOffice is pretty good at processing XLS files, but it may not
perfectly render every such file - so if you need the ultimate in
compatibility you will have to use Microsoft Excel on a Windows
Platform or emulator. "IT AsiaOne" looked at several alternatives to
Microsoft Office (including OpenOffice) and wrote that "while none of
the alternative suites promise ... full compatibility with Microsoft
Office-created documents, in general, they do a decent job of
translating Microsoft ".doc", ".ppt" and ".xls" file formats":

IT AsiaOne - Specials - Yours For The Picking
http://it.asia1.com.sg/specials/mmedia20020724_001.html


Additional links:

OpenOffice.org Home Page
http://www.openoffice.org/

Ghostscript Home Page
http://www.cs.wisc.edu/~ghost/

PHP Home Page
http://www.php.net/


Google search strategy:

openoffice scripting pdf linux
://www.google.com/search?q=openoffice%20scripting%20pdf%20linux

openoffice print "command line"
://www.google.com/search?q=openoffice%20scripting%20pdf%20linux

followed by a search for "command line parameters" from the
openoffice.org home page.


Regards,
eiffel-ga

Request for Answer Clarification by g8z-ga on 17 Mar 2003 13:42 PST
hi, thanks for the answer.

I'm contacted my hosting company, but it turns out they they are
unwilling to install OpenOffice on the server that my site is hosted
on. Is there any other solution to this problem other than the one who
described?

Clarification of Answer by eiffel-ga on 18 Mar 2003 04:34 PST
Hi g8z,

I have looked for alternative solutions to the problem of producing
PDF files from XLS files, but haven't come up with much. I presume, by
the way, that your current hosting company would be as reluctant to
install an alternative spreadsheet such as Gnumeric as it is to
install OpenOffice.

There are no standard Linux command-line utilities that process XLS
files. I ran the "apropos" command on a RedHat 8.0 installation with
arguments of "xls", "excel", "convert" and "pdf" and came up with
nothing useful.

According to the usual naming convention for command-line conversion
utilities, I would expect such a utility to be named "xls2pdf" or
"xlstopdf", so I performed a Google search on those terms:

xls2pdf OR xlstopdf
://www.google.com/search?q=xls2pdf+OR+xlstopdf

This search yielded two commercial utilities ($90 and $120) which run
under MS-Windows and require MS-Excel to be installed:

CZ-Xls2Pdf V1.0 Batch Converter
http://www.convertzone.com/xls2pdf/help.htm

ez-XLS2PDF PDF Converter
http://www.civiltools.com/apppages/products.asp?ID=19

Perhaps you could run those utilities when your XLS files are
generated, instead of producing the PDF version on the webserver?

Although Google is usually my search engine of choice, I occasionally
also use AltaVista because it accepts a wildcard as part of a search
term. I searched AltaVista for "xls2*" and "xlsto*"
http://www.altavista.com/web/results?q=xls2*
http://www.altavista.com/web/results?q=xlsto*
and found utilities to convert XLS files to other formats such as DXF,
TXT, LaTeX and HTML - but all of these run on MS-Windows and require
either MS-Excel or MS-Word to be installed.

I also searched SourceForge and FreshMeat for various combinations of
"xls", "excel", "linux", "reading" and "converting". This brought up a
few libraries and tools that could be useful if you want to program
your own utility.

A beta-version of a C-library to read XLS data is available:
"The Chicago Project"
http://chicago.sourceforge.net/

The PERL module ParseExcel can also read data from an XLS file:
http://www.cpan.org/authors/id/K/KW/KWITKNR/Spreadsheet-ParseExcel-0.25.readme

There is a utility "xls2csv" built around this PERL module, but much
of the following web page is in Cyrillic:
http://www.opennet.ru/prog/info/909.shtml

The ParseExcel module is being "actively ported" to PHP:
http://www.zoulcreations.com/projects/PHP_Excel/

Once you have read the XLS data you could write it to PDF using a PHP
library such as PDFlib:
http://www.pdflib.com/

Clearly, this "roll your own" approach will be much more work than
letting OpenOffice convert the file for you.

Could you consider using HTML as your target format instead of PDF? In
that case, you could use the xlhtml program under Linux:

Debian GNU/Linux - xlhtml
http://packages.debian.org/unstable/utils/xlhtml.html

There are other utilities and libraries listed at Planet PDF:

Planet PDF - Search Results - "converting xls"
http://www.planetpdf.com/mainpage%2Easp?webpageid=539&PP=1&q=converting+xls&IsPostback=true&submit2=Search&qphrasetype=free

Finally I searched for online services that could convert XLS to PDF.
The author of the doc2pdf utility (which runs on MS-Windows) offers an
evaluation service where you can email a small XLS file (as an
attachment) and it will be emailed back as PDF. You could contact the
author to see if he would be willing to offer this service for higher
volumes and/or larger files on a paid basis:

Doc2pdf Home Page
http://doc2pdf.sourceforge.net/

It seems to me that OpenOffice (in conjunction with ghostscript as a
PDF generator) is by far the most straightforward way to obtain PDF
files from XLS files. Perhaps you are able to consider switching to a
hosting company that is OpenOffice-friendly? Failing that, could you
perform your file conversion on a server that you control instead of
one at a hosting company?


Additional links:

FreshMeat Home Page
http://freshmeat.net/

SourceForge Home Page
http://sourceforge.net/

Planet PDF Home Page
http://www.planetpdf.com/


Google search strategy:

"converting xls OR excel" pdf linux
://www.google.com/search?q=%22converting+xls+OR+excel%22+pdf+linux


Regards,
eiffel-ga
g8z-ga rated this answer:5 out of 5 stars

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy