0

I have developed exporting of multiple highcharts on a single pdf page (A3 Layout) which consists of 6 graphs and relative text.

The functionality is working fine. The below is the link for my code.

Link

When I click on the "Export PDF" button, the pdf is been downloaded containg all the charts and text (I have copied only 1 chart in the sample example).

When you click on the ctrl+p on the downloaded document to print then the user can see lot of additional white space on the top and bottom of the pdf page. There shouldn't be any such additional white space. In the downloaded document there is no such white space.

How to modify the SVG elemets for height and width of such issues.

White space

$(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
}
});

If I increase the height in the SVG then it is increasing the width as well

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

Red color mark in white space

John
  • 565
  • 8
  • 23
  • I can't reproduce it. I've got a correct pdf and when it is printed it looks the same as pdf. – Wojciech Chmiel Feb 26 '19 at 07:39
  • Have you considered using additional library to generate a pdf with multiple charts like jsPdf? I've noticed that you have a lot of hardcoded code to make it with Highcharts default exporting module which is not designed to generate complex pdfs. Check this example with jspdf: https://stackoverflow.com/questions/54761784/highcharts-export-multiple-charts-using-jspdf/54768981#54768981 – Wojciech Chmiel Feb 26 '19 at 07:44
  • @WojciechChmiel, Is there any white space on the top of the pdf than the usual size of the header white space? I am getting around 8 lines of white space on the top and 6 lines of white space on the bottom. – John Feb 26 '19 at 12:27
  • @WojciechChmiel, I have edited my question with the red color mark on the white space after taking a printout or when clicked on ctrl+p – John Feb 26 '19 at 12:34
  • Yes, there is a white space on the top and bottom of the pdf, but this is caused by the format of the content and because it is fitted to the page like that. It is the correct behavior. – Wojciech Chmiel Feb 26 '19 at 12:57
  • @WojciechChmiel I want to change the SVG so that there shouldn't be any white space on the top atleast. I can increase the height of the 1st highchart to cover the top white space. Could you please help me in changing the svg at the top. – John Feb 26 '19 at 13:43
  • @WojciechChmiel Any word on this pelase – John Feb 26 '19 at 15:26
  • as you can see the pdf exported by highcharts is correct. During print, the pdf is best fitted in the page of a particular format. As I suggested you above you have a lot of hardcoded code to make it work and Highcharts exporting is not designed to make complicated pdfs with many charts and other elements. Try to make it with jspdf using the approach from this thread: https://stackoverflow.com/questions/54761784/highcharts-export-multiple-charts-using-jspdf/54768981#54768981 – Wojciech Chmiel Feb 27 '19 at 08:56

1 Answers1

0

Your SVG is forming square image 2000x2000 (height="2000" width="2000").

Your printer is probably set to print A4 or Letter format, which is 210 × 297 millimeters (for A4) - not square.

Furthermore, your printer tries to center the content of your document (PDF), leading to white space abode and below your graphs.

There are 3 very simple solutions:

  1. Twig your printer so that it will print your file at the top of the page
  2. Change your graphs size, so that it fits your printing page (eg. height="2100" width="2970")
  3. Change your pdf document size, so that your exported document already fits your SVG graph at the top
Alex Under
  • 1,406
  • 1
  • 12
  • 25