ASP.NET MVC EPPlus Download Excel File - c#

So I'm using the fancy EPPlus library to write an Excel file and output it to the user to download. For the following method I'm just using some test data to minimize on the code, then I'll add the code I'm using to connect to database later. Now I can download a file all fine, but when I go to open the file, Excel complains that it's not a valid file and might be corrupted. When I go to look at the file, it says it's 0KB big. So my question is, where am I going wrong? I'm assuming it's with the MemoryStream. Haven't done much work with streams before so I'm not exactly sure what to use here. Any help would be appreciated!
[Authorize]
public ActionResult Download_PERS936AB()
{
ExcelPackage pck = new ExcelPackage();
var ws = pck.Workbook.Worksheets.Add("Sample1");
ws.Cells["A1"].Value = "Sample 1";
ws.Cells["A1"].Style.Font.Bold = true;
var shape = ws.Drawings.AddShape("Shape1", eShapeStyle.Rect);
shape.SetPosition(50, 200);
shape.SetSize(200, 100);
shape.Text = "Sample 1 text text text";
var memorystream = new MemoryStream();
pck.SaveAs(memorystream);
return new FileStreamResult(memorystream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { FileDownloadName = "PERS936AB.xlsx" };
}

Here's what I'm using - I've been using this for several months now and haven't had an issue:
public ActionResult ChargeSummaryData(ChargeSummaryRptParams rptParams)
{
var fileDownloadName = "sample.xlsx";
var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
var package = CreatePivotTable(rptParams);
var fileStream = new MemoryStream();
package.SaveAs(fileStream);
fileStream.Position = 0;
var fsr = new FileStreamResult(fileStream, contentType);
fsr.FileDownloadName = fileDownloadName;
return fsr;
}
One thing I noticed right off the bat is that you don't reset your file stream position back to 0.

Related

Blazor WASM Load and display large pdfs by splitting them as streams

I'm working on a Blazor WASM App and I want my users to easily open pdf files on specific pages that contain additional information.
I cannot distribute those files myself or upload them to any kind of server. Each user has to provide them themselves.
Because the files are up to 60MB big I cannot convert the uploaded file to base64 and display them as described here.
However I don't have to display the whole file and could just load the needed page +- some pages around them.
For that I tried using iText7 ExtractPageRange(). This answer indicates, that I have to override the GetNextPdfWriter() Method and to store all streams in an collection.
class ByteArrayPdfSplitter : PdfSplitter {
public ByteArrayPdfSplitter(PdfDocument pdfDocument) : base(pdfDocument) {
}
protected override PdfWriter GetNextPdfWriter(PageRange documentPageRange) {
CurrentMemoryStream = new MemoryStream();
UsedStreams.Add(CurrentMemoryStream);
return new PdfWriter(CurrentMemoryStream);
}
public MemoryStream CurrentMemoryStream { get; private set; }
public List<MemoryStream> UsedStreams { get; set; } = new List<MemoryStream>();
Then I thought I could merge those streams and convert them to base64
var file = loadedFiles.First();
using (MemoryStream ms = new MemoryStream())
{
var rs = file.OpenReadStream(maxFileSize);
await rs.CopyToAsync(ms);
ms.Position = 0;
//rs needed to be converted to ms, because the PdfReader constructer uses a
//synchronious read that isn't supported by rs and throws an exception.
PdfReader pdfReader = new PdfReader(ms);
var document = new PdfDocument(pdfReader);
var splitter = new ByteArrayPdfSplitter(document);
var range = new PageRange();
range.AddPageSequence(1, 10);
var splitDoc = splitter.ExtractPageRange(range);
//Edit commented this out, shouldn't have been here at all leads to an exception
//splitDoc.Close();
var outputMs = new MemoryStream();
foreach (var usedMs in splitter.UsedStreams)
{
usedMs.Position = 0;
outputMs.Position = outputMs.Length;
await usedMs.CopyToAsync(outputMs);
}
var data = outputMs.ToArray();
currentPdfContent = "data:application/pdf;base64,";
currentPdfContent += Convert.ToBase64String(data);
pdfLoaded = true;
}
This however doesn't work.
Has anyone a suggestion how to get this working? Or maybe a simpler solution I could try.
Edit:
I took a closer look in debug and it seems like, the resulting stream outputMs is always empty. So it is probably a problem in how I split the pdf.
After at least partially clearing up my misconception of what it means to not being able to access the file system from blazor WASM I managed to find a working solution.
await using MemoryStream ms = new MemoryStream();
var rs = file.OpenReadStream(maxFileSize);
await using var fs = new FileStream("test.pdf", FileMode.Create)
fs.Position = 0;
await rs.CopyToAsync(fs);
fs.Close();
string path = "test.pdf";
string range = "10 - 15";
var pdfDocument = new PdfDocument(new PdfReader("test.pdf"));
var split = new MySplitter(pdfDocument);
var result = split.ExtractPageRange(new PageRange(range));
result.Close();
await using var splitFs = new FileStream("split.pdf", FileMode.Open))
await splitFs.CopyToAsync(ms);
var data = ms.ToArray();
var pdfContent = "data:application/pdf;base64,";
pdfContent += System.Convert.ToBase64String(data);
Console.WriteLine(pdfContent);
currentPdfContent = pdfContent;
With the MySplitter Class from this answer.
class MySplitter : PdfSplitter
{
public MySplitter(PdfDocument pdfDocument) : base(pdfDocument)
{
}
protected override PdfWriter GetNextPdfWriter(PageRange documentPageRange)
{
String toFile = "split.pdf";
return new PdfWriter(toFile);
}
}

How to download edited PDF using ITextSharp and C# Blazor

I have a Blazor application in which I'm trying to download a PDF I have edited using ITextSharp. All resources I search tend to use the Response class which I don't have (I believe because I am using .NET 6). I found some solutions that that send the byte array to a javascript function but I'm having no such luck. I get the error
System.ArgumentNullException: 'Value cannot be null.' When I Envoke the JS function.
I've posted my code below. Though I'm not passing any null objects, I do see some potential errors in my memory stream and I'm not sure why (See Image). Any information on where I'm going wrong or any easy approach would be appreciated. Thanks.
MemoryStream output = new MemoryStream();
PdfReader pdfReader = new PdfReader("MY_SOURCE_PATH_CHANGED_FOR_POST");
PdfStamper pdfStamper = new PdfStamper(pdfReader, output);
// Make Edits
//editPdf(ref pdfStamper.AcroFields);
pdfStamper.FormFlattening = false;
pdfStamper.Close();
await JSRuntime.InvokeVoidAsync("saveAsFile", "test.pdf", output.ToArray());
And the JS
function saveAsFile(filename, bytesBase64)
{
if (navigator.msSaveBlob) {
var data = window.atob(bytesBase64);
var bytes = new Unit8Array(data.length);
for (var i = 0; i < data.length; i++) {
bytes[i] = data.charCodeAt(i);
}
var blob = new Blob([bytes.buffer], { type: "application/octet-stream" });
navigator.msSaveBlob(blob, filename);
}
else
{
var link = document.createElement("a");
link.download = filename;
link.href = "data:application/octet-stream;base64," + bytesBase64;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
*Note for the sake of the post I changed the source path but I have verified the path does return a PDF, from a direct URL from azure blob storage.
For anyone who needs it, I solved this issue by creating a controller.
[HttpGet("{appId}/{formCode}")]
public FileContentResult DownloadForm(int appId, int formCode)
{
byte[] PdfData = Repo.GetFormBytes(appId, (Enums.FormType)formCode);
if (PdfData != null)
{
FileContentResult file = new FileContentResult(PdfData, "application/pdf");
file.FileDownloadName = Repo.MostRecentFileName;
return file;
}
else
return null;
}
Where GetFormBytes returns byte array output.ToArray() after it was edited. (See question).

angular 4 download pdf comes out blank

Hello I am trying to download a pdf from a report that i create on the server. When I create it I save it on the server for a moment then I make a second call and download it.
The following is the code that is on the web API:
var rootDirectory = HostingEnvironment.MapPath("~");
var filePath = $#"{rootDirectory}\Temp\{file}.pdf";
var bytes = File.ReadAllBytes(filePath);
File.Delete(filePath);
var memoryStream = new MemoryStream(bytes);
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(memoryStream.ToArray())
};
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "report.pdf"
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return result;
then I get back to the typescript and here is the code that I use do download it:
this.reportsService
.download(id)
.subscribe(r => {
let pdf =r._body;
var pdfName = `${this.selectedReport.name}.pdf`;
let blob = new Blob([pdf], { type: 'application/pdf' });
window.open(URL.createObjectURL(blob));
});
});
I should mention that when I open the pdf from where it is saved on the server it is correct.
Can someone please tell me what I'm missing?

