Excel spreadsheets are widely used to store and analyze data in the form of rows and columns. However, in various cases, you need to convert the Excel files to PDF before sharing them over the internet. In order to automate Excel to PDF conversion, this article shows how to convert Excel files to PDF programmatically using 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 Excel to PDF Conversion API
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.
1. Place Java.inc file in the root folder of the API’s package that you have downloaded.
2. 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
3. Run example.php from the API’s root folder using the below command:
> php example.php
Convert Excel XLSX or 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
Advanced Options for Excel to PDF Conversion
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 a Free API License
In order to use the API without evaluation limitations, you can request a free temporary license.
Conclusion
In this article, you have learned how to convert Excel files to PDF using PHP. Furthermore, you have seen how to customize Excel to PDF conversion using advanced options. You can explore more about the API using the documentation. Furthermore, you can contact us via our forum in case of any query.