• ActiveReportsJs 账票印刷


    参考资料

    1. 官方文档

    一. HTML部分

    • 在页面上添加了Loading效果,账票印刷开始时显示Loading效果,印刷结束后隐藏Loading效果。
    • ar-js-core.js是核心文件
    • ar-js-pdf.js用来印刷PDF
    • ar-js-xlsx.js用来印刷EXCEL
    • ar-js-locales.js用来设置语言
    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style>
            @keyframes donut-spin {
                0% {
                    transform: rotate(0deg);
                }
                100% {
                    transform: rotate(360deg);
                }
            }
            .donut-container {
                display: flex;
                justify-content: center;
                align-items: center;
                margin-top: 20%;
            }
            .donut {
                display: inline-block;
                border: 4px solid rgba(0, 0, 0, 0.1);
                border-left-color: #7983ff;
                border-radius: 50%;
                width: 30px;
                height: 30px;
                animation: donut-spin 1.2s linear infinite;
            }
            .hidden {
                display: none;
            }
        style>
        <title>Documenttitle>
    head>
    <body>
        
        <div id="container">
            <button id="pdf">打印PDFbutton>
            <hr>
            <button id="excel">打印Excelbutton>
        div>
    
        
        <div class="donut-container hidden" id="loading">
            <div class="donut">div>
        div>
    
    body>
    
    <script src="https://cdn.grapecity.com/activereportsjs/4.0.0/dist/ar-js-core.js">script>
    
    <script src="https://cdn.grapecity.com/activereportsjs/4.0.0/dist/ar-js-pdf.js">script>
    
    <script src="https://cdn.grapecity.com/activereportsjs/4.0.0/dist/ar-js-xlsx.js">script>
    
    <script src="https://cdn.grapecity.com/activereportsjs/4.0.0/dist/locales/ar-js-locales.js">script>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    二. 账票定义体

    ⏹账票定义体本质上就是通过ActiveReportsJS Designer制作的特殊json文件,该json文件用来描述账票的样式,和后台提供的json数据进行绑定。只要后台提供的json数据的格式和账票定义体绑定的json数据格式保持一致,后台的数据便可显示在账票上。

    在这里插入图片描述


    三. JS部分

    • GC.ActiveReports对象中解构出账票印刷所需的各类对象
    • 账票定义体是后缀为.rdlx-json的文件,本质上是一个json文件。
    • 从后台请求账票印刷所需的json数据,然后通过账票定义体的DataSources[0].ConnectionProperties.ConnectString连接字符串将其放入账票定义体。
    • 可以定义一个配置对象,用来配置印刷账票的相关设置
    • 使用new Intl.DateTimeFormat来获取时间对象
    window.addEventListener('load', () => {
       init();
    });
    
    function init() {
    
        document.querySelector("#container").addEventListener("click", async function({target: {id}}) {
    
            if (!id) {
                return;
            }
    
            // 显示loading效果
            document.querySelector("#loading").classList.remove("hidden");
    
            // 从GC(GrapeCity的缩写)对象中,解构出账票打印的对象
            const {
                Core: ARJS,
                PdfExport,
                XlsxExport
            } = GC.ActiveReports;
    
            // 模拟从后台获取的json数据
            const dataResponse = await fetch('./01-reports/sales_data_sample_2.json');
            const reportJsonData = await dataResponse.json();
    
            /*
                获取账票的定义体对象,
                该定义体对象一般保存在前台的静态资源文件处
            */ 
            const reportResponse = await fetch('./01-reports/SalesDataByProductLineAndDate.rdlx-json');
            const report = await reportResponse.json();
    
            // 将后台得到的json数据放到账票定义体中
            report.DataSources[0].ConnectionProperties.ConnectString = "jsondata=" + JSON.stringify(reportJsonData);
    
            // 根据账票类型进行配置
            const exportCategoryMap = new Map([
                ["pdf", {
                    info: {
                        title: 'Invoice List',
                        subject: 'This is the Invoice List',
                        author: 'John K',
                        keywords: 'PDF; import; export'
                    }
                }],
                ["excel", {
                    info: {
                        creator: '贾飞天'
                    },
                    // 设置sheet页名称
                    sheetName: 'Sheet_Details',
                    // 印刷纸张设置
                    pageSettings: {
                        size:'A4',
                        orientation: 'landscape'
                    },
                    // Excel文件设置的密码(该密码可以由后台返回给前台)
                    password: 'password'
                }],
            ]);
            const exportSetting = exportCategoryMap.get(id);
            
            // 账票类型的Map映射
            const exportObj = new Map([
                ["pdf", PdfExport],
                ["excel", XlsxExport]
            ]).get(id);
    
            // 注册字体
            ARJS.FontStore.registerFonts('./02-fonts/fontsConfig.json');
            
            // 加载账票定义体对象
            const pageReport = new ARJS.PageReport({ language: "ja" });
    await pageReport.load(report);
    
            // 获取账票导出对象
            const pageDocument = await pageReport.run();
    const result = await exportObj.exportDocument(pageDocument, exportSetting);
    
            // 账票下载
            result.download(`账票名_${yyyyMMddHHmmssTime()}`);
            
            // 隐藏loading效果
            document.querySelector("#loading").classList.add("hidden");
        });
    }
    
    // 构造yyyyMMddHHmmss时间
    function yyyyMMddHHmmssTime() {
         return new Intl.DateTimeFormat('jp', {
             	year: 'numeric', 
    			month: '2-digit', 
    			day: '2-digit',   
    			hour: 'numeric',
    			hour12: false,  
    			minute: 'numeric', 
    			second: 'numeric',
    			fractionalSecondDigits: 3,
         }).format(new Date())
    	   .replaceAll("/", "")
    	   .replaceAll(":", "")
    	   .replaceAll(" ", "")
    	   .replaceAll(".", "");
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105

    四. 效果

    ⏹PDF效果
    在这里插入图片描述

    ⏹Excel效果

    在这里插入图片描述


    五. Vue用法

    ⏹package.json

    "dependencies": {
    	"@grapecity/activereports": "^4.0.2",
    	"@grapecity/activereports-localization": "^4.0.2",
     	// ...省略...
    },
    
    • 1
    • 2
    • 3
    • 4
    • 5

    ⏹Vue部分

    import { Core, PdfExport, XlsxExport } from "@grapecity/activereports";
    import '@grapecity/activereports-localization';
    import '@grapecity/activereports/xlsxexport';
    import '@grapecity/activereports/pdfexport';
    import '@grapecity/activereports';
    
    // 账票印刷部分代码和纯JS代码相同...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    计算机毕业设计java基于javaweb+ssm+vue婚纱摄影网站
    智能售后工单系统是什么?智能工单系统有什么用?
    数据结构和常用排序算法复杂度
    经典目标检测神经网络 - RCNN、SSD、YOLO
    仿真必修课:计算电磁学入门(附件参考文献与笔记)
    认识JUC
    Python包管理工具之pipenv
    Java常用类String
    关于数据迁移:解决kettle中mysql的数据是tinyint的kettle查出来后变成boolean问题
    Redis(四) 主从、哨兵、集群环境搭建
  • 原文地址:https://blog.csdn.net/feyehong/article/details/132724088