iTextSharp generated PDF works in browser but not after download

I have a PDF which is generated by iTextSharp in C# - it's a template PDF, which gets some additional lines of text added using stamper, then pushed to S3 and finally returned to the browser as a file stream (using mvc.net).
The newly added lines work fine when the PDF is viewed in the browser (Chrome), but when I download the PDF and open it locally (with Preview or Adobe Acrobat on Mac), only the template is showing, and the newly added lines are gone.
What could cause this?
Here's a code example: (condensed)
using(var receiptTemplateStream = GetType().Assembly.GetManifestResourceStream("XXXXX.DepositReceipts.Receipt.pdf" ))
{
var reader = new PdfReader(receiptTemplateStream);
var outputPdfStream = new MemoryStream();
var stamper = new PdfStamper(reader, outputPdfStream) { FormFlattening = true, FreeTextFlattening = true };
var _pbover = stamper.GetOverContent(1);
using (var latoLightStream = GetType().Assembly.GetManifestResourceStream("XXXXX.DepositReceipts.Fonts.Lato-Light.ttf"))
using (var latoLightMS = new MemoryStream())
{
_pbover.SetFontAndSize(latoLight, 11.0f);
var verticalPosition = 650;
_pbover.ShowTextAligned(0, account.company_name, 45, verticalPosition, 0);
verticalPosition = verticalPosition - 15;
var filename = "Receipt 0001.pdf";
stamper.SetFullCompression();
stamper.Close();
var file = outputPdfStream.ToArray();
using (var output = new MemoryStream())
{
output.Write(file, 0, file.Length);
output.Position = 0;
var response = await _s3Client.PutObjectAsync(new PutObjectRequest()
{
InputStream = output,
BucketName = "XXXX",
CannedACL = S3CannedACL.Private,
Key = filename
});
}
return filename;
}
}
This was a funky one!
I had another method in the same solution that worked without problems. It turns out that the template PDF, that I load and write my content over, was the issue.
The template I used was generated in Adobe Illustrator. I had another one which was generated in Adobe Indesign - which worked.
When I pulled this template pdf into Indesign, and then exported it again (from Indesign) it suddenly worked.
I'm not sure exactly what caused this issue, but it must be some sort of encoding.

