Convert Excel to PDF using PHP

Convert Excel to PDF using PHP

We widely use Excel spreadsheets to maintain invoices, ledgers, inventory, accounts, computational data, and other reports. Whereas, PDF is the most popular format for sharing and printing documents without losing the formatting. In certain cases, we may need to convert Excel to PDF to share Excel data in a portable form. We can easily convert Excel Spreadsheets to PDF documents programmatically on the cloud. In this article, we will learn how to convert Excel to PDF using PHP.

The following topics shall be covered in this article:

Excel to PDF Conversion REST API and PHP SDK

For converting XLSX to PDF, we will be using the PHP SDK of GroupDocs.Conversion Cloud API. It enables us to seamlessly convert documents and images of any supported file format to any format we require. Please install it using the following command in the console:

composer require groupdocscloud/groupdocs-conversion-cloud

After installation, please use the Composers’ autoload to use the SDK as shown below:

Please get your Client ID and Secret from the dashboard before following the mentioned steps. Once you have your ID and secret, add in the code as shown below:

Convert Excel to PDF using a REST API in PHP

We can easily convert Excel files to PDF documents programmatically on the cloud by following the steps given below:

  1. Upload the XLSX file to the cloud
  2. Convert Excel to PDF
  3. Download the converted PDF file

Upload the Excel File

Firstly, we will upload the XLSX file to the cloud using the following code sample:

As a result, the uploaded file will be available in the files section of the dashboard on the cloud.

Convert Excel to PDF in PHP

Now, we will convert the uploaded XLSX file to the PDF document by following the steps given below:

  1. Firstly, create an instance of the ConvertApi.
  2. Next, Initialize the ConvertSettings instance and set the uploaded XLSX file path.
  3. Then, assign “pdf” to the format and provide the output file path.
  4. Next, set various SpreadsheetLoadOptions such as setOnePagePerSheet, etc.
  5. Optionally, set various PdfConvertOptions such as setCenterWindowsetMarginTopsetMarginLeft, etc.
  6. After that, create ConvertDocumentRequest with ConvertSettings as an argument.
  7. Finally, convert by calling the ConvertApi.convertDocument() method.

The following code example shows how to convert Excel Spreadsheet to a PDF document using a REST API in PHP.

Convert Excel to PDF using a REST API in PHP
Convert Excel to PDF using a REST API in PHP.

Download the Converted File

The above code sample will save the converted PDF file on the cloud. It can be downloaded using the following code sample:

Convert Specific Excel Spreadsheets to PDF in PHP

We can also convert specific Excel Spreadsheets to a PDF document by following the steps given below:

  1. Firstly, create an instance of the ConvertApi.
  2. Next, Initialize the ConvertSettings instance.
  3. Then, set the uploaded XLSX file path.
  4. Also assign “pdf” to the format and provide the output file path.
  5. Next, initialize the PdfConvertOptions object
  6. Then, provide specific sheet indexes in a comma-separated array to convert.
  7. After that, create ConvertDocumentRequest with ConvertSettings as an argument.
  8. Finally, convert by calling the ConvertApi.convertDocument() method.

The following code example shows how to convert a specific Excel Spreadsheets to a PDF document using a REST API in PHP. Please follow the steps mentioned earlier to upload and download the files.

Convert Specific Excel Spreadsheets to PDF in PHP
Convert Specific Excel Spreadsheets to PDF in PHP.

We can also convert a range of spreadsheets from Excel to a PDF file by following the steps mentioned earlier. However, we just need to mention the range of sheets as shown follows:

$convertOptions = new GroupDocs\Conversion\Model\PdfConvertOptions();
$convertOptions->setFromPage(2);
$convertOptions->setPagesCount(4);

Excel to PDF Conversion with Watermark

We can convert Excel Spreadsheets to a PDF document and add a watermark to the converted document by following the steps given below:

  1. Firstly, create an instance of the ConvertApi.
  2. Next, Initialize the ConvertSettings instance.
  3. Then, set the uploaded XLSX file path.
  4. Also assign “pdf” to the format and provide the output file path.
  5. Next, initialize the WatermarkOptions object and set Watermark Text, Color, Width, Height, etc.
  6. Then, define the PdfConvertOptions and assign WatermarkOptions.
  7. After that, create ConvertDocumentRequest with ConvertSettings as an argument.
  8. Finally, convert by calling the ConvertApi.convertDocument() method.

The following code sample shows how to convert an Excel Spreadsheet to a PDF document and add a watermark to the converted PDF document using a REST API in PHP. Please follow the steps mentioned earlier to upload and download the files.

Excel to PDF Conversion with Watermark
Excel to PDF Conversion with Watermark.

Convert Excel to PDF without using Cloud Storage

We can convert Excel to PDF without using cloud storage by following the steps given below:

  • Firstly, create an instance of the ConvertApi.
  • Next, create ConvertDocumentDirectRequest with target format and the input file path as arguments.
  • Finally, convert by calling the convertDocumentDirect() method.

The following code sample shows how to convert an Excel Spreadsheet to a PDF document without using cloud storage. We pass the input file in the request body and receive the output file in the API response.

Try Online

Please try the following free online XLSX to PDF conversion tool, which is developed using the above API. https://products.groupdocs.app/conversion/xlsx-to-pdf

Conclusion

In this article, we have learned how to:

  • convert Excel to PDF using PHP;
  • convert a specific Excel spreadsheets to PDF in PHP;
  • add watermark to the converted PDF document;
  • convert without using cloud storage;
  • upload XLSX file to the cloud;
  • download PDF file from the cloud.

Besides, you can learn more about GroupDocs.Conversion Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. In case of any ambiguity, please feel free to contact us on the forum.

See Also