Formatting issue in Excel to PDF Conversion

Thanks, I’ll do that even though I think that it’s better to do this in one API call.

Another issue I have is, that the Excel conversion is not honoring the pagesize / pagebreak setting. See this example. The page size is A4 in the Excel workbook, but when converting the second sheet, there’s no page break. The sheet is two a4 pages when printed (3 in total) but only 2 pages in total when converted?

https://mofassets.blob.core.windows.net/tempword/Boregnskab5010212_73463dd71e12461aa30980fcdd5bbb93.xlsx

https://mofassets.blob.core.windows.net/tempword/Boregnskab5010212_73463dd71e12461aa30980fcdd5bbb93.pdf

@MortenF,

We will check it with your provided information.

@MortenF,

We checked the file you provided and after analysis, we believe that this is caused by the default OnePagePerSheet parameter being true when converting the file.

You can set the OnePagePerSheet parameter to false in the query parameters

Thanks,
And how to do that?

I have the CellsApi for dotnet v 23.3 and first of all it says that all is obsolete/deprecated. Ther’s no higher version available through nuget.

https://mofassets.blob.core.windows.net/tempword/asposenuget.png

https://mofassets.blob.core.windows.net/tempword/asposedeprecated.png

aspose-cells/Aspose.Cells-for-.NET: Aspose.Cells for .NET examples, plugins and showcases (github.com)

I’m using the, also obsolete/deprecated method, CellsWorkbookPutConvertWorkbook

So how do I 1. update to latest not deprecated version, and 2. Convert using the parameters?

@MortenF,
You may use the new model,
Reference Code:

CellsApi cellsApi = new CellsApi(“clientId”, “clientSecrent”);
PutConvertWorkbookRequest putConvertWorkbookRequest = new PutConvertWorkbookRequest();
putConvertWorkbookRequest.File.Add(“Book1.xlsx”,File.Open(“Book1.xlsx”, FileMode.Open));
putConvertWorkbookRequest.format = “json”;
cellsApi.PutConvertWorkbook(putConvertWorkbookRequest);

And you may refer to test case on github.com.

As I said, that’s not working with the latest Nuget package (23.3.0)

https://mofassets.blob.core.windows.net/tempword/asposenuget.png
https://mofassets.blob.core.windows.net/tempword/aspose2.png
https://mofassets.blob.core.windows.net/tempword/aspose3.png
https://mofassets.blob.core.windows.net/tempword/aspose1.png

@MortenF,

I created a test project and please check it.cloud.sdk.test.zip (1.3 KB)

This is a dotnet Core project. I’m on .NET framework 4.8.

From Nuget:

Aspose.Cells-Cloud 23.3.0

.NET Standard 2.0 .NET Framework 4.5.2

@MortenF,

We found it and will fix it ASAP.

1 Like

@MortenF,

Please try the Aspose.Cells-Cloud 23.4.1 from Nuget.

1 Like

Thanks, its still not working? The example on GitHub is marked obsolete (UploadFile)

I managed to get it working with the old/obsolete method. Could you translate this into the new model?

var opt = new Dictionary<string, string>
{
{“OnePagePerSheet”, “false” }
};

                        var response = cellsApi.CellsWorkbookPutConvertWorkbook(System.IO.File.OpenRead(Path.Combine(tempFolder, filename)), "pdf", null, null,null,opt);

This is not working:

string localName = “Book1.xlsx”;
string remoteName = “Book1.xlsx”;

this.UploadFile( localName, remoteFolder + “/” + remoteName, “”);

var saveOptions = new PdfSaveOptions()
{
SaveFormat = format
};
var request = new PostWorkbookSaveAsRequest(
name: remoteName,
newfilename: newfilename,
saveOptions: saveOptions,
folder: remoteFolder
);
cellsApi.PostWorkbookSaveAs(request);

@MortenF,

I built a test project and passed the test.

Please refer to testnet48.zip (3.0 KB).

@MortenF,

I built a test project which added convert code. Please refer testnet48 (2).zip (3.2 KB).

testnet48.zip: save as code.

testnet48 (2).zip: save as and convert code.

Still not working, it hangs at the cellsApi.PostWorkbookSaveAs(postWorkbookSaveAsRequest) No errors returning, just a timeout, isn’t there a way just to convert with options without saving anything as in the deprecated function?

I tried this with no luck:

putConvertWorkbookRequest.extendQueryParameterMap = new Dictionary<string, string> { };
putConvertWorkbookRequest.extendQueryParameterMap.Add(“OnePagePerSheet”, “false”);

Working:

var opt = new Dictionary<string, string>
{
{“OnePagePerSheet”, “false” }
};

var response = cellsApi.CellsWorkbookPutConvertWorkbook(System.IO.File.OpenRead(Path.Combine(tempFolder, filename)), “pdf”, null, null, null, opt);

@MortenF,

