// 打印相关的逻辑,点击按钮 调用handlePrint()
@Bind()
handlePrint() {
const { dispatch } = this.props;
const { selectedRowKeys } = this.state;
// selectedRowKeys是需要打印的数据的标识,将需要打印的数据标示传递给后端,根据后端要求传递,
dispatch({
type: 'qualityResult/fetchListPrint',
payload: selectedRowKeys,
}).then((res) => {
// 后端返回已经描绘好的发票或者资源数据,一般是pdf格式或其他格式,需要前端转码展示
if (res) {
const file = new Blob([res], { type: 'application/pdf' });
const fileURL = URL.createObjectURL(file);
const printWindow = window.open(fileURL);
// printWindow.print();是浏览器自带打印方法
printWindow.print();
}
});
}
/**
* 质检结果查询-批量打印
* @param {object} params
*/
// 注意接口附加上 responseType: 'blob',
export async function fetchListPrint(params) {
return request(`${SRM_SQAM}/v1/${organizationId}/incoming-inspections/batch/print`, {
method: 'POST',
responseType: 'blob',
body: params,
});
}
或者将后端返回的条形码文件转码在新窗口打开
seeCode() {
const { dispatch, detailHeader } = this.props;
const { itemCode } = detailHeader;
console.log(detailHeader, itemCode);
dispatch({
type: 'incomingInspectionMaintain/barCode',
payload: {
itemCode,
},
}).then((res) => {
if (res) {
const file = new Blob([res], { type: 'application/PNG' });
// const fileURL = URL.createObjectURL(file);
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onloadend = function () {
var base64data = reader.result;
const img = new Image();
img.src = base64data;
img.width = 300;
img.style.position = 'absolute';
img.style.top = '0';
img.style.left = '0';
img.style.right = '0';
img.style.bottom = '0';
img.style.margin = ' auto';
const newWin = window.open('', '_blank');
newWin.document.write(img.outerHTML);
newWin.document.title = '条形码';
};
}
});
}```