Excel spreadsheets are widely used to store and analyze data in rows and columns. However, in each case, you need to convert Excel files to PDF before you can share them over the Internet. To automatically convert Excel to PDF, this article shows you how to programmatically convert Excel files to PDF using PHP. In addition, advanced options for customizing Excel to PDF conversions were discussed.
Let’s explore the following:
- Convert Excel XLSX/XLS to PDF
- Excel XLSX/XLS to PDF advanced options
To convert Excel XLSX or XLS files to PDF, we’ll use Aspose.Cells for PHP, a features-rich API that lets you automate spreadsheets in your Web applications.
>> You can download Asposite.Cells for the latest version of PHP test experience.
Convert Excel XLSX or XLS to PDF in PHP
Here are the steps to convert Excel XLSX or XLS to PDF.
- Create an object of the Workbook class and initialize it using the path to an Excel file.
- Convert Excel to PDF using the $workbook-> save (” output.pdf “, SaveFormat :: PDF) method.
The following code example shows how to convert Excel XLSX files to PDF in PHP.
require_once("Java.inc");
require_once("lib/aspose.cells.php");
use aspose\cells;
use aspose\cells\Workbook;
// load document to be converted
$wkbk = new Workbook("Book2.xlsx");
// save in PDF format
$wkbk->save("output.pdf", SaveFormat::PDF);
Copy the code
Excel file
Converted PDF
Advanced options for Excel to PDF conversion
Aspose.Cells also provides additional options to customize Excel to PDF conversion. For example, you can set PDF compliance, compression rate, gridline style, number of pages per page, etc. Here are the steps to set the other options to PDF conversion in Excel.
- Create an object of the Workbook class and initialize it using the path to an Excel file.
- Create an object of class PdfSaveOptions.
- Use the PdfSaveOptions-> setCompliance (PdfCompliance :: PDF_A_1_B) method to set the required options, such as PDF compliance.
- Convert Excel to PDF using the $workbook-> save (” output.pdf “, PdfSaveOptions) method.
The following code example demonstrates how to customize Excel for PDF conversion using other options.
require_once("Java.inc");
require_once("lib/aspose.cells.php");
use aspose\cells;
use aspose\cells\Workbook;
use aspose\cells\PdfSaveOptions;
use aspose\cells\PdfCompliance;
// load document to be converted
$wkbk = new Workbook("Book2.xlsx");
// create PDF options
pdfOptions = new PdfSaveOptions();
// set PDF compliance
pdfOptions->setCompliance(PdfCompliance::PDF_A_1_B);
// save in PDF format
$wkbk->save("output.pdf", pdfOptions);
Copy the code
If you have any questions or requirements, please feel free to join the Aspose Technology Exchange Group (761297826), we are happy to provide you with inquiries and consultation.