
【场景】使用导出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)
其他预览方案自行评估

```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;