A custom font can be registered with the PDFFontsManager.registerFont function. When you set the cell's style as the customer font, SpreadJS will register that font when exporting to PDF.
Override the PDFFontsManager.fallbackFont function to provide a custom font file for the specific font string:
<template>
<div class="sample-tutorial">
<gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread">
</gc-spread-sheets>
<div class="options-container">
<p>Click this button to save the workbook in Spread with its own custom fonts to a PDF file.</p>
<div class="option-row">
<input type="button" value="Export PDF" id="savePDF" v-on:click="exportPDF" />
</div>
</div>
</div>
</template>
<script>
import Vue from 'vue';
import '@mescius/spread-sheets-vue'
import GC from '@mescius/spread-sheets';
import '@mescius/spread-sheets-print';
import '@mescius/spread-sheets-pdf';
import './styles.css';
let App = Vue.extend({
name: "app",
data: function () {
return {
autoGenerateColumns: false,
spread: null
};
},
methods: {
exportPDF() {
this.spread.savePDF(function (blob) {
saveAs(blob, 'download.pdf');
}, console.log);
},
initSpread(spread) {
this.spread = spread;
let data = report, fontsObj = fonts;
spread.fromJSON(data);
let sheet = spread.getActiveSheet();
sheet.setColumnWidth(0, 50);
sheet.setColumnWidth(1, 120);
sheet.setColumnWidth(2, 60);
sheet.setColumnWidth(3, 60);
// register the specific custom font
this.registerCustomFont(fontsObj);
this.setCustomFontForCell(sheet);
// Provide a custom font file for the specific font string.
this.customizeFont(fontsObj);
// setting printInfo
this.setPrintInfo(spread);
},
registerCustomFont(fontsObj) {
let fonts = {
normal: fontsObj["customfont1.ttf"]
};
GC.Spread.Sheets.PDF.PDFFontsManager.registerFont('customfont1', fonts);
},
setCustomFontForCell(sheet) {
var style = sheet.getStyle(0, 0);
style.fontFamily = 'customfont1';
sheet.setStyle(0, 0, style);
},
customizeFont(fontsObj) {
let fonts = {
normal: fontsObj["customfont2.ttf"]
};
GC.Spread.Sheets.PDF.PDFFontsManager.fallbackFont = function (font) {
let fontInfoArray = font.split(' '), fontName = fontInfoArray[fontInfoArray.length - 1];
if (fontName === 'Calibri') {
return fonts.normal;
}
}
},
setPrintInfo(spread) {
let sheet = spread.getActiveSheet();
let printInfo = sheet.printInfo();
printInfo.showBorder(true);
printInfo.showGridLine(false);
printInfo.showColumnHeader(GC.Spread.Sheets.Print.PrintVisibilityType.hide);
printInfo.showRowHeader(GC.Spread.Sheets.Print.PrintVisibilityType.hide);
printInfo.centering(GC.Spread.Sheets.Print.PrintCentering.horizontal);
}
},
computed: {
dataSource() {
return getData();
}
}
});
new Vue({ render: h => h(App) }).$mount('#app');
</script>
<!doctype html>
<html style="height:100%;font-size:14px;">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="$DEMOROOT$/en/vue/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/spread/source/data/fonts.js" type="text/javascript"></script>
<script src="$DEMOROOT$/spread/source/data/pdfExportData.js" type="text/javascript"></script>
<script src="$DEMOROOT$/spread/source/js/FileSaver.js" type="text/javascript"></script>
<!-- SystemJS -->
<script src="$DEMOROOT$/en/vue/node_modules/systemjs/dist/system.src.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('./src/app.vue');
System.import('$DEMOROOT$/en/lib/vue/license.js');
</script>
</head>
<body>
<div id="app" style="height: 100%;"></div>
</body>
</html>
.sample-tutorial {
position: relative;
height: 100%;
overflow: hidden;
}
.sample-spreadsheets {
width: calc(100% - 280px);
height: 100%;
overflow: hidden;
float: left;
}
.options-container {
float: right;
width: 280px;
padding: 12px;
height: 100%;
box-sizing: border-box;
background: #fbfbfb;
overflow: auto;
}
.option-row {
font-size: 14px;
padding: 5px;
margin-top: 10px;
}
input {
padding: 8px 14px;
display: block;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
(function (global) {
System.config({
transpiler: 'plugin-babel',
babelOptions: {
es2015: true
},
meta: {
'*.css': { loader: 'css' },
'*.vue': { loader: 'vue-loader' }
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
'@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js',
'@mescius/spread-sheets-print': 'npm:@mescius/spread-sheets-print/index.js',
'@mescius/spread-sheets-pdf': 'npm:@mescius/spread-sheets-pdf/index.js',
'@mescius/spread-sheets-vue': 'npm:@mescius/spread-sheets-vue/index.js',
'@grapecity/jsob-test-dependency-package/react-components': 'npm:@grapecity/jsob-test-dependency-package/react-components/index.js',
'jszip': 'npm:jszip/dist/jszip.js',
'css': 'npm:systemjs-plugin-css/css.js',
'vue': 'npm:vue/dist/vue.min.js',
'vue-loader': 'npm:systemjs-vue-browser/index.js',
'tiny-emitter': 'npm:tiny-emitter/index.js',
'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js',
'systemjs-babel-build':'npm:systemjs-plugin-babel/systemjs-babel-browser.js'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
src: {
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
},
"node_modules": {
defaultExtension: 'js'
}
}
});
})(this);