简体   繁体   中英

Excel to PDF in PHP

I need to convert an Excel(.xls) spreadsheet to a PDF document with an image in PHP. If there is a library available please put the link.

Note - I have created excel(.xls) to PDF with "PHPExcel" library but my output is without image and border.

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. Ref

If PHPExcel can't do it, then you're stumped for a straight PHP solution, and might have to look at options like COM.

You don't mention what your problem is with the borders, and these have been a problem for some time in PHPExcel... the 1.7.6 version of PHPExcel resolved some of these issues, and there is a patch listed in the Issues section of the PHPExcel site that fixes some other problems with borders.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM