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:
window.onload = function () {
var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), {sheetCount: 2});
initSpread(spread);
};
function initSpread(spread) {
document.getElementById('savePDF').onclick = function () {
spread.savePDF(function (blob) {
saveAs(blob, 'download.pdf');
}, console.log);
};
var data = report, fontsObj = fonts;
spread.fromJSON(data);
var 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(spread);
}
function registerCustomFont(fontsObj) {
var fonts = {
normal: fontsObj["customfont1.ttf"]
};
GC.Spread.Sheets.PDF.PDFFontsManager.registerFont('customfont1', fonts);
}
function setCustomFontForCell (sheet) {
var style = sheet.getStyle(0, 0);
style.fontFamily = 'customfont1';
sheet.setStyle(0, 0, style);
}
function customizeFont(fontsObj) {
var fonts = {
normal: fontsObj["customfont2.ttf"]
};
GC.Spread.Sheets.PDF.PDFFontsManager.fallbackFont = function (font) {
var fontInfoArray = font.split(' '), fontName = fontInfoArray[fontInfoArray.length - 1];
if (fontName === 'Calibri') {
return fonts.normal;
}
}
}
function setPrintInfo(spread) {
var sheet = spread.getActiveSheet();
var 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);
}
<!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/purejs/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/en/purejs/node_modules/@mescius/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/spread/source/js/FileSaver.js" type="text/javascript"></script>
<script src="$DEMOROOT$/en/purejs/node_modules/@mescius/spread-sheets-print/dist/gc.spread.sheets.print.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/en/purejs/node_modules/@mescius/spread-sheets-pdf/dist/gc.spread.sheets.pdf.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/spread/source/js/license.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>
<script src="app.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="sample-tutorial">
<div id="ss" class="sample-spreadsheets"></div>
<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">
</div>
</div>
</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;
}