修改为bar模板打印
This commit is contained in:
@@ -21,6 +21,7 @@ using Infrastructure.Converter;
|
||||
using NPOI.HPSF;
|
||||
using System.IO;
|
||||
using System.Web;
|
||||
using System.Resources;
|
||||
|
||||
//创建时间:2024-07-16
|
||||
namespace DOAN.Admin.WebApi.Controllers
|
||||
@@ -386,14 +387,21 @@ namespace DOAN.Admin.WebApi.Controllers
|
||||
//TODO 打印机打印工单
|
||||
[AllowAnonymous]
|
||||
[HttpGet("print")]
|
||||
public IActionResult ExportWorkorderPDF(string[] workorderArray)
|
||||
public async Task<IActionResult> ExportWorkorderPDF(string[] workorderArray,string? path)
|
||||
{
|
||||
if(workorderArray==null||workorderArray.Length<1)
|
||||
{
|
||||
throw new CustomException("workorderArray");
|
||||
}
|
||||
Task<(string, Stream)> conntext = _ProWorkorderService.ExportPDFByQuestPDFDemo(workorderArray);
|
||||
return File(conntext.Result.Item2, "application/pdf", HttpUtility.UrlEncode(conntext.Result.Item1));
|
||||
if (path==null)
|
||||
{
|
||||
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));
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,17 @@
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<NoWarn>1591</NoWarn>
|
||||
</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>
|
||||
<PackageReference Include="NPOI" Version="2.7.2" />
|
||||
<PackageReference Include="QuestPDF" Version="2024.12.1" />
|
||||
|
||||
@@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using DOAN.Model.MES.base_;
|
||||
using DOAN.Model.MES.base_.Dto;
|
||||
using SqlSugar;
|
||||
using Infrastructure;
|
||||
|
||||
|
||||
|
||||
@@ -64,5 +65,6 @@ namespace DOAN.Service.MES.product.IService
|
||||
int WorkOrderLog(string workorder, string log, string Operator);
|
||||
|
||||
Task<(string, Stream)> ExportPDFByQuestPDFDemo(string[] workorderArray);
|
||||
Task<CustomException> PrintTicketsByTemplate(string[] workorderArray,string path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,9 @@ using QuestPDF;
|
||||
using QuestPDF.Fluent;
|
||||
using QuestPDF.Helpers;
|
||||
using QuestPDF.Infrastructure;
|
||||
using BarTender;
|
||||
using MathNet.Numerics.RootFinding;
|
||||
using Infrastructure;
|
||||
|
||||
|
||||
namespace DOAN.Service.MES.product
|
||||
@@ -1303,7 +1306,7 @@ namespace DOAN.Service.MES.product
|
||||
|
||||
/// <summary>
|
||||
/// https://www.questpdf.com/
|
||||
/// </summary>
|
||||
/// </summary>要打印的工单号
|
||||
/// <param name="workorderArray"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<(string, Stream)> ExportPDFByQuestPDFDemo(string[] workorderArray)
|
||||
@@ -1381,10 +1384,56 @@ namespace DOAN.Service.MES.product
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user