Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# Aspose.Cells.Cloud: Method to Convert to PDF fails intermittently #4

Open
bvrit-sravya opened this issue Nov 2, 2021 · 0 comments

Comments

@bvrit-sravya
Copy link

bvrit-sravya commented Nov 2, 2021

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: filename’ 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.

@bvrit-sravya bvrit-sravya changed the title Aspose.Cells.Cloud: Method to Convert to PDF fails intermittently C# Aspose.Cells.Cloud: Method to Convert to PDF fails intermittently Nov 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant