How to Convert Excel Spreadsheets to PDF using Ruby

How to Convert Excel Spreadsheets to PDF using REST API in Ruby?

Excel spreadsheets are widely used for receipts, invoices, ledgers, inventory, accounts, and other reports. The XLS/XLSX to PDF conversion API lets you share Excel data in a portable PDF format. As a Ruby developer, you can easily convert Excel spreadsheets to PDF documents programmatically on the cloud. In this article, we will learn how to convert Excel spreadsheets to PDF using Ruby.

The following topics are covered in this article:

Excel to PDF Conversion REST API and Ruby SDK

To convert XLSX to PDF, we will use the Ruby SDK of GroupDocs.Conversion Cloud API. The API supports conversion to any format you need and handles more than 50 document types, including Word, Excel, PowerPoint, PDF, HTML, JPG, PNG, and CAD. REST API also provides .NET, Java, PHP, Node.js, Android, and Python SDKs as its document conversion family members for the Cloud EST API.

Install GroupDocs.Conversion Cloud in your Ruby application with the following command:

gem install groupdocs_conversion_cloud

Retrieve your Client ID and Client Secret from the dashboard before you start. Add them to the code as shown below:

Convert Excel to PDF using a REST API in Ruby

You can convert Excel spreadsheets to PDF on the cloud by following these simple steps:

  • Create an instance of the ConvertApi
  • Create an instance of the ConvertSettings
  • Set the XLSX file path
  • Assign “pdf” to the format
  • Create an instance of the SpreadsheetLoadOptions
  • Set hide_comments and one_page_per_sheet values
  • Provide load_options and the output file path
  • Create a ConvertDocumentRequest with ConvertSettings
  • Call the ConvertApi.convertDocument() method with the request

The code example below demonstrates how to convert an Excel spreadsheet to a PDF document using a REST API in Ruby.

Convert Excel to PDF using a REST API in Ruby

Convert Excel to PDF using a REST API in Ruby

Convert Specific Excel Spreadsheets to PDF in Ruby

To convert only selected worksheets to PDF, follow these steps:

  • Create an instance of the ConvertApi
  • Create an instance of the ConvertSettings
  • Set the XLSX file path
  • Assign “pdf” to the format
  • Create an instance of PdfConvertOptions
  • Specify the worksheet pages to convert
  • Set PdfConvertOptions and provide the output file path
  • Create a ConvertDocumentRequest with ConvertSettings
  • Call the ConvertApi.convertDocument() method with the request

The following example shows how to convert specific Excel worksheets to a PDF document using a REST API in Ruby.

Convert Specific Excel Spreadsheets to PDF in Ruby

Convert Specific Excel Spreadsheets to PDF in Ruby

Excel to PDF Conversion in Ruby using Advanced Options

Use the steps below to apply advanced settings during conversion:

  • Create an instance of the ConvertApi
  • Create an instance of the ConvertSettings
  • Set the XLSX file path
  • Assign “pdf” to the format
  • Create an instance of the SpreadsheetLoadOptions
  • Configure options such as hideComments, onePagePerSheet, etc.
  • Create an instance of PdfConvertOptions
  • Define additional convert options
  • Provide load_options, convertOptions, and the output file path
  • Create a ConvertDocumentRequest with ConvertSettings
  • Call the ConvertApi.convertDocument() method with the request

The code example below illustrates how to convert an Excel spreadsheet to PDF with advanced conversion options:

Convert Excel to PDF using a REST API using Ruby

Excel to PDF Conversion with Advanced Options

Convert Excel to PDF and Add Watermark

To add a watermark to the converted PDF, follow these steps:

  • Create an instance of the ConvertApi
  • Create an instance of the ConvertSettings
  • Set the XLSX file path
  • Assign “pdf” to the format
  • Create an instance of the WatermarkOptions
  • Define watermark text, color, width, height, etc.
  • Set the PdfConvertOptions and attach WatermarkOptions
  • Provide convert options and the output file path
  • Create a ConvertDocumentRequest with ConvertSettings
  • Call the ConvertApi.convertDocument() method with the request

The example below shows how to convert an Excel spreadsheet to PDF and add a watermark using a REST API in Ruby.

Convert Excel to PDF and Add Watermark

Convert Excel to PDF and Add Watermark

Online XLSX to PDF Converter for Free

How to convert Excel to PDF online? Try the free online XLSX to PDF converter tool, which is built on the same API.

Conclusion

In this article, you learned how to convert Excel to PDF documents on the cloud and how to add a watermark using Ruby. You also saw how to apply advanced conversion options. Explore more about GroupDocs.Conversion Cloud API in the documentation and browse the API Reference to interact with the services directly.

Ask a question

If you have any queries about XLSX to PDF Converter, please feel free to ask us on the Forum.

See Also