【问题标题】:Multiple Highcharts on a PDF page - SVG Page LayoutPDF 页面上的多个 Highcharts - SVG 页面布局
【发布时间】:2019-07-18 18:24:41
【问题描述】:

我开发了在单个 pdf 页面(A3 布局)上导出多个高图,其中包含 6 个图表和相关文本。

该功能运行良好。以下是我的代码的链接。

Link

当我点击“导出 PDF”按钮时,会下载包含所有图表和文本的 pdf(我在示例示例中只复制了 1 个图表)。

当您在下载的文档上单击 ctrl+p 进行打印时,用户可以在 pdf 页面的顶部和底部看到大量额外的空白。不应该有任何这样的额外空白。在下载的文档中没有这样的空白。

如何修改 SVG 元素的高度和宽度等问题。

$(function () {


Highcharts.getSVG = function(charts,texts, options, callback) {


var svgArr = [],
top = 0,
width = 0,
newLine = false,
txt,txt1,txt2,txt3,txt4,txt5,txt6,txt7,txt8,txt9,txt10,txt11,txt12,txt13,txt14,txt15,txt16,txt17,txt18,txt19,txt20,txt21,txt22,txt23,txt24,txt25,txt26;
addSVG = function(svgres,i) {


  // Grab width/height from exported chart
  var svgWidth = +svgres.match(
      /^<svg[^>]*width\s*=\s*\"?(\d+)\"?[^>]*>/
    )[1],
    svgHeight = +svgres.match(
      /^<svg[^>]*height\s*=\s*\"?(\d+)\"?[^>]*>/
    )[1],
    // Offset the position of this chart in the final SVG
    svg;




      if (svgWidth > 900) {
          if(i==5){
              top = 1000;
              svg = svgres.replace('<svg', '<g transform="translate(0,' + top + ')"');
          }else{
              svg = svgres.replace('<svg', '<g transform="translate(' + width + ', 0 )"');
          }

        top = Math.max(top, svgHeight);

      } else {
        if (newLine) {
          if(i==4){
              width = 800;
          }
          svg = svgres.replace('<svg', '<g transform="translate(' + width + ', ' + top + ')"');
          top += svgHeight;
          width += svgWidth;
          newLine = false;
        } else {
          newLine = true;
          if(i==5){
              top = 1000;
              svg = svgres.replace('<svg', '<g transform="translate(0,' + top + ')" ');
          }else{
              svg = svgres.replace('<svg', '<g transform="translate(0,' + top + ')" ');
          }

          top = Math.max(top, svgHeight);
          width += svgWidth;
          //width = Math.max(width, chart.chartWidth);
        }
      }



  svg = svg.replace('</svg>', '</g>');
  svgArr.push(svg);

   txt = texts[i];

},
exportChart = function(i) {
  if (i === charts.length) {

      // add SVG image to exported svg
    //addSVG(svgImg.outerHTML);
    //addSVG(svgImg.outerHTML);

      //console.log(top+'-----'+width);
    return callback('<svg height="2000" width="2000" version="1.1" xmlns="http://www.w3.org/2000/svg">' + svgArr.join('') + '</svg>');
  }
  charts[i].getSVGForLocalExport(options, {}, function() {
    console.log("Failed to get SVG");
  }, function(svg) {
    addSVG(svg,i);
    return exportChart(i + 1); // Export next only when this SVG is received
  });
};

//console.log(svgArr);  
exportChart(0);
};

Highcharts.exportCharts = function(charts,texts, options) {
options = Highcharts.merge(Highcharts.getOptions().exporting, options);

// Get SVG asynchronously and then download the resulting SVG
Highcharts.getSVG(charts,texts, options, function(svg) {
Highcharts.downloadSVGLocal(svg, options, function() {
  console.log("Failed to export on client side");
});
});
};

//Set global default options for all charts
Highcharts.setOptions({
exporting: {
fallbackToExportServer: false // Ensure the export happens on the client side or not at all
}
});

如果我增加 SVG 的高度,那么它也会增加宽度

 return callback('<svg height="2000" width="2000" version="1.1" xmlns="http://www.w3.org/2000/svg">' + svgArr.join('') + '</svg>');

【问题讨论】:

  • 我无法复制它。我有一个正确的 pdf,打印出来的时候和 pdf 一样。
  • 您是否考虑过使用额外的库来生成包含多个图表的 pdf,例如 jsPdf?我注意到你有很多硬编码的代码来使用 Highcharts 默认导出模块,它不是为生成复杂的 pdf 而设计的。用 jspdf 检查这个例子:stackoverflow.com/questions/54761784/…
  • @WojciechChmiel,pdf 顶部的空白是否比通常的标题空白大小?我在顶部有 8 行空白,在底部有 6 行空白。
  • @WojciechChmiel,我在打印输出后或点击 ctrl+p 时用白色空间上的红色标记编辑了我的问题
  • 是的,pdf的顶部和底部都有一个空白,但这是由内容的格式造成的,因为它适合这样的页面。这是正确的行为。

标签: javascript html svg highcharts


【解决方案1】:

您的 SVG 正在形成 2000x2000 (height="2000" width="2000") 的方形图像。

您的打印机可能设置为打印 A4 或 Letter 格式,即 210 × 297 毫米(对于 A4)- 不是正方形。

此外,您的打印机会尝试将您的文档 (PDF) 的内容居中,从而导致出现在图表下方的空白区域。

有3个非常简单的解决方案:

  1. 微调您的打印机,使其在页面顶部打印您的文件
  2. 更改图表大小,使其适合您的打印页面(例如height="2100" width="2970"
  3. 更改您的 pdf 文档大小,以便您导出的文档在顶部已​​经适合您的 SVG 图表

【讨论】:

    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多