生成/导出PDF格式的高图表格+图形

时间:2019-05-18 16:03:13

标签: angular pdf highcharts

一种方法是使用绘图API,然后在导出PDF时逐行绘制表格。 还有什么其他方法,例如将html(表)代码呈现为PDF或明智地创建表的行和列,以便更轻松地格式化或更新表。

1 个答案:

答案 0 :(得分:1)

不幸的是,这是唯一具有纯Highcharts库的解决方案:

Highcharts文档:https://www.highcharts.com/docs/getting-started/frequently-asked-questions/#add-data-table

但是,可以采用以下方法制作具有多个图表和其他元素(如表格,图像,标题等)的更复杂的pdf:

  1. 将AJAX发送给Highcharts服务器,其中包含每个图表的选项。返回值将是服务器上图像的URL。
  2. 将Highcharts服务器中的图像转换为base64格式。 (您可以使用以下方法:https://stackoverflow.com/a/20285053/10077925
  3. 使用jspdf库将图表图像,表格,标题等添加到pdf并保存结果。

代码:

$(function() {

  const toDataURL = url => fetch(url)
    .then(response => response.blob())
    .then(blob => new Promise((resolve, reject) => {
      const reader = new FileReader()
      reader.onloadend = () => resolve(reader.result)
      reader.onerror = reject
      reader.readAsDataURL(blob)
    }))

  var chartOptions = {

    title: {
      text: 'Solar Employment Growth by Sector, 2010-2016'
    },

    subtitle: {
      text: 'Source: thesolarfoundation.com'
    },
    
    exporting: {
    	showTable: true
    },

    yAxis: {
      title: {
        text: 'Number of Employees'
      }
    },
    legend: {
      layout: 'vertical',
      align: 'right',
      verticalAlign: 'middle'
    },

    plotOptions: {
      series: {
        label: {
          connectorAllowed: false
        },
        pointStart: 2010
      }
    },

    series: [{
      name: 'Installation',
      data: [43934, 52503, 57177, 69658, 97031, 119931, 137133, 154175]
    }, {
      name: 'Manufacturing',
      data: [24916, 24064, 29742, 29851, 32490, 30282, 38121, 40434]
    }, {
      name: 'Sales & Distribution',
      data: [11744, 17722, 16005, 19771, 20185, 24377, 32147, 39387]
    }, {
      name: 'Project Development',
      data: [null, null, 7988, 12169, 15112, 22452, 34400, 34227]
    }, {
      name: 'Other',
      data: [12908, 5948, 8105, 11248, 8989, 11816, 18274, 18111]
    }],

    responsive: {
      rules: [{
        condition: {
          maxWidth: 500
        },
        chartOptions: {
          legend: {
            layout: 'horizontal',
            align: 'center',
            verticalAlign: 'bottom'
          }
        }
      }]
    }

  };
  
  var chart = Highcharts.chart('container', chartOptions),
  	chartRows = chart.getDataRows();

  var specialElementHandlers = {
    '#editor': function(element, renderer) {
      return true;
    }
  };

  $('#cmd').click(function() {
    var obj = {
        options: JSON.stringify(chartOptions),
        type: 'image/png',
        async: true
      },
      exportUrl = 'https://export.highcharts.com/',
      imgContainer = $("#container"),
      doc = new jsPDF(),
      chartsLen = 1,
      imgUrl;

    var calls = [];

    for (var i = 0; i < chartsLen; i++) {
      calls.push({
        type: 'post',
        url: exportUrl,
        data: obj,
      });
    }

    $.when(
      $.ajax(calls[0])
    ).done(function(c1) {

      imgUrl = exportUrl + c1;

      toDataURL(imgUrl)
        .then(dataUrl => {
          doc.setFontSize(30);
          doc.text(35, 25, 'PDF Title');
          doc.addImage(dataUrl, 'PNG', 15, 40);

					doc.autoTable({
          	startY: 180,
            head: [chartRows[0]],
            body: chartRows.filter((elem, i) => {
            	if (i !== 0) {
              	return elem;
              }
            })
          });

          doc.save('sample-file.pdf');
        })
    });
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="https://unpkg.com/jspdf"></script>
<script src="https://unpkg.com/jspdf-autotable"></script>

<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<div class="chart-outer">
    <div id="container"></div>
    <!-- data table is inserted here -->
</div>
<br><br>
<button id="cmd">generate PDF</button>

演示: