套打.二开案例.使用导出PDF文件进行预览
【场景】使用导出PDF文件进行预览
部分客户不想用前端的预览打印界面,或者需要保持多端同一的时候,可以通过生成一份PDF进行预览打印
缺点:使用PDF文件的打印不受控制,或者自行实现类似于pdf.js的预览并自行屏蔽打印逻辑
【案例】二开菜单,菜单生成PDF文件,并进行自定义预览(本文案例是本地文件服务预览)
<0>在应用服务器生成对应的套打导出PDF文件
[单据套打:创建目标单据视图进行套打导出](https://vip.kingdee.com/link/s/lpmkI)
<1>生成pdf文件后,使用预览方案打开pdf文件
本地文件服务预览方案(需要操作用户电脑安装星空本地服务)
[文件服务.二开案例.使用本地服务预览自定义文件](https://vip.kingdee.com/link/s/lgMV5)
其他预览方案自行评估
![image.webp](/download/0100ade32d17ab2b43249d9272aedd8ca9ba.webp)
```csharp
using Kingdee.BOS;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Import;
using Kingdee.BOS.Core.List.PlugIn;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.NotePrint;
using Kingdee.BOS.JSON;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.ServiceHelper.FileServer;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.Web;
namespace Fk.PlugInSample.FormPlugIn
{
[Kingdee.BOS.Util.HotUpdate]
[System.ComponentModel.Description("套打导出PDF并通过本地文件服务预览文件")]
public class ExportPreviewPdf : AbstractListPlugIn
{
public override void BarItemClick(BarItemClickEventArgs e)
{
if (e.BarItemKey.EqualsIgnoreCase("tb_ExportAndPreview"))
{
string formId = "PUR_Requisition";
string billId = "314323";
string templateId = "734cffd8-26ef-4131-b0c2-1d26b29d90c0";
var exportFileInfo = GetExportFile(formId, billId, templateId);
PreviewPdf(exportFileInfo);
}
}
#region 生成套打导出文件
/// <summary>
/// 生成导出文件
/// </summary>
/// <param name="formId"></param>
/// <param name="billId"></param>
/// <param name="templateId"></param>
/// <returns></returns>
private ExportFileInfo GetExportFile(string formId, string billId, string templateId)
{
return ExportTargetBill(this.Context, formId, billId, templateId);
}
/// <summary>
/// 套打导出指定单据
/// </summary>
/// <param name="formId"></param>
/// <param name="billId"></param>
/// <param name="templateId"></param>
/// <returns></returns>
private static ExportFileInfo ExportTargetBill(Context ctx, string formId, string billId, string templateId)
{
IDynamicFormView dynamicFormView = CreateBillView(ctx, formId, billId); //此行为代码模拟打开凭证界面
if (dynamicFormView == null)
return null;
IImportView billView = dynamicFormView as IImportView;
if (billView != null)
{
billView.AddViewSession();
}
try
{
return ExportTargetBillWithDynamic(dynamicFormView, formId, billId, templateId);
}
finally
{
if (billView != null)
{
billView.RemoveViewSession();
}
dynamicFormView.Close();
}
}
private static ExportFileInfo ExportTargetBillWithDynamic(IDynamicFormView dynamicFormView, string formId, string billId, string templateId)
{
IDynamicFormViewService viewService = dynamicFormView as IDynamicFormViewService;
if (viewService == null)
return null;
List<string> billIds = new List<string>() { billId };
List<string> templateIds = new List<string>() { templateId };
PrintExportInfo pExInfo = new PrintExportInfo();
pExInfo.PageId = dynamicFormView.PageId;
pExInfo.FormId = formId;
pExInfo.BillIds = billIds; //单据内码
pExInfo.TemplateIds = templateIds; //套打模板ID
pExInfo.FileType = ExportFileType.PDF; //文件格式
pExInfo.ExportType = ExportType.BillTempId; //导出格式
string fileName = Guid.NewGuid().ToString() + ".PDF";
string temppath = PathUtils.GetPhysicalPath("TempfilePath", fileName);
pExInfo.FilePath = temppath; //文件输出路径
//指定动态文件目录和动态文件名
pExInfo.ExportDynamicDirectory = pExInfo.Id;
pExInfo.ExportDynamicFileName = "{FBillNo}";
viewService.ExportNotePrint(pExInfo);
if (pExInfo.ExportFileInfos == null || pExInfo.ExportFileInfos.Count <= 0)
return null;
return pExInfo.ExportFileInfos[0];
}
/// <summary>
/// 构建表单打开参数
/// </summary>
/// <param name="ctx"></param>
/// <param name="metadata"></param>
/// <param name="pkValue"></param>
/// <returns></returns>
private static BillOpenParameter CreateOpenParameter(Context ctx, FormMetadata metadata, string pkValue)
{
var form = metadata.BusinessInfo.GetForm();
BillOpenParameter openPara = new BillOpenParameter(form.Id, metadata.GetLayoutInfo().Id);
openPara.Context = ctx;
openPara.PageId = Guid.NewGuid().ToString();
openPara.Status = OperationStatus.VIEW;
openPara.CreateFrom = CreateFrom.Default;
openPara.DefaultBillTypeId = string.Empty;
openPara.PkValue = pkValue;
openPara.FormMetaData = metadata;
openPara.SetCustomParameter(Kingdee.BOS.Core.FormConst.PlugIns, form.CreateFormPlugIns());
openPara.ServiceName = form.FormServiceName;
return openPara;
}
/// <summary>
/// 创建视图
/// </summary>
/// <param name="ctx"></param>
/// <param name="formId"></param>
/// <param name="pkValue"></param>
/// <returns></returns>
private static IDynamicFormView CreateBillView(Context ctx, string formId, string pkValue)
{
FormMetadata metadata = MetaDataServiceHelper.Load(ctx, formId) as FormMetadata;
if (metadata == null)
return null;
var openParameter = CreateOpenParameter(ctx, metadata, pkValue);
var provider = metadata.BusinessInfo.GetForm().GetFormServiceProvider();
string importViewClass = "Kingdee.BOS.Web.Import.ImportBillView,Kingdee.BOS.Web";
Type type = Type.GetType(importViewClass);
IDynamicFormViewService billView = (IDynamicFormViewService)Activator.CreateInstance(type);
billView.Initialize(openParameter, provider);
billView.LoadData();
return (IDynamicFormView)billView;
}
#endregion
#region 本地文件服务预览
public void PreviewPdf(ExportFileInfo exportInfo)
{
if (exportInfo == null)
return;
//文件名
var fileName = exportInfo.FileName;
//高版本的下载路径
var downloadUrl = string.Format("{0}{1}",
FileServerHelper.GetAppSiteOuterNetUrl(this.Context, HttpContext.Current.Request), GetDownloadUrl(exportInfo.FilePath));
//低版本自行拼接下载路径,目前直接使用文件虚拟路径映射返回
//var downloadUrl = string.Format("{0}{1}",
// FileServerHelper.GetAppSiteOuterNetUrl(this.Context, HttpContext.Current.Request), exportInfo.FileServicePath);
JSONObject webobj = new JSONObject();
webobj["action"] = "OpenRes";//使用客户端本地程序直接打开文件资源,如pdf默认谷歌浏览器打开,word默认使用office打开
webobj["filepath"] = downloadUrl;//文件下载url
webobj["fileid"] = Guid.NewGuid().ToString();//用作本地服务缓存文件,如果每次都需要下载最新的就使用guid
webobj["targetfilename"] = HttpUtility.UrlEncode(fileName);//文件名
webobj["pageid"] = this.View.PageId;
this.View.AddAction("OpenClientLocalResource", webobj);
this.View.SendDynamicFormAction(this.View);
this.View.ShowMessage("附件正在加载中..");
CacheUtil.SetCache(this.Context.GetAreaCacheKey(), "OpenClientLocalResourceCall", this.View.PageId, this.View);
}
/// <summary>
/// 将文件物理路径转换成下载链接
/// </summary>
/// <param name="pdfFilePath"></param>
/// <returns></returns>
private string GetDownloadUrl(string pdfFilePath)
{
///支持版本 24.03
string downloadUrl = Kingdee.BOS.ServiceHelper.FileServer.FileServerHelper.GetDownloadUrl(this.Context,
Kingdee.BOS.Core.FileServer.DownloadIdType.PhysicalPath, pdfFilePath);
return downloadUrl;
}
#endregion
}
}
```
【效果】
![20240529 2048.webp](/download/0100e9978f45c5a94db9a5f1368d91c94d7c.webp)
套打.二开案例.使用导出PDF文件进行预览
【场景】使用导出PDF文件进行预览部分客户不想用前端的预览打印界面,或者需要保持多端同一的时候,可以通过生成一份PDF进行预览打印缺点:...
点击下载文档
本文2024-09-23 04:11:52发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-163752.html
您需要登录后才可以发表评论, 登录登录 或者 注册
最新文档
热门文章