PDF 导出基础示例

SpreadJS 支持将工作簿导出为 PDF 文件,无需依赖服务端处理。通过简单的 API 调用,即可将表格数据、样式、图表等内容完整导出为 PDF 格式,适用于生成报表、打印存档等场景。

概述 本 Demo 展示了 SpreadJS 导出 PDF 的基本功能。Demo 中创建了一个家庭业务成本分析表,包含年度数据表格和饼图迷你图,并设置了打印选项(页眉、页脚、网格线、边框),点击按钮即可将工作表导出为 PDF 文件。 实现思路 引入必要的插件:打印插件(gc.spread.sheets.print.min.js)和 PDF 插件(gc.spread.sheets.pdf.min.js) 初始化工作簿并创建数据表格,设置单元格样式和公式 添加饼图迷你图展示数据占比 配置打印信息:设置页眉、页脚、边框和网格线 绑定按钮事件,调用 savePDF 方法导出 PDF 代码解析 引入插件 导出 PDF 需要先引入打印插件和 PDF 插件,且打印插件必须先加载: 设置打印信息 这段代码配置了打印选项:showBorder 显示边框,showGridLine 显示网格线,headerCenter 设置页眉中间文本,footerCenter 使用特殊格式 &P/&N 显示当前页码和总页数。 导出 PDF 调用 savePDF 方法导出 PDF:第一个参数是成功回调函数,接收导出的 blob 对象;第二个参数是错误回调函数;第三个参数设置 PDF 文档属性(标题、作者、主题等)。 创建饼图迷你图 使用 PIESPARKLINE 公式创建饼图迷你图,第一个参数是数据范围,后续参数指定各扇形的颜色。 运行效果 页面显示一个家庭业务成本分析表,包含 2018-2022 年的各项成本数据 表格下方显示一个饼图迷你图,直观展示各项成本的占比 点击"导出 PDF"按钮,浏览器会下载名为 download.pdf 的文件 导出的 PDF 包含完整的数据表格、饼图迷你图、页眉(Family Business Costs)和页脚(页码/总页数) API 参考 savePDF 方法 successCallback: 成功回调函数,参数为 blob 对象 errorCallback: 错误回调函数,参数为错误信息 options: PDF 文档选项(可选) options.title: 文档标题 options.author: 文档作者 options.subject: 文档主题 options.keywords: 文档关键字 options.creator: 创建者应用名称 sheetIndex: 指定导出的工作表索引(可选),不设置则导出所有可见工作表 printInfo 方法 获取工作表的打印信息对象,可设置: showBorder(boolean): 是否显示边框 showGridLine(boolean): 是否显示网格线 headerCenter(text): 设置页眉中间内容 headerLeft(text): 设置页眉左侧内容 footerCenter(text): 设置页脚中间内容 页眉页脚支持的特殊格式: &P: 当前页码 &N: 总页数 &D: 当前日期 &T: 当前时间 &G: 图片占位符
window.onload = function () { var spread = new spreadNS.Workbook(document.getElementById("ss")); document.getElementById('savePDF').onclick = function () { spread.savePDF( function (blob) { saveAs(blob, 'download.pdf'); }, console.log, { title: 'Test Title', author: 'Test Author', subject: 'Test Subject', keywords: 'Test Keywords', creator: 'test Creator' }); }; var sheet = spread.getActiveSheet(); sheet.suspendPaint(); var style = new GC.Spread.Sheets.Style(); style.font = '15px Times'; sheet.setDefaultStyle(style); addTableContent(sheet); addPieContent(sheet); var printInfo = sheet.printInfo(); printInfo.showBorder(true); printInfo.showGridLine(true); printInfo.headerCenter("Family Business Costs"); printInfo.headerLeft("&G"); printInfo.footerCenter("&P/&N"); sheet.resumePaint(); }; var spreadNS = GC.Spread.Sheets; function addTableContent (sheet) { sheet.addSpan(1, 0, 1, 7); sheet.setRowHeight(1, 40); sheet.getCell(1, 0).value("Costs").font("28px Times").foreColor("#11114f").hAlign(spreadNS.HorizontalAlign.headerLeft).vAlign(spreadNS.VerticalAlign.center); sheet.addSpan(2, 0, 1, 7); sheet.setRowHeight(2, 30); sheet.getCell(2, 0).value("Family Business").font("18px Times").foreColor("#11114f").backColor("#f5f5f5").hAlign(spreadNS.HorizontalAlign.headerLeft).vAlign(spreadNS.VerticalAlign.center); sheet.setColumnWidth(0, 105); sheet.setRowHeight(3, 35); sheet.getCell(3, 0).value("Costs Elements").font("Bold 15px Times").foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.headerLeft).vAlign(spreadNS.VerticalAlign.center); sheet.setColumnWidth(1, 70); sheet.getCell(3, 1).value("2018").font("Bold 15px Times").foreColor("#171717").backColor("#dfe9fb").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.setColumnWidth(2, 70); sheet.getCell(3, 2).value("2019").font("Bold 15px Times").foreColor("#171717").backColor("#d1dffa").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.setColumnWidth(3, 70); sheet.getCell(3, 3).value("2020").font("Bold 15px Times").foreColor("#171717").backColor("#9bbaf3").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.setColumnWidth(4, 70); sheet.getCell(3, 4).value("2021").font("Bold 15px Times").foreColor("#ffffff").backColor("#5c7ee6").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.setColumnWidth(5, 70); sheet.getCell(3, 5).value("2022").font("Bold 15px Times").foreColor("#ffffff").backColor("#1346a4").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.setColumnWidth(6, 70); sheet.getCell(3, 6).value("Total").font("Bold 15px Times").foreColor("#ffffff").backColor("#102565").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(4, 0).value("Salaries").foreColor("#171717").backColor("#ffffff").vAlign(spreadNS.VerticalAlign.center); sheet.setValue(4, 1, 200); sheet.setValue(4, 2, 210); sheet.setValue(4, 3, 250); sheet.setValue(4, 4, 300); sheet.setValue(4, 5, 360); sheet.getCell(4, 1).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(4, 2).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(4, 3).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(4, 4).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(4, 5).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(4, 6).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(5, 0).value("Consulting").foreColor("#171717").backColor("#ffffff").vAlign(spreadNS.VerticalAlign.center); sheet.setValue(5, 1, 100); sheet.setValue(5, 2, 100); sheet.setValue(5, 3, 100); sheet.setValue(5, 4, 240); sheet.setValue(5, 5, 260); sheet.getCell(5, 1).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(5, 2).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(5, 3).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(5, 4).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(5, 5).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(5, 6).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(6, 0).value("Hardware").foreColor("#171717").backColor("#ffffff").vAlign(spreadNS.VerticalAlign.center); sheet.setValue(6, 1, 2000); sheet.setValue(6, 2, 2500); sheet.setValue(6, 3, 0); sheet.setValue(6, 4, 3000); sheet.setValue(6, 5, 3200); sheet.getCell(6, 1).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(6, 2).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(6, 3).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(6, 4).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(6, 5).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(6, 6).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(7, 0).value("Software").foreColor("#171717").backColor("#ffffff").vAlign(spreadNS.VerticalAlign.center); sheet.setValue(7, 1, 500); sheet.setValue(7, 2, 100); sheet.setValue(7, 3, 100); sheet.setValue(7, 4, 200); sheet.setValue(7, 5, 250); sheet.getCell(7, 1).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(7, 2).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(7, 3).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(7, 4).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(7, 5).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(7, 6).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(8, 0).value("Training").foreColor("#171717").backColor("#ffffff").vAlign(spreadNS.VerticalAlign.center); sheet.setValue(8, 1, 1100); sheet.setValue(8, 2, 1300); sheet.setValue(8, 3, 2500); sheet.setValue(8, 4, 1300); sheet.setValue(8, 5, 1250); sheet.getCell(8, 1).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(8, 2).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(8, 3).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(8, 4).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(8, 5).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(8, 6).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(9, 0).value("Travel").foreColor("#171717").backColor("#ffffff").vAlign(spreadNS.VerticalAlign.center); sheet.setValue(9, 1, 2000); sheet.setValue(9, 2, 2000); sheet.setValue(9, 3, 1000); sheet.setValue(9, 4, 1500); sheet.setValue(9, 5, 0); sheet.getCell(9, 1).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(9, 2).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(9, 3).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(9, 4).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(9, 5).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(9, 6).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(10, 0).value("Other").foreColor("#171717").backColor("#ffffff").vAlign(spreadNS.VerticalAlign.center); sheet.setValue(10, 1, 200); sheet.setValue(10, 2, 200); sheet.setValue(10, 3, 100); sheet.setValue(10, 4, 150); sheet.setValue(10, 5, 30); sheet.getCell(10, 1).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(10, 2).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(10, 3).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(10, 4).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(10, 5).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.getCell(10, 6).foreColor("#171717").backColor("#ffffff").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.setFormula(4, 6, "=SUM(B5:F5)"); sheet.setFormula(5, 6, "=SUM(B6:F6)"); sheet.setFormula(6, 6, "=SUM(B7:F7)"); sheet.setFormula(7, 6, "=SUM(B8:F8)"); sheet.setFormula(8, 6, "=SUM(B9:F9)"); sheet.setFormula(9, 6, "=SUM(B10:F10)"); sheet.setFormula(10, 6, "=SUM(B11:F11)"); sheet.getRange(4,1,7,6).formatter("$0.0"); } function addPieContent(sheet) { sheet.addSpan(12, 0, 1, 4); sheet.getCell(12, 0).value("Total Costs").font("15px Times").foreColor("#11114f").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.addSpan(13, 0, 9, 4); sheet.setFormula(13, 0, '=PIESPARKLINE(G5:G11,"#dfe9fb","#d1dffa","#9bbaf3","#5c7ee6","#1346a4","#102565", "#ededed")'); }
<!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="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>点击此按钮将 Spread 组件导出为 PDF 文件。</p> <div class="option-row"> <input type="button" value="导出 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; }