修改为bar模板打印

This commit is contained in:
小魔仙
2025-04-10 13:42:12 +08:00
parent 8fd6ae9efb
commit 6c0087e871
4 changed files with 79 additions and 9 deletions

View File

@@ -21,6 +21,7 @@ using Infrastructure.Converter;
using NPOI.HPSF; using NPOI.HPSF;
using System.IO; using System.IO;
using System.Web; using System.Web;
using System.Resources;
//创建时间2024-07-16 //创建时间2024-07-16
namespace DOAN.Admin.WebApi.Controllers namespace DOAN.Admin.WebApi.Controllers
@@ -386,14 +387,21 @@ namespace DOAN.Admin.WebApi.Controllers
//TODO 打印机打印工单 //TODO 打印机打印工单
[AllowAnonymous] [AllowAnonymous]
[HttpGet("print")] [HttpGet("print")]
public IActionResult ExportWorkorderPDF(string[] workorderArray) public async Task<IActionResult> ExportWorkorderPDF(string[] workorderArray,string? path)
{ {
if(workorderArray==null||workorderArray.Length<1) if(workorderArray==null||workorderArray.Length<1)
{ {
throw new CustomException("workorderArray"); throw new CustomException("workorderArray");
} }
Task<(string, Stream)> conntext = _ProWorkorderService.ExportPDFByQuestPDFDemo(workorderArray); if (path==null)
return File(conntext.Result.Item2, "application/pdf", HttpUtility.UrlEncode(conntext.Result.Item1)); {
path ="./Resources/gxassembly_production_label.btw";
}
//Task<(string, Stream)> conntext = _ProWorkorderService.ExportPDFByQuestPDFDemo(workorderArray);
var exception = await _ProWorkorderService.PrintTicketsByTemplate(workorderArray,path);
return (IActionResult)exception;
//return File(conntext.Result.Item2, "application/pdf", HttpUtility.UrlEncode(conntext.Result.Item1));
} }

View File

@@ -8,6 +8,17 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<NoWarn>1591</NoWarn> <NoWarn>1591</NoWarn>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<COMReference Include="BarTender">
<WrapperTool>tlbimp</WrapperTool>
<VersionMinor>1</VersionMinor>
<VersionMajor>10</VersionMajor>
<Guid>d58562c1-e51b-11cf-8941-00a024a9083f</Guid>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
<EmbedInteropTypes>true</EmbedInteropTypes>
</COMReference>
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="NPOI" Version="2.7.2" /> <PackageReference Include="NPOI" Version="2.7.2" />
<PackageReference Include="QuestPDF" Version="2024.12.1" /> <PackageReference Include="QuestPDF" Version="2024.12.1" />

View File

@@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Mvc;
using DOAN.Model.MES.base_; using DOAN.Model.MES.base_;
using DOAN.Model.MES.base_.Dto; using DOAN.Model.MES.base_.Dto;
using SqlSugar; using SqlSugar;
using Infrastructure;
@@ -64,5 +65,6 @@ namespace DOAN.Service.MES.product.IService
int WorkOrderLog(string workorder, string log, string Operator); int WorkOrderLog(string workorder, string log, string Operator);
Task<(string, Stream)> ExportPDFByQuestPDFDemo(string[] workorderArray); Task<(string, Stream)> ExportPDFByQuestPDFDemo(string[] workorderArray);
Task<CustomException> PrintTicketsByTemplate(string[] workorderArray,string path);
} }
} }

View File

@@ -32,6 +32,9 @@ using QuestPDF;
using QuestPDF.Fluent; using QuestPDF.Fluent;
using QuestPDF.Helpers; using QuestPDF.Helpers;
using QuestPDF.Infrastructure; using QuestPDF.Infrastructure;
using BarTender;
using MathNet.Numerics.RootFinding;
using Infrastructure;
namespace DOAN.Service.MES.product namespace DOAN.Service.MES.product
@@ -1303,7 +1306,7 @@ namespace DOAN.Service.MES.product
/// <summary> /// <summary>
/// https://www.questpdf.com/ /// https://www.questpdf.com/
/// </summary> /// </summary>要打印的工单号
/// <param name="workorderArray"></param> /// <param name="workorderArray"></param>
/// <returns></returns> /// <returns></returns>
public async Task<(string, Stream)> ExportPDFByQuestPDFDemo(string[] workorderArray) public async Task<(string, Stream)> ExportPDFByQuestPDFDemo(string[] workorderArray)
@@ -1381,10 +1384,56 @@ namespace DOAN.Service.MES.product
return new(fileName, ms); return new(fileName, ms);
} }
/// <summary>
/// 根据模板打印工单信息
/// </summary>
/// <param name="workorderArray"></param>
/// <param name="path">模板路径</param>
/// <returns></returns>
public async Task<CustomException> PrintTicketsByTemplate(string[] workorderArray,string path)
{
var dataList = await Context.Queryable<ProWorkorder>().Where(p => workorderArray.Contains(p.Workorder)).ToListAsync();
if (!dataList.Any())
{
return new CustomException(204, "未找到匹配的工单数据");
}
Application bartenderApp = null;
Format bartenderFormat = null;
try
{
bartenderApp = new Application { Visible = false };
bartenderFormat = bartenderApp.Formats.Open(path);
// 4. 遍历数据并打印
foreach (var data in dataList)
{
bartenderFormat.SetNamedSubStringValue("Workorder", data.Workorder);
bartenderFormat.SetNamedSubStringValue("StoveCode", data.StoveCode);
bartenderFormat.SetNamedSubStringValue("PlanNum", data.PlanNum.ToString());
await Task.Delay(500);
bartenderFormat.PrintOut(false, false); // 静默打印
}
return new CustomException(200, "标签打印成功");
}
catch (Exception ex)
{
// 5. 错误处理(记录日志)
Console.WriteLine($"打印标签时出错: {ex.Message}");
return new CustomException(200, $"打印标签失败: {ex.Message}");
}
finally
{
// 6. 确保资源释放(逆序关闭)
if (bartenderFormat != null)
{
bartenderFormat.Close(BtSaveOptions.btDoNotSaveChanges);
}
if (bartenderApp != null)
{
bartenderApp.Quit(BtSaveOptions.btDoNotSaveChanges);
}
}
}
} }
} }