Please refer to the following code:
testnet48 (3).zip (3.5 KB)

        // Create CellsApi Object.
        CellsApi cellsApi = new CellsApi("your client id", "your client secret");

        // Upload File.
        Stream fileStream = File.OpenRead(@"D:\cells.test\testdata\Book1.xlsx");
        UploadFileRequest uploadFileRequest = new UploadFileRequest();
        uploadFileRequest.UploadFiles = new Dictionary<string, Stream>() { };
        uploadFileRequest.UploadFiles.Add("Book1.xlsx", fileStream);
        uploadFileRequest.path = "NetTest";
        cellsApi.UploadFile(uploadFileRequest);
        fileStream.Close();

        // Call SaveAs Api, save Book1.xlsx as Book1ToPDF.pdf of NetTest folder.
        var saveOptions = new PdfSaveOptions()
        {
            SaveFormat = "pdf"
        };
        PostWorkbookSaveAsRequest postWorkbookSaveAsRequest = new PostWorkbookSaveAsRequest();
        postWorkbookSaveAsRequest.checkExcelRestriction = true;
        postWorkbookSaveAsRequest.name = "Book1.xlsx";
        postWorkbookSaveAsRequest.folder = "NetTest";
        postWorkbookSaveAsRequest.newfilename = "NetTest/Book1ToPDF.pdf";
        postWorkbookSaveAsRequest.saveOptions = saveOptions;
        cellsApi.PostWorkbookSaveAs(postWorkbookSaveAsRequest);

        // Download Book1ToPDF.pdf of NetTest folder.
        DownloadFileRequest downloadFileRequest = new DownloadFileRequest();
        downloadFileRequest.path = "NetTest/Book1ToPDF.pdf";
        Stream stream = cellsApi.DownloadFile(downloadFileRequest);
        using (Stream desFile = File.Create("Book1Saveas.pdf"))
        {
            stream.Position = 0;
            stream.CopyTo(desFile);
            stream.Close();
            desFile.Flush();
            desFile.Close();
        }
        stream.Close();

        // Convert local Book1.xlsx to pdf file with the Convert Api.
        PutConvertWorkbookRequest putConvertWorkbookRequest = new PutConvertWorkbookRequest();

        putConvertWorkbookRequest.checkExcelRestriction = true;
        putConvertWorkbookRequest.format = "pdf";            
        putConvertWorkbookRequest.File = new Dictionary<string, Stream> { };

        fileStream = File.OpenRead(@"D:\cells.test\testdata\Book1.xlsx");
        putConvertWorkbookRequest.File.Add("Book1.xlsx", fileStream);
        stream = cellsApi.PutConvertWorkbook(putConvertWorkbookRequest);
        using (Stream desFile = File.Create("Book1.pdf"))
        {
            stream.Position = 0;
            stream.CopyTo(desFile);
            stream.Close();
            desFile.Flush();
            desFile.Close();
        }
        fileStream.Close();

        // Convert local Book1.xlsx to pdf file with the deprecated Convert Api. 
        // Add extended query parameters.
        var opt = new Dictionary<string, string>() { { "OnePagePerSheet", "false" } };

        fileStream = File.OpenRead(@"D:\cells.test\testdata\Book1.xlsx");
        stream  =  cellsApi.CellsWorkbookPutConvertWorkbook(fileStream, "pdf", null, null, null, opt);
        using (Stream desFile = File.Create("Book1Convert.pdf"))
        {
            stream.Position = 0;
            stream.CopyTo(desFile);
            stream.Close();
            desFile.Flush();
            desFile.Close();
        }

By the way, regarding the timeout issue, we are checking and trying to fix it.

You haven’t answered my question, but I guess not when seeing your code.

Even this simple test isn’t working in my code (no response)

PutConvertWorkbookRequest putConvertWorkbookRequest = new PutConvertWorkbookRequest();
putConvertWorkbookRequest.checkExcelRestriction = true;
putConvertWorkbookRequest.format = “pdf”;
putConvertWorkbookRequest.File = new Dictionary<string, Stream> { };
var filestream = System.IO.File.Open(Path.Combine(tempFolder, filename), FileMode.Open);
putConvertWorkbookRequest.File.Add(filename, filestream);
Stream stream = cellsApi.PutConvertWorkbook(putConvertWorkbookRequest);

@MortenF,

I had tested your provided code. I think that your code is ok.
The timeout question is caused by a net question or server performance. Please provide your email. we will check your run logs on the server.

Strange, it’s just hanging with no reply in my code. e-mail is mfahrnholz(at)gmail.com

@MortenF,

I found more than 20 logs about calling the PutConvertWorkbook API.
These records are normal. I will contact other colleagues to check the load balancer log, this will take some time.

Could you post your development environment here? I’ll simulate this environment for testing.

I did some digging and cretaed a test project with my code which worked.

But what I found was that you are using (and installing as a dependency) A very old version of RestSharp. It is deprecated and has serious vulnerabilities Incorrect Regular Expression in RestSharp · CVE-2021-27293 · GitHub Advisory Database

Im using the newer packages elsewhere in my project. And when updating the package in my test project it isn’t working

Maybe I should just use the REST api with a good old httpclient instead. Your wrapper is obviously flawed.