自定义字体

这个示例展示了如何在导出到PDF时注册和使用字体。

当你使用 PDFFontsManager.registerFont 去注册一个字体之后, 你就可以给单元格的样式设置自定义字体了, 而且当你导出PDF时候,就会用你注册的字体。 重写 PDFFontsManger.fallbackFont 方法去提供一个备选的字体。
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); // add custom font to fontManager addFontsToFontManager(fontsObj); // setting printInfo setPrintInfo(spread); } function registerCustomFont(fontsObj) { var fonts = { normal: fontsObj["simkai.ttf"] }; GC.Spread.Sheets.PDF.PDFFontsManager.registerFont('simkai', fonts); } function addFontsToFontManager(fontsObj) { var fonts = { normal: fontsObj["MTCORSVA.TTF"] }; GC.Spread.Sheets.PDF.PDFFontsManager.fallbackFont = function (font) { var fontInfoArray = font.split(' '), fontName = fontInfoArray[fontInfoArray.length - 1]; if (fontName === 'mtcorsva') { 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 name="spreadjs culture" content="zh-cn" /> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/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$/zh/purejs/node_modules/@grapecity-software/spread-sheets-print/dist/gc.spread.sheets.print.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-pdf/dist/gc.spread.sheets.pdf.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-resources-zh/dist/gc.spread.sheets.resources.zh.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; }