# 打印开发步骤

打印后端开发

打印前端开发

打印模板配置

打印脚本抽取



# 打印后端

## 1 打印Action 类

继承 AbstractPrintAction

重写 getPrintServiceModule()  - （返回模块名） 

重写 getPrintServiceName()     - （返回后端处理类全路径）

```
import nccloud.web.platform.print.AbstractPrintAction;

/**
 * 打印action demo代码
 */
1 usage
public class PrintDemoAction extends AbstractPrintAction {

    @Override
    public String getPrintServiceModule() {
        String module = "此处是你的模块编码";
        return module;
    }

    @Override
    public String getPrintServiceName() {
        //打印后端类全路径  比如：
        return "nc.impl.fbm.cztest.cztestmaster.AggDemoPrintServiceImpl";
    }
}

```



## 2 打印后端处理

继承 AbstractPrintService

```
import nc.ui.pub.print.IDataSource;
import nccloud.pubitf.platform.print.AbstractPrintService;
import nccloud.pubitf.platform.print.IPrintInfo;
import nccloud.pubitf.platform.print.vo.PrintInfo;

/**
 * 打印服务demo
 */
public class AggDemoPrintServiceImpl extends AbstractPrintService {
	
	@Override
    public IDataSource[] getDataSources(IPrintInfo info) {
        //打印参数
        PrintInfo printinfo = (PrintInfo) info;
        //打印单据的id
        String[] ids = printinfo.getIds();
        //构建数据源
        AggDemoPrintDataSource ds = new AggDemoPrintDataSource(ids);
        //返回数据源
       	return new IDataSource[] { ds };
       
    }
}

```



## 3 打印数据源

实现 IMetaDataDataSource，getMDObjects() 返回查询出来的数据 VO

```
import nc.bs.framework.common.NCLocator;
import nc.md.model.MetaDataException;
import nc.md.persist.framework.IMDPersistenceQueryService;
import nc.ui.pub.print.IMetaDataDataSource;
import nccloud.framework.core.exception.ExceptionUtils;

/**
 * 打印数据源
 */
public class AggDemoPrintDataSource implements IMetaDataDataSource {

	private static final long serialVersionUID = 1L;

	private String[] oids;

	public AggDemoPrintDataSource(String[] oids) {
		this.oids = oids;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object[] getMDObjects() {
		IMDPersistenceQueryService bs = NCLocator.getInstance().lookup(IMDPersistenceQueryService.class);
		//替换为单据的vo
		AggDemoVO[] aggvos = new AggDemoVO[] {};
		try {
			aggvos = (AggDemoVO[]) bs.queryBillOfVOByPKs(AggDemoVO.class, this.oids, false).toArray(new AggDemoVO[0]);
		} catch (MetaDataException e) {
			ExceptionUtils.wrapException(e);
		}
		return aggvos;
	}
	
	@Override
	public String[] getItemValuesByExpress(String itemExpress) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isNumber(String itemExpress) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String[] getDependentItemExpressByExpress(String itemExpress) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String[] getAllDataItemExpress() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String[] getAllDataItemNames() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getModuleName() {
		// TODO Auto-generated method stub
		return null;
	}



}

```

## 4 配置打印动作映射文件

鉴权文件一般存放路径为:

client/yyconfig/modules/模块编码/组件编码/实体编码/config/action/实体编码_action.xml

```xml
<?xml version="1.0" encoding="UTF-8"?>
<actions>
    <action>
        <!-- 比如nresa.treegrid.SaveTreegridMasterVOAction -->
        <name>模块编码.组件编码.action类名</name>
        <label>打印</label>
        <clazz>PrintDemoAction的全路径</clazz>
    </action>
    <action>
        <!-- 其他 action 配置 -->
    </action>
</actions>
```

## 5 配置打印鉴权文件

需要授权的应用编码，写在appcode中， *表示所有节点都有权限

鉴权文件一般存放路径为:

client/yyconfig/modules/模块编码/组件编码/实体编码/config/authorize/实体编码_authorize.xml

```xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<authorizes>
    <authorize>
        <appcode>*</appcode>
        <actions>
             <!-- 比如nresa.treegrid.SaveTreegridMasterVOAction -->
            <action>模块编码.组件编码.action类名</action>
        </actions>
    </authorize>
</authorizes>
```



# 打印前端

```js


import React, { Component } from 'react';
import ReactDOM from 'react-dom';

import {createPage,  high, print, output} from 'nc-lightapp-front';
const {PrintOutput} = high;

import ExcelOutput from 'uap/common/components/ExcelOutput';  // 导出组件

const URLS = {
    printUrl: '/nccloud/demoModule/demo/PrintDemoAction.do',         //打印 请求路径
};

class ApplicationPage extends Component {

    constructor(props) {
        super(props);
        this.state = this.createState();
    }

	/**
	 * 创建state
	 */
    createState = () => {
        let state = {
			print:{
				ref:'printOutput',
				url:URLS.printUrl,
				data:{
					funcode: '此处输入你的应用编码',				//appcode
					oids: [],								 	//数据的pks
					nodekey: undefined,						    //默认输出模板的key
					outputType: undefined						//如果是输出的话需要设为output,不是输出就置空
				}
			}
        }
        return state;   //返回state解构
    }

	/**
	 * 功能介绍 ： 打印 流程组装
	 * 
	 * 在打印按钮点击处调用该方法，里面的参数需要具体根据你的界面来获取需要传递数据
	 */
    onPrint = () => {

		//设置打印默认模板
        this.state.print.data.nodekey = '打印模板的key'
		//需要打印的pks
        this.state.print.data.oids = '需要打印的pks';
        //输出类型，打印的输出类型为空
        this.state.print.data.outputType = 'print'
		//更新state
        this.setState(this.state,() => {
			//调用平台的打印api
            print('pdf', URLS.printUrl, this.state.print.data);
        })

    }

	/**
	 * 功能介绍 ： 输出 流程组装
	 * 
	 * 在输出按钮点击处调用该方法，里面的参数需要具体根据你的界面来获取需要传递数据
	 */
    onOutput = () => {

    	//构建选中的数据
    	let { pks } = this.getCheckedDatas();
    	//打印数据模板key
        this.state.print.data.nodekey = '打印模板的'
		//需要输出的pks
        this.state.print.data.oids = '需要打印的pks';
		//输出类型
		this.state.print.data.outputType = 'output'
		//更新state
        this.setState(this.state,() => {
			//打开输出弹窗
            output({url : this.state.print.url,data : this.state.print.data});
        })

    }

	/**
	 * 渲染
	 */
    render() {
		
        const renderPrintOutput = () => {
			//参数解构
			let { print } = this.state;
            return (
                <PrintOutput {...print}/>
            );
        }

		return  <div>
                    {renderPrintOutput() /** 渲染打印输出*/}
                </div>

    }
}



```

# 打印模板配置

## 1 模板配置

位置: 动态建模平台 -> 开发配置 -> 模板管理 -> 输出模板初始化   

进行模板创建



## 2 分配打印模板

位置： 动态建模平台 -> 开发配置 -> 模板管理 -> 默认输出模板设置

处进行模板分配



# 打印脚本抽取

需要抽取的表：

打印模板： pub_print_template

默认模板： pub_systemplate_base

item.xml配置文件示例

```xml
<?xml version="1.0" encoding="UTF-8"?>
<items>
    <item>
        <itemKey>pub_print_template</itemKey>
        <itemName>打印模板</itemName>
        <itemRule>pub_print_template</itemRule>
        <sysField></sysField>
        <corpField></corpField>
        <grpField></grpField>
        <fixedWhere>
            dr=0 and appcode in ('你的应用编码')
        </fixedWhere>
    </item>
    <item>
        <itemKey>pub_systemplate_base</itemKey>
        <itemName>默认模板(打印模板输出)</itemName>
        <itemRule>pub_systemplate_base</itemRule>
        <sysField></sysField>
        <corpField></corpField>
        <grpField></grpField>
        <fixedWhere>
            dr=0 and appcode = '你的应用编码' and tempstyle = 3
        </fixedWhere>
    </item>
</items>
```

