金蝶云自定义接口-套打导出PDF文件
业务需求:开发移动端显示和下载套打文件
tempfilePath 为临时存储目录,开启执行计划定时清理接口生成临时文件
using Kingdee.BOS;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.NotePrint;
using Kingdee.BOS.JSON;
using Kingdee.BOS.ServiceFacade.KDServiceFx;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.WebApi.ServicesStub;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Kingdee.BOS.Core;
using Kingdee.BOS.Util;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.List;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Web.Core;
using System.IO;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.FormElement;
using Kingdee.BOS.Core.CommonFilter;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.ListFilter;
using Kingdee.BOS.Web.List;
using Kingdee.BOS.Core.List.PlugIn;
namespace KingDee.api.ServicesStub
{
public class CustomBusinessService : AbstractWebApiBusinessService
{
public RETURNROOT ExecuteService(ROOT parameter)
{
RETURNROOT _RETURNROOT = new RETURNROOT();
try
{
Context ctx = this.KDContext.Session.AppContext;
IDynamicFormView view = CreateView(ctx, parameter.FormId);
CommonSession.GetCurrent(ctx.UserToken).SessionManager.GetOrAdd(view.PageId, view.GetType().Name, view);
//单据内码与套打模板标识一一对应
List<string> billIds = new List<string> { parameter.FID }; //单据内码
List<string> tempIds = new List<string> { parameter.FPrintID };//套打模板标识
PrintExportInfo pExInfo = new PrintExportInfo();
pExInfo.PageId = view.PageId;
pExInfo.FormId = view.BillBusinessInfo.GetForm().Id;
pExInfo.BillIds = billIds;//单据内码
pExInfo.TemplateIds = tempIds;//套打模板ID
pExInfo.FileType = ExportFileType.PDF;//文件格式
pExInfo.ExportType = ExportType.ByPage;//导出格式
const string FileUploadServicesDir = "tempfilePath";
string physicalPath = PathUtils.GetPhysicalPath(FileUploadServicesDir, parameter.FileName + "_" + Guid.NewGuid().ToString() + ".PDF");
pExInfo.FilePath = physicalPath;//文件输出路径
Export(pExInfo, view);
_RETURNROOT.Success = true;
_RETURNROOT.str64 = FileToBase64Str(physicalPath);
}
catch(Exception e)
{
_RETURNROOT.Success = false;
_RETURNROOT.Exception =e.ToString();
}
return _RETURNROOT;
}
public string FileToBase64Str(string filePath)
{
string base64Str = string.Empty;
try
{
using (FileStream filestream = new FileStream(filePath, FileMode.Open))
{
byte[] bt = new byte[filestream.Length];
//调用read读取方法
filestream.Read(bt, 0, bt.Length);
base64Str = Convert.ToBase64String(bt);
filestream.Close();
}
return base64Str;
}
catch (Exception ex)
{
return base64Str;
}
}
private void Export(PrintExportInfo pExInfo, IDynamicFormView view)
{
if (view is IListViewService)
{
ListView list = (ListView)view;
list.ExportNotePrint(pExInfo);
}
}
#region 创建listview
public IDynamicFormView CreateView(Context ctx, string formId)
{
FormMetadata _metadata = (FormMetadata)Kingdee.BOS.ServiceHelper.MetaDataServiceHelper.Load(ctx, formId);
var OpenParameter = CreateOpenParameter(OperationStatus.VIEW, ctx, formId, _metadata);
var Provider = GetListServiceProvider(OpenParameter);
string importViewClass = "Kingdee.BOS.Web.List.ListView,Kingdee.BOS.Web";
Type type = Type.GetType(importViewClass);
IListViewService View = (IListViewService)Activator.CreateInstance(type);
((IListViewService)View).Initialize(OpenParameter, Provider);
((IListViewService)View).LoadData();
return (IDynamicFormView)View;
}
private ListOpenParameter CreateOpenParameter(OperationStatus status, Context ctx, string formId, FormMetadata _metadata)
{
ListOpenParameter openPara = new ListOpenParameter(formId, _metadata.GetLayoutInfo().Id);
Form form = _metadata.BusinessInfo.GetForm();
openPara = new ListOpenParameter(formId, string.Empty);
openPara.Context = ctx;
openPara.ServiceName = form.FormServiceName;
openPara.PageId = Guid.NewGuid().ToString();
// 单据
openPara.FormMetaData = _metadata;
openPara.LayoutId = _metadata.GetLayoutInfo().Id;
openPara.ListFormMetaData = (FormMetadata)FormMetaDataCache.GetCachedFormMetaData(ctx, FormIdConst.BOS_List);
// 操作相关参数
openPara.SetCustomParameter(FormConst.PlugIns, form.CreateListPlugIns());
openPara.SetCustomParameter("filterschemeid", "");
openPara.SetCustomParameter("listfilterparameter", new ListRegularFilterParameter());
// 修改主业务组织无须用户确认
openPara.SetCustomParameter("ShowConfirmDialogWhenChangeOrg", false);
openPara.SetCustomParameter("SessionManager", CommonSession.GetCurrent(ctx.UserToken).SessionManager);
return openPara;
}
private IResourceServiceProvider GetListServiceProvider(DynamicFormOpenParameter param)
{
FormServiceProvider provider = new FormServiceProvider();
provider.Add(typeof(IDynamicFormView), CreateListView(param));
provider.Add(typeof(DynamicFormViewPlugInProxy), new ListViewPlugInProxy());
provider.Add(typeof(DynamicFormModelPlugInProxy), new ListModelPlugInProxy());
provider.Add(typeof(IDynamicFormModelService), GetListModel(param));
provider.Add(typeof(IListFilterModelService), GetListFilterModel());
var type = TypesContainer.GetOrRegister("Kingdee.BOS.Business.DynamicForm.DefaultValue.DefaultValueCalculator,Kingdee.BOS.Business.DynamicForm");
provider.Add(typeof(IDefaultValueCalculator), Activator.CreateInstance(type));
// 注册IDBModelService
type = TypesContainer.GetOrRegister("Kingdee.BOS.Business.DynamicForm.DBModel.DBModelService,Kingdee.BOS.Business.DynamicForm");
provider.Add(typeof(IDBModelService), Activator.CreateInstance(type));
return provider;
}
/// 获取视图
private IDynamicFormView CreateListView(DynamicFormOpenParameter param)
{
Form form = param.FormMetaData.BusinessInfo.GetForm();
if (form.FormGroups != null && form.FormGroups.Count > 0)
{
return new TreeListView();
}
else
{
return new ListView();
}
}
/// 获取视图模型
private IDynamicFormModelService GetListModel(DynamicFormOpenParameter param)
{
Form form = param.FormMetaData.BusinessInfo.GetForm();
if (form.FormGroups != null && form.FormGroups.Count > 0)
{
var type = TypesContainer.GetOrRegister("Kingdee.BOS.Model.List.TreeListModel,Kingdee.BOS.Model");
return (IDynamicFormModelService)Activator.CreateInstance(type);
}
else
{
var type = TypesContainer.GetOrRegister("Kingdee.BOS.Model.List.ListModel,Kingdee.BOS.Model");
return (IDynamicFormModelService)Activator.CreateInstance(type);
}
}
/// 创建过滤条件模型
private IListFilterModelService GetListFilterModel()
{
Type type = TypesContainer.GetOrRegister("Kingdee.BOS.Model.ListFilter.ListFilterModel,Kingdee.BOS.Model");
return (IListFilterModelService)Activator.CreateInstance(type);
}
#endregion
public CustomBusinessService(KDServiceContext context): base(context)
{
}
}
public class ROOT
{
public string FormId {get;set;}
public string FID {get;set;}
public string FPrintID {get;set;}
public string FileName { get; set; }
}
public class RETURNROOT
{
public bool Success { get; set; }
public string str64 { get; set; }
public string Exception { get; set; }
}
}