score:31
Yes this is possible but involves a few different libraries to get working. The first Library is jsPDF which allows the creation of PDF in the browser. The second is canvg which allows for the rendering and parsing of SVG's, the bit that is really cool though is it can render an svg on to canvas element. Lastly is Highcharts export module which will allow us to send the svg to the canvg to turn into a data URL which can then be given to jsPDF to turn into your pdf.
Here is an example http://fiddle.jshell.net/leighking2/dct9tfvn/ you can also see in there source files you will need to include in your project.
So to start highcharts provides an example of using canvg with it's export to save a chart as a png. because you want all the iamges in a pdf this has been slightly altered for our purpose to just return the data url
// create canvas function from highcharts example http://jsfiddle.net/highcharts/PDnmQ/
(function (H) {
H.Chart.prototype.createCanvas = function (divId) {
var svg = this.getSVG(),
width = parseInt(svg.match(/width="([0-9]+)"/)[1]),
height = parseInt(svg.match(/height="([0-9]+)"/)[1]),
canvas = document.createElement('canvas');
canvas.setAttribute('width', width);
canvas.setAttribute('height', height);
if (canvas.getContext && canvas.getContext('2d')) {
canvg(canvas, svg);
return canvas.toDataURL("image/jpeg");
}
else {
alert("Your browser doesn't support this feature, please use a modern browser");
return false;
}
}
}(Highcharts));
Then for the example i have set up export on a button click. This will look for all elements of a certain class (so choose one to add to all of your chart elements) and then call their highcharts.createCanvas function.
$('#export_all').click(function () {
var doc = new jsPDF();
// chart height defined here so each chart can be palced
// in a different position
var chartHeight = 80;
// All units are in the set measurement for the document
// This can be changed to "pt" (points), "mm" (Default), "cm", "in"
doc.setFontSize(40);
doc.text(35, 25, "My Exported Charts");
//loop through each chart
$('.myChart').each(function (index) {
var imageData = $(this).highcharts().createCanvas();
// add image to doc, if you have lots of charts,
// you will need to check if you have gone bigger
// than a page and do doc.addPage() before adding
// another image.
/**
* addImage(imagedata, type, x, y, width, height)
*/
doc.addImage(imageData, 'JPEG', 45, (index * chartHeight) + 40, 120, chartHeight);
});
//save with name
doc.save('demo.pdf');
});
important to note here that if you have lots of charts you will need to handle placing them on a new page. The documentation for jsPDF looks really outdated (they do have a good demos page though just not a lot to explain all the options possible), there is an addPage() function and then you can just play with widths and heights until you find something that works.
the last part is to just setup the graphs with an extra option to not display the export button on each graph that would normally display.
//charts
$('#chart1').highcharts({
navigation: {
buttonOptions: {
enabled: false
}
},
//this is just normal highcharts setup form here for two graphs see fiddle for full details
The result isn't too bad i'm impressed with the quality of the graphs as I wasn't expecting much from this, with some playing of the pdf positions and sizes could look really good.
Here is a screen shot showing the network requests made before and after the export, when the export is made no requests are made http://i.imgur.com/ppML6Gk.jpg
here is an example of what the pdf looks like http://i.imgur.com/6fQxLZf.png (looks better when view as actual pdf)
quick example to be tried on local https://github.com/leighquince/HighChartLocalExport
score:0
Maybe this link can help you out.
Try refer to the exporting properties (fallbackToExportServer: false) and the necessary file that need to be include (offline-exporting.js).
Whereas for the export all at once part, currently I myself also still trying. Will update here if any.
score:0
This question is a bit old but was something i was working on myself recently and had some trouble with it.
I used the jsPDF library: https://github.com/MrRio/jsPDF
Issues i ran into involved jsPDF not supporting the SVG image exported by the high chart + images being blurry and low quality.
Below is the solution I used to get two charts into one pdf document:
function createPDF() {
var doc = new jsPDF('p', 'pt', 'a4'); //Create pdf
if ($('#chart1').length > 0) {
var chartSVG = $('#chart1').highcharts().getSVG();
var chartImg = new Image();
chartImg.onload = function () {
var w = 762;
var h = 600;
var chartCanvas = document.createElement('canvas');
chartCanvas.width = w * 2;
chartCanvas.height = h * 2;
chartCanvas.style.width = w + 'px';
chartCanvas.style.height = h + 'px';
var context = chartCanvas.getContext('2d');
chartCanvas.webkitImageSmoothingEnabled = true;
chartCanvas.mozImageSmoothingEnabled = true;
chartCanvas.imageSmoothingEnabled = true;
chartCanvas.imageSmoothingQuality = "high";
context.scale(2, 2);
chartCanvas.getContext('2d').drawImage(chartImg, 0, 0, 762, 600);
var chartImgData = chartCanvas.toDataURL("image/png");
doc.addImage(chartImgData, 'png', 40, 260, 250, 275);
if ($('#chart2').length > 0) {
var chart2SVG = $('#chart2').highcharts().getSVG(),
chart2Img = new Image();
chart2Img.onload = function () {
var chart2Canvas = document.createElement('canvas');
chart2Canvas.width = w * 2;
chart2Canvas.height = h * 2;
chart2Canvas.style.width = w + 'px';
chart2Canvas.style.height = h + 'px';
var context = chart2Canvas.getContext('2d');
chart2Canvas.webkitImageSmoothingEnabled = true;
chart2Canvas.mozImageSmoothingEnabled = true;
chart2Canvas.imageSmoothingEnabled = true;
chart2Canvas.imageSmoothingQuality = "high";
context.scale(2, 2);
chart2Canvas.getContext('2d').drawImage(chart2Img, 0, 0, 762, 600);
var chart2ImgData = chart2Canvas.toDataURL("image/png");
doc.addImage(chart2ImgData, 'PNG', 300, 260, 250, 275);
doc.save('ChartReport.pdf');
}
chart2Img.src = "data:image/svg+xml;base64," + window.btoa(unescape(encodeURIComponent(chart2SVG)));
}
}
chartImg.src = "data:image/svg+xml;base64," + window.btoa(unescape(encodeURIComponent(chartSVG)));
}
}
scripts to include:
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.2.61/jspdf.min.js"></script>
score:1
You need to setup your own exporting server, locally like in the article
score:1
Here is an example using the library pdfmake:
html:
<div id="chart_exchange" style="width: 450px; height: 400px; margin: 0 auto"></div>
<button id="export">export</button>
<canvas id="chart_exchange_canvas" width="450" height="400" style="display: none;"></canvas>
javascript:
function drawInlineSVG(svgElement, canvas_id, callback) {
var can = document.getElementById(canvas_id);
var ctx = can.getContext('2d');
var img = new Image();
img.setAttribute('src', 'data:image/svg+xml;base64,' + btoa(unescape(encodeURIComponent(svgElement))));
img.onload = function() {
ctx.drawImage(img, 0, 0);
callback(can.toDataURL("image/png"));
}
}
full working code: https://jsfiddle.net/dimitrisscript/f6sbdsps/
Source: stackoverflow.com
Related Query
- Export Highcharts to PDF (using javascript and local server - no internet connection)
- Using PhantomJS to create HighCharts grahps server side for use in PDF creation (PHP) - results in exit code 11 from PHPs exec();
- Using html2canvas to render a highcharts chart to pdf doesn't work on IE and Firefox
- Query rails database, and update highcharts using Javascript
- Issues using highcharts node export server from ClojureScript - "0x03 error when performing chart generation"
- Weekly PDF generation using PHP and Highcharts
- How to update the highcharts.js to make the export server to my own ipaddress,instead of connect to the highcharts server?
- How do I export the content of a page as either jpg or pdf with Highcharts and scrollable data?
- Not able to render the charts in pdf using Wickedpdf and Highcharts in rails 2.3 app
- Highcharts Export server - Can't generate image using --options
- change marginLeft and marginRight from a javascript statment using highcharts
- Highcharts Export Chart To PDF on The Fly Using TCPDF
- Highcharts SVG Export from Python Server Side Code
- Highcharts resize chart size using custom export button and replace expand and collapse button dynamically
- Highcharts - Local export server installation issue (Tomcat Java)
- Calling Highcharts export to jpeg but the source is https and exporting is http (security warnings issued by browser)
- Unable to get highcharts image from highcharts export server using rails httparty
- Exporting Charts on server side with Highcharts and PhantomJS to make a custom PDF
- Server side chart .svg export with Highcharts and PhantomJS, error loading data from .csv file
- How to create a column range chart in Highcharts using range and navigator functions?
- Loading Highcharts via shim using RequireJS and maintaining jQuery dependency
- JavaScript - Export Div with SVG chart + HTML as and Image
- How do I dynamically change a data point in Highcharts using JavaScript
- Disable PDF and SVG download options in Highcharts
- Javascript Highcharts v3.0.5 - How to hide Y Axis Title when using multiple Y Axis
- JavaScript error when using Highcharts
- I can't make Highcharts phantomJs export server work
- Using Highcharts and displaying a message over or on the chart when there is no data
- Convert hours and minute to millisecond using javascript or jQuery
- Using HighCharts and DotNet.HighCharts to "Play" Multiple Series
More Query from same tag
- Highcharts: circle with image background
- Highcharts dual x axis column + line is adding ticks to secondary x axis
- Using local file instead of $.getJSON
- Highcharts dataLabel positioning issue in FF and IE
- Highcharts: is it possible to set up top border radius for the columns in Column chart?
- Dealing with large numbers on yAxis
- R highcharter plot points color based on value
- how do you call function to pull data automaticall to chart in highcharts
- Highcharts conditionally disable marker on hover
- How to avoid duplicate X time axis labels
- Change highlight behavior in Highcharts
- Highchart's heat-map plot not rendering correctly when x-axis is in time format (HH:MM)
- Network chart nodes relation nodes merging -> highcharts
- How do i enable scrollbar in highcharts in react
- How to convert HTML table to Chart?
- Ruby on Rails, JSON and Highcharts
- How to make rangeSelector do dataGrouping of multiple series
- Format highcharts date tooltip to display timezone
- Uncaught TypeError adding chart in markdown using Jekyll
- Highcharts fold/unfold waterfall
- How to differentiate the xAxis and yAxis plotbands in Highchart
- Highcharts - Multiple Y axis
- HighCharts, multiple charts on page call via function
- Highcharts Custom tooltips for multiple series
- Changing bar colors on data that is within the same series data
- R Highcharts heatmap with motion, but motion bar is overlapping with x axis
- Using a highstock chart with stacked series
- Highcharts isn't displaying the data
- Highcharts timeline series positioning
- How to remove the padding on the both ends of the HighChart