Reading memory stream

I'm trying to create an excel file and make that available as download. But i have some trouble with the download part.
I'm using EPPlus to create an xml file. I've got that working. But just local. I'm not sure how to force the download of the file.
This is my code:
public Stream GetXlsxDocument(IQueryable data)
{
const string sheetName = "Sheet1";
var localFile = new FileInfo(#"C:\test2.xlsx");
var file = new FileInfo("test2.xlsx");
// Used for local creation
//ExcelPackage p = new ExcelPackage();
MemoryStream stream = new MemoryStream();
using (ExcelPackage p = new ExcelPackage(stream))
{
p.Workbook.Worksheets.Add("Sheet1");
ExcelWorksheet ws = p.Workbook.Worksheets[1];
ws.Name = sheetName;
ws.Cells.Style.Font.Size = 11;
ws.Cells.Style.Font.Name = "Calibri";
ws.SetValue(1, 1, "aaa"); // Test data
// Used for local creation
//p.SaveAs(localFile);
p.SaveAs(stream);
}
return stream;
}
Like i said before. Creating the xlsx file locally on my C:\ disk works. But how can i force the download of the created xlsx file?
Right now its giving me an xlsx file of 0 bytes. I'd need to return a stream which isn't empty. Anyone any idea how i can do this..??
rewind it:
stream.Position = 0;
return stream;

Categories