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:
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import './styles.css';
import { AppFunc } from './app-func';
// import { App } from './app-class';
// 1. Functional Component sample
ReactDOM.render(<AppFunc />, document.getElementById('app'));
// 2. Class Component sample
// ReactDOM.render(<App />, document.getElementById('app'));
import * as React from 'react';
import GC from '@mescius/spread-sheets';
import '@mescius/spread-sheets-print';
import '@mescius/spread-sheets-pdf';
import { SpreadSheets } from '@mescius/spread-sheets-react';
export function AppFunc() {
let spread = null;
const exportPDF = () => {
spread.savePDF(function (blob) {
saveAs(blob, 'download.pdf');
}, console.log);
}
const initSpread = (currSpread) => {
spread = currSpread;
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
registerCustomFont(fontsObj);
setCustomFontForCell(sheet);
// Provide a custom font file for the specific font string.
customizeFont(fontsObj);
// setting printInfo
setPrintInfo();
}
const registerCustomFont = (fontsObj) => {
let fonts = {
normal: fontsObj["customfont1.ttf"]
};
GC.Spread.Sheets.PDF.PDFFontsManager.registerFont('customfont1', fonts);
}
const setCustomFontForCell = (sheet) => {
var style = sheet.getStyle(0, 0);
style.fontFamily = 'customfont1';
sheet.setStyle(0, 0, style);
}
const 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;
}
}
}
const setPrintInfo = () => {
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);
}
return (
<div class="sample-tutorial">
<div class="sample-spreadsheets">
<SpreadSheets workbookInitialized={initSpread}>
</SpreadSheets>
</div>
<Panel exportPDF={exportPDF} />
</div>
);
}
function Panel(props) {
return (
<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" onClick={props.exportPDF} />
</div>
</div>
);
}
import * as React from 'react';
import GC from '@mescius/spread-sheets';
import '@mescius/spread-sheets-print';
import '@mescius/spread-sheets-pdf';
import { SpreadSheets } from '@mescius/spread-sheets-react';
const Component = React.Component;
export class App extends Component {
constructor(props) {
super(props);
this.spread = null;
}
render() {
return (
<div class="sample-tutorial">
<div class="sample-spreadsheets">
<SpreadSheets workbookInitialized={spread => this.initSpread(spread)}>
</SpreadSheets>
</div>
<Panel exportPDF={(e) => { this.exportPDF(e) }} />
</div>
)
}
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);
}
}
class Panel extends Component {
constructor(props) {
super(props);
}
render() {
return (
<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" onClick={() => { this.props.exportPDF() }} />
</div>
</div>
)
}
}
<!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/react/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/spread/source/js/FileSaver.js" type="text/javascript"></script>
<script src="$DEMOROOT$/spread/source/data/fonts.js" type="text/javascript"></script>
<script src="$DEMOROOT$/spread/source/data/pdfExportData.js" type="text/javascript"></script>
<!-- SystemJS -->
<script src="$DEMOROOT$/en/react/node_modules/systemjs/dist/system.src.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('$DEMOROOT$/en/lib/react/license.js').then(function () {
System.import('./src/app');
});
</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,
react: true
},
meta: {
'*.css': { loader: 'css' }
},
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-react': 'npm:@mescius/spread-sheets-react/index.js',
'@grapecity/jsob-test-dependency-package/react-components': 'npm:@grapecity/jsob-test-dependency-package/react-components/index.js',
'react': 'npm:react/umd/react.production.min.js',
'react-dom': 'npm:react-dom/umd/react-dom.production.min.js',
'css': 'npm:systemjs-plugin-css/css.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: 'jsx'
},
"node_modules": {
defaultExtension: 'js'
},
}
});
})(this);