Tutorial: Converting Excel Files to PDF with Aspose.Cells Cloud API
Prerequisites
Before starting this tutorial, make sure you have:
- An Aspose Cloud account with an active subscription or free trial
- Your Client ID and Client Secret from the Aspose Cloud Dashboard
- Basic understanding of REST APIs and your preferred programming language
- A sample Excel file for testing (you can use your own or download our sample)
Introduction to Excel to PDF Conversion
Converting Excel spreadsheets to PDF is one of the most common tasks when working with spreadsheet data. PDFs provide a consistent, universal format that preserves your formatting and is easily shareable. The Aspose.Cells Cloud API provides multiple approaches to perform this conversion, giving you flexibility based on your specific requirements.
Using the REST API
Aspose.Cells Cloud provides three different API endpoints to convert Excel to PDF:
PUT /cells/convert
- Converts directly from request content (best for client-side applications)POST /cells/{name}/saveAs
- Saves an existing Excel file to PDF format in cloud storageGET /cells/{name}
- Exports a stored Excel file to PDF format
Let’s explore each approach in detail.
Approach 1: Direct Conversion with PUT /cells/convert
This approach is ideal when you have the Excel file locally and want to convert it in a single API call without storing it first.
Try It Yourself: Using cURL
curl -v "https://api.aspose.cloud/v3.0/cells/convert?format=pdf" \
-X PUT \
-F "file=@YourExcelFile.xlsx" \
-H "Content-Type: multipart/form-data" \
-H "Accept: multipart/form-data" \
-H "Authorization: Bearer <your_jwt_token>"
Replace <your_jwt_token>
with your actual authentication token. The response will contain the PDF file content that you can save to your local system.
Implementation in C#
// For complete examples and data files, please go to https://github.com/aspose-cells-cloud/aspose-cells-cloud-dotnet/
// Initialize the API with your client credentials
CellsApi cellsApi = new CellsApi(clientId, clientSecret);
// Prepare the conversion request
var fileStream = File.OpenRead("Book1.xlsx");
var request = new PutConvertWorkbookRequest(
file: new Dictionary<string, Stream> { {"Book1.xlsx", fileStream} },
format: "pdf"
);
// Execute the conversion
var response = cellsApi.PutConvertWorkbook(request);
// Save the resulting PDF
using (var fileStream = File.Create("output.pdf"))
{
response.CopyTo(fileStream);
}
Console.WriteLine("Excel file successfully converted to PDF!");
Implementation in Java
// Initialize API client with your credentials
CellsApi cellsApi = new CellsApi(clientId, clientSecret);
// Prepare the conversion request
PutConvertWorkbookRequest request = new PutConvertWorkbookRequest();
request.setFormat("pdf");
// Add the input file
HashMap<String, File> fileMap = new HashMap<String, File>();
fileMap.put("Book1.xlsx", new File("Book1.xlsx"));
request.setFile(fileMap);
// Execute the conversion
File result = cellsApi.putConvertWorkbook(request);
// The result contains the PDF file
System.out.println("Excel file successfully converted to PDF!");
Approach 2: SaveAs with POST /cells/{name}/saveAs
This approach is useful when your Excel file is already stored in Aspose Cloud Storage and you want to convert it to PDF.
Try It Yourself: Using cURL
curl -v "https://api.aspose.cloud/v3.0/cells/Book1.xlsx/saveAs?newfilename=output.pdf" \
-X POST \
-d "{'SaveFormat':'pdf', 'EnableHTTPCompression': true}" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer <your_jwt_token>"
Implementation in C#
// Initialize the API with your client credentials
CellsApi cellsApi = new CellsApi(clientId, clientSecret);
// Upload your file to cloud storage first (if not already there)
cellsApi.UploadFile(new UploadFileRequest(
uploadFiles: new Dictionary<string, Stream> { { "Book1.xlsx", File.OpenRead("Book1.xlsx") } },
path: "Book1.xlsx"
));
// Prepare conversion options
var saveOptions = new PdfSaveOptions {
SaveFormat = "pdf",
EnableHTTPCompression = true,
// Optional: Customize your PDF
OnePagePerSheet = true
};
// Execute the conversion
var result = cellsApi.PostDocumentSaveAs(new PostDocumentSaveAsRequest(
name: "Book1.xlsx",
newfilename: "output.pdf",
saveOptions: saveOptions
));
Console.WriteLine("Excel file successfully converted to PDF and saved to cloud storage!");
Approach 3: Direct Export with GET /cells/{name}
This approach allows you to export an Excel file that’s already in cloud storage directly to PDF format.
Try It Yourself: Using cURL
curl -v "https://api.aspose.cloud/v3.0/cells/Book1.xlsx?format=pdf" \
-X GET \
-H "Accept: application/json" \
-H "Authorization: Bearer <your_jwt_token>" \
-o output.pdf
Implementation in C#
// Initialize the API with your client credentials
CellsApi cellsApi = new CellsApi(clientId, clientSecret);
// Upload your file to cloud storage first (if not already there)
cellsApi.UploadFile(new UploadFileRequest(
uploadFiles: new Dictionary<string, Stream> { { "Book1.xlsx", File.OpenRead("Book1.xlsx") } },
path: "Book1.xlsx"
));
// Execute the export
var response = cellsApi.GetWorkbook(new GetWorkbookRequest(
name: "Book1.xlsx",
format: "pdf"
));
// Save the resulting PDF
using (var fileStream = File.Create("output.pdf"))
{
response.CopyTo(fileStream);
}
Console.WriteLine("Excel file successfully exported to PDF!");
Customizing PDF Conversion Options
Aspose.Cells Cloud offers many options to customize the PDF output. Here are some useful options you can include in your requests:
Option | Description |
---|---|
OnePagePerSheet | Places each worksheet on a separate page |
PrintingPageType | Controls page layout options (e.g., ignore blank pages) |
Compliance | Sets PDF compliance level (PDF/A, etc.) |
EmbedFullFonts | Embeds full fonts in the PDF |
TextCompression | Sets text compression level |
Example with Custom Options (C#)
var saveOptions = new PdfSaveOptions {
SaveFormat = "pdf",
EnableHTTPCompression = true,
OnePagePerSheet = true,
Compliance = "PDF/A-1b",
EmbedFullFonts = true
};
// Use this saveOptions object in your conversion request
Troubleshooting Common Issues
Issue: Authentication Failed
Solution: Double check your Client ID and Client Secret. Make sure you’re correctly generating the JWT token.
Issue: File Not Found
Solution: Verify that the file exists in your cloud storage or that you’re correctly providing the file in your request.
Issue: Converting Large Files
Solution: For large files, use the SaveAs approach with EnableHTTPCompression set to true.
What You’ve Learned
In this tutorial, you’ve learned:
- Three different approaches to convert Excel files to PDF using Aspose.Cells Cloud API
- How to implement each approach using cURL, C#, and Java
- How to customize the PDF output with various options
- Common troubleshooting tips for the conversion process
Further Practice
To reinforce your learning, try these exercises:
- Convert an Excel file with multiple worksheets, setting OnePagePerSheet to both true and false to see the difference
- Create a simple application that allows users to upload Excel files and download them as PDFs
- Try batch-converting multiple Excel files to PDF in a single operation