本帖最后由 KearneyKang 于 2023-6-21 09:46 编辑
ARJS提供web端的设计器,Web端设计器在进行报表设计的时候默认可选择的字体有 42 种可供选择
但是在实际业务中,有时候我们不需要报表设计者有那么多的字体可供选择。我只想设置几种常见的字体如:微软雅黑、宋体、楷体、等线等几种字体。
因为ARJS是一种纯前端报表控件,在报表导出PDF的时候如果没有进行对应字体配置,会出现一个情况进行。导出PDF时候部分字体会出现小空格、字体显示乱码的情况。但是我们在进行报表集成的时候也很难做到在字体配置的那个json文件中把所有字体对应的字体配置文件全部给装载到项目中,比较字体文件还是比较大的。
为了解决上面的问题,下面我就给大家讲解下如何通过前端配置实现web设计器字体下拉想选择自定义
1、首先进行字体配置文件Json的设置——fontsConfig.json文件
其中Source就是对应字体文件存放的源文件的相对路径,这点一定要配置好,不然导出PDF的时候就会出现小空格、字体显示乱码的情况
- {
- "path": "",
- "descriptors": [
- {
- "name": "微软雅黑",
- "locals": [
- "微软雅黑",
- "MicrosoftYaHei"
- ],
- "source": "fonts/SimHei.ttf"
- },
- {
- "name": "宋体",
- "source": "<span style="color: rgb(163, 21, 21); background-color: rgb(255, 255, 255); font-family: Consolas, "Courier New", monospace; white-space: pre;">fonts/simsun.ttc</span>"
- }
- ]
- }
复制代码 2、Source对应字体文件的来源可以在网上下载,也可以直接在本地电脑的 Fonts文件中查找
3、json文件配置好之后,把对应的文件要集成到项目中去,然后进行有一个字体下拉重置的设置
- var designer = new GC.ActiveReports.ReportDesigner.Designer("#designer-host", { language: "zh" });
- GC.ActiveReports.Core.FontStore.registerFonts("/fonts/fontsConfig.json").then(() => {
- designer.fontSet = 'registered';
- });
复制代码 4、在Html项目中的具体配置
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8" />
- <meta http-equiv="X-UA-Compatible" content="IE=edge" />
- <title>ActiveReportsJS sample</title>
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <script src="/dist/ie-polyfills.js"></script>
- <script src="https://cdn.grapecity.com/activereportsjs/4.latest/dist/ar-js-core.js"></script>
- <script src="/dist/ar-js-designer.js"></script>
- <script src="/dist/ar-js-viewer.js"></script>
- <script src="/dist/ar-js-pdf.js"></script>
- <script src="/dist/locales/designer/zh-locale.js"></script>
- <script src="/dist/locales/ar-js-locales.js"></script>
- <link href="https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,300;0,400;0,500;0,600;0,700;1,400&display=swap"
- rel="stylesheet" />
- <link rel="stylesheet"
- href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
- integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z"
- crossorigin="anonymous" />
- <link rel="stylesheet"
- type="text/css"
- href="/styles/ar-js-ui.css" />
- <link rel="stylesheet"
- type="text/css"
- href="/styles/ar-js-viewer.css" />
- <link rel="stylesheet"
- type="text/css"
- href="/styles/ar-js-designer.css" />
- <style>
- #designer-host, #viewer-host {
- margin: 0 auto;
- width: 100%;
- height: 800px;
- }
- </style>
- </head>
- <body>
- <div id="designer-toolbar" class="container-fluid">
- <div class="row mt-3 mb-3">
- <button id="btnPdfPreview"
- type="button"
- class="btn btn-primary btn-sm col-sm-2 ml-1"
- onclick="onPdfPreview()">
- PDF 预览
- </button>
- <button id="btnDesignerOpen"
- type="button"
- class="btn btn-primary btn-sm col-sm-2 ml-1"
- onclick="onOpenDesigner()">
- 打开设计器
- </button>
- </div>
- </div>
- <div id="designer-host"></div>
- <div id="viewer-host"></div>
- <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
- integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
- crossorigin="anonymous"></script>
- <script>
- var designer = new GC.ActiveReports.ReportDesigner.Designer("#designer-host", { language: "zh" });
- GC.ActiveReports.Core.FontStore.registerFonts("/fonts/fontsConfig.json").then(() => {
- designer.fontSet = 'registered';
- });
- function onOpenDesigner() {
- $('#viewer-host, #btnDesignerOpen').hide();
- $('#designer-host, #btnPdfPreview').show();
- }
- async function onPdfPreview() {
- const reportInfo = await this.designer.getReport();
- const report = new GC.ActiveReports.Core.PageReport()
- await report.load(reportInfo?.definition);
- const doc = await report.run();
- const result = await GC.ActiveReports.PdfExport.exportDocument(doc);
- result.download("exportedreport.pdf");
- }
- var reportTemplates = GC.ActiveReports.ReportDesigner.templates;
- var resolveFunc = null;
- var counter = 0;
- reportStorage = new Map();
- function fillReportList() {
- $('#listReports').empty();
- let reportIds = reportStorage.keys();
- let result = reportIds.next();
- while (!result.done) {
- const reportId = result.value;
- const openReportBtn = $('<button type="button" class="list-group-item list-group-item-action">' + reportId + '</button>');
- openReportBtn.on('click', function () {
- onSelectReport(reportId);
- })
- $('#listReports').append(openReportBtn);
- result = reportIds.next();
- }
- }
- designer.setActionHandlers({
- onRender: (report) => {
- $('#viewer-host, #btnDesignerOpen').show();
- $('#designer-host, #btnPdfPreview').hide();
- viewer.open(report.definition);
- return Promise.resolve();
- },
- onCreate: function () {
- designer.setReport({ id: 'reports/空白RDL报表.rdlx-json'});
- },
- /*onOpen: function () {
- const ret = new Promise(function (resolve) {
- resolveFunc = resolve
- fillReportList();
- $("#dlgOpen").modal("show");
- });
- return ret;
- },*/
- onOpen:function()
- {
- const input=document.createElement("input");
- input.id="input";
- input.type="file";
- input.style.opacity=0;
- input.addEventListener('change',() => {
- let files = document.getElementById("input").files;
- if(files.length){
- let file = files[0];
- let reader = new FileReader();
- reader.onload = function(){
- console.log(this.result);
- };
- reader.readAsText(file);
- }
- });
- if( document.getElementById('input')){
- $("#input").click();
- }
-
- document.body.appendChild(input);
- },
- onSave: function (info) {
- const reportId = info.id || `NewReport${++this.counter}`;
- reportStorage.set(reportId, info.definition);
- return Promise.resolve({ displayName: reportId });
- },
- onSaveAs: function (info) {
- const reportId = `NewReport${++this.counter}`;
- alert(reportId);
- reportStorage.set(reportId, info.definition);
- return Promise.resolve({ id: reportId, displayName: reportId });
- }
- });
- designer.setReport({ id: 'reports/钻头使用情况.rdlx-json'});
- var viewer = new ActiveReports.Viewer('#viewer-host', { language: "zh" });
- $('#viewer-host, #btnDesignerOpen').hide();
- function onSelectReport(reportId) {
- if (resolveFunc) {
- $("#dlgOpen").modal("hide");
- resolveFunc({ definition: reportStorage.get(reportId), id: reportId, displayName: reportId });
- resolveFunc = null;
- }
- }
- //designerOptions.dataTab.dataSources.visible=true;
- //designerOptions.dataTab.dataSets.canModify =true;
- //designerOptions.dataTab.dataSources.canModify =true;
- </script>
- <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
- integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
- crossorigin="anonymous"></script>
- <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"
- integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN"
- crossorigin="anonymous"></script>
- <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"
- integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV"
- crossorigin="anonymous"></script>
- <div class="modal" id="dlgOpen" tabindex="-1" aria-hidden="true">
- <div class="modal-dialog">
- <div class="modal-content">
- <div class="modal-header">
- <h5 class="modal-title" id="exampleModalLabel">
- 打开报表
- </h5>
- <button type="button"
- class="close"
- data-dismiss="modal"
- aria-label="Close">
- <span aria-hidden="true">×</span>
- </button>
- </div>
- <div class="modal-body">
- <h2>请选择报表:</h2>
- <div class="list-group" id="listReports">
- </div>
- </div>
- <div class="modal-footer">
- <button type="button" class="btn btn-secondary" data-dismiss="modal">
- 关闭
- </button>
- </div>
- </div>
- </div>
- </div>
- </body>
- </html>
复制代码 5、预览结果
6、demo
|