We're sorry AsposeCloud doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.cloud

Convert Excel to PDF in C# API Crashes Intermittently

Hi,

One of the features of our application is to create multiple books by converting the excel and word documents to PDF and ultimately merging them into a single PDF. The following code snippet is from the service worker task.

private async Task ConvertToPdf(string fileUri, string guid, string bookFileName)
{
try
{
_telemetryTracker.TrackTrace(string.Format(“File URI: {0}”, fileUri), true);
var fileInfo = fileUri.Split("/");

            CloudStorageAccount account = null;
            CloudBlobContainer cloudBlobContainer = null;
            MemoryStream memstream = new MemoryStream();

            if (CloudStorageAccount.TryParse(_settings.CloudStorageConnection, out account))
            {
                var splitext = fileInfo[2].Split(".");
                var fileNameNoExt = Path.GetFileNameWithoutExtension(fileInfo[2]);
                var ext = splitext[splitext.Length - 1];
                CloudBlobClient client = account.CreateCloudBlobClient();
                var container = client.GetContainerReference(fileInfo[1]);
                var blob = container.GetBlockBlobReference(fileInfo[2]);

                if (ext == "xlsx" || ext == "xls" || ext == "xlsm")
                {
                    var apiInstance = new CellsApi(_settings.AsposeClientId, _settings.AsposeClientKey);
                    var name = fileInfo[2];  // string | The document name.
                    string needRemovedSheet = "Sheet1";
                    string needRemovedSheet1 = "Sheet2";
                    string needRemovedSheet2 = "Sheet3";
                    string needRemovedSheet3 = "Sheet4";
                    string needRemovedSheet4 = "Sheet5";
                    string needRemovedSheet5 = "Sheet6";
                    PdfSaveOptions saveOptions = new PdfSaveOptions(); // SaveOptions | Save options. (optional) 
                    saveOptions.SaveFormat = "pdf";
                    saveOptions.OnePagePerSheet = true;
                    var newfilename = $"converted/{fileNameNoExt}.pdf";  // string | The new file name. (optional) 
                    var isAutoFitRows = false;  // bool? | Autofit rows. (optional)  (default to false)
                    var isAutoFitColumns = false;  // bool? | Autofit columns. (optional)  (default to false)
                    var folder = "refresh";  // string | The document folder. (optional) 
                    var storage = _settings.AsposeStorageName;  // string | storage name. (optional) 
                    //remove specified worksheets from excel sheets 
                    await apiInstance.CellsWorksheetsDeleteWorksheetAsync(name, needRemovedSheet, folder, storage);
                    await apiInstance.CellsWorksheetsDeleteWorksheetAsync(name, needRemovedSheet1, folder, storage);
                    await apiInstance.CellsWorksheetsDeleteWorksheetAsync(name, needRemovedSheet2, folder, storage);
                    await apiInstance.CellsWorksheetsDeleteWorksheetAsync(name, needRemovedSheet3, folder, storage);
                    await apiInstance.CellsWorksheetsDeleteWorksheetAsync(name, needRemovedSheet4, folder, storage);
                    await apiInstance.CellsWorksheetsDeleteWorksheetAsync(name, needRemovedSheet5, folder, storage);
                    // Convert document and save result to storage.
                    Aspose.Cells.Cloud.SDK.Model.SaveResponse result = apiInstance.CellsSaveAsPostDocumentSaveAs(name, saveOptions, newfilename, isAutoFitRows, isAutoFitColumns, folder, storage);
                    LogInformation($"Successfully converted: {name} to PDF for {bookFileName}");
                }
                else if (ext == "docx" || ext == "doc" || ext == "dotx")
                {
                    CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileInfo[2]);
                    await blockBlob.DownloadToStreamAsync(memstream);

                    var format = "pdf";
                    WordsApi WordsApi = new WordsApi(_settings.AsposeClientId, _settings.AsposeClientKey);
                    memstream.Position = 0;  //reset stream position so it's not starting at EOF
                    var request = new ConvertDocumentRequest(memstream, format);
                    var result = WordsApi.ConvertDocument(request);
                    var cont = client.GetContainerReference("refresh");
                    var newfilename = $"converted/{fileNameNoExt}.pdf";
                    CloudBlockBlob bl = cont.GetBlockBlobReference(newfilename);
                    await bl.UploadFromStreamAsync(result);
                    LogInformation($"Properly Converted file: {newfilename} for {bookFileName}");
                }
            }
        }
        catch (Exception ex)
        {
            _telemetryTracker.TrackException(new Exception("Failed to convert the file to pdf",ex));
            throw;
        }
    }

Intermittently this process fails i.e It doesn’t throw any exceptions but simply crashes midway. Looking at the logs, I see that it fails to convert all the excel templates into PDF ( There are 14 excel templates that needs to be converted to PDF in our case and the call to this method is in a for loop) but the traces show ‘FileURI’ message for every document and only few ‘successfully converted’ or ‘properly converted’ messages ( we are writing these messages to logs as shown in the above code). Couldn’t find anything when debugged and is successful each time. Can anyone please guide me to proceed further?
Any help is greatly appreciated. Thank you in advance.

PS: Noticed that this usually happens when multiple books (4 in our case) are created at the same time. They are multi-threaded processes.

@s2205,

We will check the server logs and analyze the cause of the exception.
And we will release Aspose.Cells Cloud SDK 21.10 for DotNet next week.
It adds new methods to delete multiple worksheets and batch convert workbooks.
hope this helps.

@wangtao Thank you for your response. Method to delete multiple worksheets at once would be really helpful.

@s2205,

We had released Aspose.Cells Cloud SDK 21.10 for DotNet.
It added two new features.

  1. It supports deleting multiple worksheets in a single call.
  2. It supports batch conversion.

Please refer to the following online development document about the feature.


Thank you @wangtao I was able to implement deletion of multiple worksheets successfully.