I want to design a professional looking pdf report of item count using skiaSharp but I have failed to get the proper code example of it. I want to use an sqlite table, to generate the report grouped into item categories with category totals.
The code returns error on:
pdfDocument.ToArray())
SKDocument doesn't contain a definition for 'ToArray'
Here is the code I tried tried.
public async Task<SKDocument> GenerateInventoryItemCountPdfAsync(DateTime date, List<Item> items, string pdfLogoPath, string pdfTitle)
{
var stream = new MemoryStream();
using var document = SKDocument.CreatePdf(stream);
var pageSize = new SKSize(595, 842);
using var canvas = document.BeginPage((int)pageSize.Width, (int)pageSize.Height);
using (var logoBitmap = SKBitmap.Decode(pdfLogoPath))
{
//var logoRect = SKRect.Create(pageSize.Width / 2 - logoBitmap.Width / 2, 50, logoBitmap.Width, logoBitmap.Height);
//canvas.DrawBitmap(logoBitmap, logoRect);
var titlePaint = new SKPaint
{
Typeface = SKTypeface.FromFamilyName("Arial", SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright),
TextSize = 24,
IsAntialias = true,
Color = SKColors.Black
};
var titleBounds = new SKRect();
titlePaint.MeasureText(pdfTitle, ref titleBounds);
canvas.DrawText(pdfTitle, pageSize.Width / 2 - titleBounds.Width / 2, 150, titlePaint);
var countPaint = new SKPaint
{
Typeface = SKTypeface.FromFamilyName("Arial", SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright),
TextSize = 18,
IsAntialias = true,
Color = SKColors.Black
};
var y = 200;
foreach (var item in items)
{
var itemCount = await GetInventoryItemCountForDateAsync(date, item.Id);//.ConfigureAwait(false);
string condition;
if (itemCount != null)
{
condition = itemCount.InventoryItemUnitQty.ToString();
}
else
{
condition = "N/A";
}
var countText = $"{item.ItemFullName}:{condition}";
var countBounds = new SKRect();
countPaint.MeasureText(countText, ref countBounds);
canvas.DrawText(countText, pageSize.Width / 2 - countBounds.Width / 2, y, countPaint);
y += 30;
}
document.EndPage();
document.Close();
return document;
}
}
public List<InventoryActivityReport> GetAllInventoryActivityReports()
{
InitAsync();
return conn.Table<InventoryActivityReport>().ToList();
}
public List<InventoryCount> GetAllInventoryCounts()
{
InitAsync();
return conn.Table<InventoryCount>().ToList();
}
public InventoryActivityReport GetInventoryActivityReport(int id)
{
InitAsync();
return conn.Table<InventoryActivityReport>().Where(i => i.Id == id).FirstOrDefault();
}
public InventoryCount GetInventoryCount(int id)
{
InitAsync();
return conn.Table<InventoryCount>().Where(i => i.Id == id).FirstOrDefault();
}
public List<InventoryCount> GetInventoryCountsByInventoryActivityReportId(int id)
{
InitAsync();
return conn.Table<InventoryCount>().Where(i => i.InventoryActivityReportID == id).ToList();
}
public List<InventoryCount> GetInventoryCountsByInventoryReport(InventoryActivityReport inventoryActivityReport)
{
InitAsync();
return conn.Table<InventoryCount>().Where(i => i.InventoryActivityReportID == inventoryActivityReport.Id).ToList();
}
public async Task<InventoryCount> GetInventoryItemCountForDateAsync(DateTime date, int id)
{
InitAsync();
return conn.Table<InventoryCount>()
.Where(ic => ic.CountedDate == date.Date && ic.ItemId == id)
.FirstOrDefault();
//if (count != null)
//{
// return count;
//}
//else
//{
// return new InventoryCount();
//}
}
public InventoryCount GetInventoryItemCountForDate(DateTime date, int id)
{
InitAsync();
//InventoryCount count = conn.Table<InventoryCount>().Where(ic => ic.CountedDate.Date == date.Date && ic.ItemId == id).FirstOrDefault();
InventoryCount count = conn.Table<InventoryCount>()
.Where(ic => ic.CountedDate == date.Date && ic.ItemId == id)
.FirstOrDefault();
if (count != null)
{
return count;
}
else
{
return new InventoryCount();
}
}
public async Task ShareInventoryItemCountPdfAsync(DateTime date, List<Item> items, string pdfLogoPath, string pdfTitle)
{
var pdfDocument = await GenerateInventoryItemCountPdfAsync(date, items, pdfLogoPath, pdfTitle).ConfigureAwait(false);
// var pdfStream = new MemoryStream(pdfDocument.ToArray());
var pdfFile = new ShareFile("InventoryItemCount.pdf", "application/pdf");
await Share.RequestAsync(new ShareFileRequest { Title = "Share Inventory Item Count PDF", File = pdfFile }).ConfigureAwait(false);
}
public void UpdateInventoryCount(InventoryCount inventoryCount)
{
int result = 0;
try
{
InitAsync();
if (inventoryCount != null)
{
result = conn.Update(inventoryCount);
}
}
catch (Exception ex)
{
}
}
You are already putting the pdfdocument into a stream:
var stream = new MemoryStream();
.
Return stream
instead of returning pdfDocument:
public async Task<MemoryStream>` ...
var stream = new MemoryStream();
using var document = SKDocument.CreatePdf(stream);
...
return stream;
// usage
var pdfStream = await GenerateInventoryItemCountPdfAsync(...