generate and share a pdf from sqlite datatable of inventory count using skiasharp .net maui

huangapple go评论74阅读模式
英文:

generate and share a pdf from sqlite datatable of inventory count using skiasharp .net maui

问题

Here is the translated content from your provided code:

我想要设计一个使用SkiaSharp创建专业外观的PDF报告,显示物品数量,但是我没有找到合适的代码示例。
我想要使用SQLite表格来生成按物品类别分组的报告,包括类别总计。

代码在以下部分出现错误:

pdfDocument.ToArray())

> SKDocument不包含ToArray”的定义

这是我尝试的代码。
```csharp
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) 
    { 
    } 
}

If you need further assistance or have any specific questions about this code, feel free to ask.

英文:

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&lt;SKDocument&gt; GenerateInventoryItemCountPdfAsync(DateTime date, List&lt;Item&gt; 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(&quot;Arial&quot;, 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(&quot;Arial&quot;, 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 = &quot;N/A&quot;; 
} 
var countText = $&quot;{item.ItemFullName}:{condition}&quot;; 
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&lt;InventoryActivityReport&gt; GetAllInventoryActivityReports() 
{ 
InitAsync(); 
return conn.Table&lt;InventoryActivityReport&gt;().ToList(); 
} 
public List&lt;InventoryCount&gt; GetAllInventoryCounts() 
{ 
InitAsync(); 
return conn.Table&lt;InventoryCount&gt;().ToList(); 
} 
public InventoryActivityReport GetInventoryActivityReport(int id) 
{ 
InitAsync(); 
return conn.Table&lt;InventoryActivityReport&gt;().Where(i =&gt; i.Id == id).FirstOrDefault(); 
} 
public InventoryCount GetInventoryCount(int id) 
{ 
InitAsync(); 
return conn.Table&lt;InventoryCount&gt;().Where(i =&gt; i.Id == id).FirstOrDefault(); 
} 
public List&lt;InventoryCount&gt; GetInventoryCountsByInventoryActivityReportId(int id) 
{ 
InitAsync(); 
return conn.Table&lt;InventoryCount&gt;().Where(i =&gt; i.InventoryActivityReportID == id).ToList(); 
} 
public List&lt;InventoryCount&gt; GetInventoryCountsByInventoryReport(InventoryActivityReport inventoryActivityReport) 
{ 
InitAsync(); 
return conn.Table&lt;InventoryCount&gt;().Where(i =&gt; i.InventoryActivityReportID == inventoryActivityReport.Id).ToList(); 
} 
public async Task&lt;InventoryCount&gt; GetInventoryItemCountForDateAsync(DateTime date, int id) 
{ 
InitAsync(); 
return  conn.Table&lt;InventoryCount&gt;() 
.Where(ic =&gt; ic.CountedDate == date.Date &amp;&amp; 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&lt;InventoryCount&gt;().Where(ic =&gt; ic.CountedDate.Date == date.Date &amp;&amp; ic.ItemId == id).FirstOrDefault(); 
InventoryCount count = conn.Table&lt;InventoryCount&gt;() 
.Where(ic =&gt; ic.CountedDate == date.Date &amp;&amp; ic.ItemId == id) 
.FirstOrDefault(); 
if (count != null) 
{ 
return count; 
} 
else 
{ 
return new InventoryCount(); 
} 
} 
public async Task ShareInventoryItemCountPdfAsync(DateTime date, List&lt;Item&gt; 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(&quot;InventoryItemCount.pdf&quot;, &quot;application/pdf&quot;); 
await Share.RequestAsync(new ShareFileRequest { Title = &quot;Share Inventory Item Count PDF&quot;, File = pdfFile }).ConfigureAwait(false); 
} 
public void UpdateInventoryCount(InventoryCount inventoryCount) 
{ 
int result = 0; 
try 
{ 
InitAsync(); 
if (inventoryCount != null) 
{ 
result = conn.Update(inventoryCount); 
} 
} 
catch (Exception ex) 
{ 
} 
}

答案1

得分: 0

你已经将pdf文档放入了一个流中:

var stream = new MemoryStream();

返回 stream 而不是返回 pdfDocument:

public async Task<MemoryStream> ...
  var stream = new MemoryStream();
  using var document = SKDocument.CreatePdf(stream);
  ...
  return stream;

// usage
var pdfStream = await GenerateInventoryItemCountPdfAsync(...
英文:

You are already putting the pdfdocument into a stream:
var stream = new MemoryStream();.

Return stream instead of returning pdfDocument:

public async Task&lt;MemoryStream&gt;` ...
  var stream = new MemoryStream();
  using var document = SKDocument.CreatePdf(stream);
  ...
  return stream;

// usage
var pdfStream = await GenerateInventoryItemCountPdfAsync(...

huangapple
  • 本文由 发表于 2023年5月13日 19:36:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定