2017-07-08 40 views
-1

通過僅使用NodeJS或Javascript,我想創建一個包含該圖表的圖表和數據表的簡單網頁。之後,我想將表格和圖表保存爲PDF文件。 重要:我的設備,網頁將繼續運行,是獨立的,一旦運行它將連接到無法訪問Internet的網絡。也就是說,有必要在設備上安裝所有設備(沒問題!),因爲它無法訪問外部組件或資源。使用Javascript的表格和圖表

如何在網頁上創建圖表和表格並保存爲PDF文件?

回答

0

您可以使用jsPDF這裏位於https://parall.ax/products/jspdf和下方是一個工作示例

var doc = new jsPDF() 
 

 
doc.text('Hello world!', 10, 10) 
 
doc.save('a4.pdf')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.4/jspdf.debug.js"></script>

var chart_data = { 
 
    labels: ['Player1', 'Player2', 'Player3', 'Player4'], 
 
    datasets: [{ 
 
    fillColor: "rgba(6, 118, 152, 0.71)", 
 
    strokeColor: "rgba(220,220,220,1)", 
 
    pointColor: "rgba(220,220,220,1)", 
 
    pointStrokeColor: "#fff", 
 
    pointHighlightFill: "#fff", 
 
    pointHighlightStroke: "rgba(220,220,220,1)", 
 
    data: [20, 34, 15, 64, ] 
 
    }] 
 
} 
 

 
//original canvas 
 
var canvas = document.querySelector('#cool-canvas'); 
 
var context = canvas.getContext('2d'); 
 

 
new Chart(context).Line(chart_data); 
 

 
//hidden canvas 
 
var newCanvas = document.querySelector('#supercool-canvas'); 
 
newContext = newCanvas.getContext('2d'); 
 

 
var supercoolcanvas = new Chart(newContext).Line(chart_data); 
 
supercoolcanvas.defaults.global = { 
 
    scaleFontSize: 600 
 
} 
 

 
//add event listener to button 
 
document.getElementById('download-pdf').addEventListener("click", downloadPDF); 
 

 
//donwload pdf from original canvas 
 
function downloadPDF() { 
 
    var canvas = document.querySelector('#cool-canvas'); 
 
    //creates image 
 
    var canvasImg = canvas.toDataURL("image/jpeg", 1.0); 
 

 
    //creates PDF from img 
 
    var doc = new jsPDF('landscape'); 
 
    doc.setFontSize(20); 
 
    doc.text(15, 15, "Cool Chart"); 
 
    doc.addImage(canvasImg, 'JPEG', 10, 10, 280, 150); 
 
    doc.save('canvas.pdf'); 
 
} 
 

 
//add event listener to 2nd button 
 
document.getElementById('download-pdf2').addEventListener("click", downloadPDF2); 
 

 
//download pdf form hidden canvas 
 
function downloadPDF2() { 
 
    var newCanvas = document.querySelector('#supercool-canvas'); 
 

 
    //create image from dummy canvas 
 
    var newCanvasImg = newCanvas.toDataURL("image/jpeg", 1.0); 
 

 
    //creates PDF from img 
 
    var doc = new jsPDF('landscape'); 
 
    doc.setFontSize(20); 
 
    doc.text(15, 15, "Super Cool Chart"); 
 
    doc.addImage(newCanvasImg, 'JPEG', 10, 10, 280, 150); 
 
    doc.save('new-canvas.pdf'); 
 
}
button { 
 
    margin-top: 30px; 
 
    padding: 10px 20px; 
 
    border-radius: 0; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.4/jspdf.debug.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script> 
 
<div> 
 
    <canvas id="cool-canvas" width="600" height="300"></canvas> 
 
</div> 
 

 
<div style="height:0; width:0; overflow:hidden;"> 
 
    <canvas id="supercool-canvas" width="1200" height="600"></canvas> 
 
</div> 
 

 
<button type="button" id="download-pdf"> 
 
    Download PDF 
 
</button> 
 

 
<button type="button" id="download-pdf2"> 
 
    Download Higher Quality PDF 
 
</button>

+0

太好了! 還有兩個問題: 1)我放在頁面上的所有東西都會保存在pdf文件中,包括一個簡單的數據表? 2)如何在同一頁上創建圖表? 謝謝! – wBB

+0

哈哈,不能爲你做所有的工作 – Rick

+0

感謝你關注上一個答案,但我並沒有要求你用完整的代碼解決問題。我只是不知道它是否可以完成。我聽說過jsPDF以及其他工具,我會嘗試使用它。但是當我要開始時,如果我不知道如何開始,我總是在開始之前問一個人。在我最初的問題中,我要求提供圖表,所以我重複了這個問題以利用這個話題。但是沒問題!在下一次我將分成兩個職位。謝謝。 – wBB