Convert XLSX to a PDF document to download

Dear Gembox team, I already have another library and I use a package.GetAsByteArray, now I want to convert to PDF to download it

async Task<byte[]> GenerateExcelWorkbookByDepartmentAsync() 
{
      using var stream = new FileStream(pathFile, FileMode.Open, FileAccess.Read);
      var package = new ExcelPackage(stream);
      ...
      return package.GetAsByteArray();
}
using MemoryStream ms = new MemoryStream(await GenerateExcelWorkbookByDepartmentAsync());
ExcelFile workbook = ExcelFile.Load(ms);
workbook.Save("Convert1.pdf", new PdfSaveOptions() { SelectionType = SelectionType.EntireFile });

I tried doing the demo but it didn’t work

That should work, what problem do you have with that code?

1 Like

I ran the code and nothing happened. Not throw exception

And the “Convert1.pdf” file is not generated?
For testing purposes, try specifying the full path in the Save method, for example:

workbook.Save(@"C:\temp\Convert1.pdf", new PdfSaveOptions() { SelectionType = SelectionType.EntireFile });

If your problem remains, please send us a small Visual Studio project that reproduces your issue so we can investigate it.

Regards,
Mario

1 Like

Thanks, It has been downloaded, but there is no notification from the browser and it also does not appear in the Download tab of the browser (Ctrl J).

That is because you’re saving it directly to that path, you’re not downloading it.
For downloading, please check our ASP.NET Core example:

Or try something like this:

using MemoryStream excelStream = new MemoryStream(await GenerateExcelWorkbookByDepartmentAsync());
ExcelFile workbook = ExcelFile.Load(excelStream);

using MemoryStream pdfStream = new MemoryStream();
PdfSaveOptions pdfOptions = new PdfSaveOptions() { SelectionType = SelectionType.EntireFile };
workbook.Save(pdfStream, pdfOptions);

// Download file.
return File(pdfStream, pdfOptions.ContentType, "Convert1.pdf");

Does this solve your issue?

return File(pdfStream, pdfOptions.ContentType, "Convert1.pdf");

This command has an error: Non-invocable member ‘File’ cannot be used like a method.

What application do you have, is it ASP.NET, ASP.NET Core, or something else?
Please send us your Visual Studio project so that I can help you out with it.

Thank you very much for your help, I did it, the above error was because I used the wrong namespace. <3