In today’s data-driven world, Excel spreadsheets play a pivotal role in organizing and presenting data. They are a versatile tool for businesses and individuals alike, allowing for efficient data management and analysis. However, there are times when you need to share your Excel data in a more secure and universally accessible format, such as PDF. To automate Excel to PDF conversion, this article shows how to convert Excel files to PDF in PHP. Furthermore, advanced options to customize the Excel to PDF conversion are also discussed.
- PHP Excel to PDF Conversion API
- Convert Excel XLSX/XLS to PDF
- Advanced Options for Excel XLSX/XLS to PDF
PHP Library to Convert Excel XLS to PDF
In order to convert Excel XLSX or XLS files to PDF, we will use Aspose.Cells for PHP via Java. It is a feature-rich API that lets you implement spreadsheet automation features within your web applications. You can download the API’s package from here.
Usage
The following are the prerequisites that you need to fulfill in order to use Aspose.Cells for PHP via Java.
Once you have completed the prerequisites, follow the below steps to execute the example.php file for testing.
Place Java.inc file in the root folder of the API’s package that you have downloaded.
Run JavaBridge.jar using the below commands in the command prompt:
> cd aspose.cells
> %JAVA_HOME%\bin\java -Djava.ext.dirs=lib -jar JavaBridge.jar SERVLET_LOCAL:8080
- Run example.php from the API’s root folder using the below command:
> php example.php
Convert Excel XLS to PDF in PHP
The following are the steps to convert Excel XLSX or XLS to PDF.
- Create an object of the Workbook class and initialize it with Excel file’s path.
- Convert Excel to PDF using $workbook->save(“output.pdf”, SaveFormat::PDF) method.
The following code sample shows how to convert an Excel XLSX file to PDF in PHP.
Excel File
Converted PDF
PHP Excel to PDF - Advanced Options
Aspose.Cells also provides additional options to customize the Excel to PDF conversion. For example, you can set PDF compliance, compression, gridlines style, number of pages per sheet, etc.
The following are the steps to set additional options in Excel to PDF conversion.
- Create an object of the Workbook class and initialize it with Excel file’s path.
- Create an object of PdfSaveOptions class.
- Set desired options such as PDF compliance using PdfSaveOptions->setCompliance(PdfCompliance::PDF_A_1_B) method.
- Convert Excel to PDF using $workbook->save(“output.pdf”, PdfSaveOptions) method.
The following code sample shows how to customize Excel to PDF conversion using additional options.
Get Free Excel to PDF Converter API
To use the API without evaluation limitations, you can get a free temporary license.
Conclusion
With Aspose.Cells for PHP, converting Excel sheets to PDF is a breeze. Whether you need to distribute your data in a more accessible format or create a printable document, this library simplifies the process. Aspose.Cells provides numerous customization options, allowing you to control the appearance and layout of your PDF output. Following the provided guidelines, you can easily integrate Excel to PDF conversion in your PHP applications.
You can explore more about the library using the documentation. Furthermore, you can contact us via our forum in case of any query.