Home >Backend Development >PHP Tutorial >How Can I Convert Word and Excel Files to PDF Using PHP Without Zend Framework?

How Can I Convert Word and Excel Files to PDF Using PHP Without Zend Framework?

Linda Hamilton
Linda HamiltonOriginal
2024-11-18 07:13:02400browse

How Can I Convert Word and Excel Files to PDF Using PHP Without Zend Framework?

Convert Word, Excel Files to PDF with PHP: A Comprehensive Guide

Introduction

Converting documents from various formats to PDF is crucial for combining files into a single document. This article will guide you through the process of converting Word and Excel files to PDF using PHP, without relying on the Zend Framework.

OpenOffice.org and PyODConverter

  1. Install OpenOffice.org: This is required to enable your server to handle MS Office files. Contact your hosting provider for installation on your VPS.
  2. Download PyODConverter: Install this Python file (https://github.com/mirkonasato/pyodconverter) to facilitate the file conversion.

Conversion Script (adocpdf)

Create a plain text file named "adocpdf" containing the following script:

directory=
filename=
extension=
SERVICE='soffice'
if [ "`ps ax|grep -v grep|grep -c $SERVICE`" -lt 1 ]; then 
unset DISPLAY
/usr/bin/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard & 
sleep 5s
fi
python /home/website/python/DocumentConverter.py /home/website/$directory$filename$extension /home/website/$directory$filename.pdf

PHP Function

Call the conversion function from your PHP script when encountering a Word / Excel file:

$output = array();
$return_var = 0;
exec("/opt/adocpdf {$directory} {$filename} {$extension}", $output, $return_var);

Note:

  • The script will create a PDF in the same directory as the original file.
  • PHPExcel can be used to convert Excel files.
  • Alternatively, converting Word documents to images (jpg) may be a viable solution.

The above is the detailed content of How Can I Convert Word and Excel Files to PDF Using PHP Without Zend Framework?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn