Overview

SpreadJS supports exporting to a PDF file. PDF Export is one of the plugin features of SpreadJS, and it requires the print plugin to be included in the page.

<p>To use the PDF Export feature, add a link to the JavaScript files in the document's head section below the link to SpreadJS:</p> <pre><code class="hljs">&lt;head&gt; ... &lt;script src='.../spreadjs/<span class="hljs-keyword">gc</span>.spread.sheets.all.<span class="hljs-keyword">x</span>.<span class="hljs-keyword">x</span>.<span class="hljs-keyword">x</span>.<span class="hljs-keyword">min</span>.js' <span class="hljs-keyword">type</span>='text/javascript'&gt;&lt;/script&gt; &lt;script src='.../spreadjs/plugins/<span class="hljs-keyword">gc</span>.spread.sheets.print.<span class="hljs-keyword">x</span>.<span class="hljs-keyword">x</span>.<span class="hljs-keyword">x</span>.<span class="hljs-keyword">min</span>.js' <span class="hljs-keyword">type</span>='text/javascript'&gt;&lt;/script&gt; &lt;script src='.../spreadjs/plugins/<span class="hljs-keyword">gc</span>.spread.sheets.pdf.<span class="hljs-keyword">x</span>.<span class="hljs-keyword">x</span>.<span class="hljs-keyword">x</span>.<span class="hljs-keyword">min</span>.js' <span class="hljs-keyword">type</span>='text/javascript'&gt;&lt;/script&gt; &lt;/head&gt; </code></pre> <p>To export the PDF file, you can using the <strong>spread.savePDF()</strong> method:</p> <pre><code class="hljs js language-js"> spread.savePDF(successCallback: <span class="hljs-built_in">Function</span>, <span class="hljs-attr">errorCallback</span>: <span class="hljs-built_in">Function</span>, options?: <span class="hljs-built_in">Object</span>, sheetIndex?: number?) </code></pre> <p>The method parameters are:</p> <p><strong>successCallback</strong>: call this function after a successful export. function (blob){}.</p> <p><strong>errorCallback</strong>: Call this function if an error occurs.</p> <p><strong>options</strong>: the options for exporting to a PDF file</p> <ul> <li><strong>options.creator: the name of the application (e.g. Adobe FrameMaker®) that created the original document from which it was converted.</strong></li> <li><strong>options.title: the document’s title</strong></li> <li><strong>options.author: the name of the person who created the document</strong></li> <li><strong>options.keywords: keywords associated with the document</strong></li> <li><strong>options.subject:the subject of the document.</strong></li> </ul> <p><strong>sheetIndex</strong>: Exports the specified sheet with a sheet index. If not set, all visible sheets will be exported.</p> <p>For example, you can export all the sheets in SpreadJS to PDF using the following code:</p> <pre><code class="hljs js language-js"> spread.savePDF(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">blob</span>) </span>{ <span class="hljs-keyword">var</span> fileName = $(<span class="hljs-string">'#fileName'</span>).val() || <span class="hljs-string">'download'</span>; saveAs(blob, fileName + <span class="hljs-string">'.pdf'</span>); <span class="hljs-comment">// Use the https://github.com/eligrey/FileSaver.js to save file directly.</span> }, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">error</span>) </span>{ <span class="hljs-built_in">console</span>.log(error); }, { <span class="hljs-attr">title</span>: <span class="hljs-string">'Test Title'</span>, <span class="hljs-attr">author</span>: <span class="hljs-string">'Test Author'</span>, <span class="hljs-attr">subject</span>: <span class="hljs-string">'Test Subject'</span>, <span class="hljs-attr">keywords</span>: <span class="hljs-string">'Test Keywords'</span>, <span class="hljs-attr">creator</span>: <span class="hljs-string">'test Creator'</span> }); </code></pre> <p>SpreadJS supports 14 built-in standard fonts:</p> <p><strong>Courier:</strong> Courier, Courier-Bold, Courier-Oblique, Courier-BoldOblique</p> <p><strong>Times:</strong> Tomes-Roman, Times-Bold, Times-Italic, Times-BoldItalic</p> <p><strong>Helvetica:</strong> Helvetica, Helvetica-Bold, Helvetica-Oblique, Helvetica-BoldOblique</p> <p><strong>Symbol:</strong> Symbol</p> <p><strong>ZapfDingbats:</strong> ZapfDingbats</p>
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); addSparklineContent(sheet); addPieContent(sheet); addTableContent(sheet); addValue(sheet); sheet.getCell(0, 7).backgroundImage("$DEMOROOT$/spread/source/images/olympic.jpg"); var printInfo = sheet.printInfo(); printInfo.showBorder(true); printInfo.showGridLine(true); printInfo.headerCenter("Olympic Athletes"); printInfo.headerLeft("&G"); printInfo.headerLeftImage("$DEMOROOT$/spread/source/images/olympic.jpg"); printInfo.footerCenter("&P/&N"); sheet.resumePaint(); }; var spreadNS = GC.Spread.Sheets; function addValue(sheet) { var value = 1; for (var r = 21; r < 68; r++) { for (var c = 0; c < 20; c++) { sheet.setValue(r, c, value++); } } } function addSparklineContent(sheet) { sheet.addSpan(0, 0, 1, 7); sheet.setRowHeight(0, 40); sheet.getCell(0, 0).value("The company revenue in 2014").font("italic Bold 20px Times").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.addSpan(1, 2, 1, 2); sheet.addSpan(1, 4, 1, 2); sheet.addSpan(1, 6, 1, 2); sheet.setValue(1, 0, "Month"); sheet.setValue(1, 1, "Revenue"); sheet.setValue(1, 2, "Diagram 1"); sheet.setValue(1, 4, "Diagram 2"); sheet.setValue(1, 6, "Diagram 3"); sheet.getRange(1, 0, 1, 7).backColor("Accent 4").foreColor("white"); for (var i = 2; i < 5; i++) { sheet.setValue(i, 0, new Date(2014, i - 1, 1)); sheet.setFormatter(i, 0, "mm/dd/yyyy"); } sheet.setColumnWidth(0, 80); sheet.setValue(2, 1, 30); sheet.setValue(3, 1, -60); sheet.setValue(4, 1, 80); sheet.addSpan(2, 2, 3, 2); sheet.setFormula(2, 2, '=LINESPARKLINE(B3:B5,0,A3:A5,0,"{ac:#ffff00,fmc:brown,hmc:red,lastmc:blue,lowmc:green,mc:purple,nc:yellowgreen,sc:pink,dxa:true,sf:true,sh:true,slast:true,slow:true,sn:true,sm:true,lw:3,dh:false,deca:1,rtl:false,minat:1,maxat:1,mmax:5,mmin:-3}")'); sheet.addSpan(2, 4, 3, 2); sheet.setFormula(2, 4, '=COLUMNSPARKLINE(B3:B5,0,A3:A5,0,"{ac:#ffff00,fmc:brown,hmc:red,lastmc:blue,lowmc:green,mc:purple,nc:yellowgreen,sc:pink,dxa:true,sf:true,sh:true,slast:true,slow:true,sn:true,sm:true,lw:3,dh:false,deca:1,rtl:false,minat:1,maxat:1,mmax:5,mmin:-3}")'); sheet.addSpan(2, 6, 3, 2); sheet.setFormula(2, 6, '=WINLOSSSPARKLINE(B3:B5,0,A3:A5,0)'); } function addPieContent(sheet) { sheet.addSpan(6, 0, 1, 5); sheet.setRowHeight(6, 40); sheet.getCell(6, 0).value("My Assets").font("20px Times-Roman").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center); sheet.addSpan(7, 2, 1, 2); sheet.addSpan(8, 2, 3, 2); sheet.setValue(7, 0, "Asset Type"); sheet.setValue(7, 1, "Amount"); sheet.setValue(7, 2, "Diagram"); sheet.setValue(7, 4, "Note"); sheet.setValue(8, 0, "Savings"); sheet.getRange(7, 0, 1, 5).backColor("Accent 4").foreColor("white"); sheet.getCell(8, 1).value(25000).formatter("$#,##0"); sheet.setValue(9, 0, "401k"); sheet.getCell(9, 1).value(55000).formatter("$#,##0"); sheet.setValue(10, 0, "Stocks"); sheet.getCell(10, 1).value(15000).formatter("$#,##0"); sheet.setFormula(8, 2, '=PIESPARKLINE(B9:B11,"#919F81","#D7913E","CEA722")'); sheet.getCell(8, 4).backColor("#919F81").formula("=B9/SUM(B9:B11)").formatter("0.00%"); sheet.getCell(9, 4).backColor("#D7913E").formula("=B10/SUM(B9:B11)").formatter("0.00%"); sheet.getCell(10, 4).backColor("#CEA722").formula("=B11/SUM(B9:B11)").formatter("0.00%"); } function addTableContent(sheet) { var data = [ ["bob", "36", "man", "Beijing", "80"], ["Betty", "28", "woman", "Xi'an", "52"], ["Gary", "23", "man", "NewYork", "63"], ["Hunk", "45", "man", "Beijing", "80"], ["Cherry", "37", "woman", "Shanghai", "58"]]; sheet.tables.addFromDataSource("sampleTable8", 13, 0, data); }
<!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/@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/en/purejs/node_modules/@grapecity/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/@grapecity/spread-sheets-print/dist/gc.spread.sheets.print.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/en/purejs/node_modules/@grapecity/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="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 export the Spread component 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; }