- Getting Started
- Installation
- Key Concepts
- Integrations
- Account & Billing
- Security & Privacy
- PDF Generation
- Reference
- Tutorials
- Troubleshooting
- Excel Generation
- Reference
- Troubleshooting
How to create a PDF with a Highcharts Chart
Highcharts is one of the most popular charting libraries and is used in many DocRaptor PDFs. This tutorial walks through creating a PDF with a Highcharts pie chart. These same steps also apply to line charts, bubble charts, donut charts, or any other chart type—and even other charting libraries.
Enable JavaScript
First, enable JavaScript in the DocRaptor API by setting the javascript
parameter to true
. You can see this parameter defined in any of the complete code examples below the tutorial. DocRaptor disables JavaScript by default to speed up the generation time of documents that don't use JavaScript.
Disable Animation
Many charting library examples include animation by default because it looks impressive, but animation isn't supported by DocRaptor (or PDFs in general). Make sure animation is disabled in your chart; otherwise we may render your document before the chart has completed the animation
Write the HTML and CSS
Let's start by including the Highcharts JavaScript library:
<script src="https://code.highcharts.com/highcharts.js"></script>
Optionally. change the document layout to landscape and remove the default margin to make it a full bleed document (where the content goes to the edges of the document):
@page {
size: landscape;
margin: 0;
}
Most chart libraries, including Highcharts, require the chart's containing element to have an explicit height and width. As we're trying to make a full bleed document, we'll simply define the height and width as the size of DocRaptor's default document size, US-Letter:
<div id="container" style="height: 816px; width: 1056px;"></div>
Create the Chart
To make the actual chart, we'll use the Pie Chart Demo code straight from the Highcharts website:
<script>
Highcharts.chart('container', {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: 'Browser market shares January, 2015 to May, 2015'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: false
},
showInLegend: true
}
},
series: [{
name: 'Brands',
colorByPoint: true,
data: [{
name: 'Microsoft Internet Explorer',
y: 56.33
}, {
name: 'Chrome',
y: 24.03,
sliced: true,
selected: true
}, {
name: 'Firefox',
y: 10.38
}, {
name: 'Safari',
y: 4.77
}, {
name: 'Opera',
y: 0.91
}, {
name: 'Proprietary or Undetectable',
y: 0.2
}]
}]
});
</script>
Wait for JavaScript to Finish
Unlike a traditional browser, which is constantly running JavaScript, our PDF renderer must detect when the JavaScript is "finished." Unfortunately, it isn't easy to programmatically detect when charts finish rendering, especially charts with external/AJAX-based data sources.
We recommend using our docraptorJavaScriptFinished()
JavaScript function to explictly tell us when your chart library is finished. When the function returns true
, we'll generate your PDF. For the fastest rendering, you should detect the rendering of the chart element. For simplicity, we'll just wait a couple seconds before making this PDF, which is plenty of time for Highcharts to finish:
<script>
var didWait = false;
docraptorJavaScriptFinished = function() {
if (! didWait) {
setTimeout(function(){
didWait = true;
}, 2000);
return false;
}
return true;
}
</script>
See any of the below complete code samples to generate a Highcharts Pie Chart PDF in your programming language with DocRaptor's HTML-to-PDF API.
Using DocRaptor's API
Below are the basics of using DocRaptor's HTTP API. It can easily be used in any programming language, but if we don't have a native agent for your language, please let us know! That'll help us write the best examples and agents.
The API key shown here, YOUR_API_KEY_HERE
, actually works! Use it for testing without creating an account. The only requirement is test mode must be used (test
parameter set to true
).
To make documents, send a POST request to this URL:
https://YOUR_API_KEY_HERE@api.docraptor.com/docs
Send JSON-encoded API parameters (you'll need to set an HTTP header for Content-Type
as application/json
). These are the only required parameters:
{
"type": "pdf",
"document_content": "Hello World!"
}
Generating the Document with cURL
Here's a completely functional code sample for this tutorial:
cat <<-'DOCUMENT_OPTIONS' > docraptor_parameters.json
{
"test": true,
"document_type": "pdf",
"document_content": "<script src=\"https://code.highcharts.com/highcharts.js\"></script><div id=\"container\" style=\"height: 816px; width: 1056px;\"></div><script>Highcharts.chart('container', {chart: {plotBackgroundColor: null,plotBorderWidth: null,plotShadow: false,type: 'pie'},title: {text: 'Browser market shares January, 2015 to May, 2015'},tooltip: {pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'},plotOptions: {pie: {allowPointSelect: true,cursor: 'pointer',dataLabels: {enabled: false},showInLegend: true}},series: [{name: 'Brands',colorByPoint: true,data: [{name: 'Microsoft Internet Explorer',y: 56.33}, {name: 'Chrome',y: 24.03,sliced: true,selected: true}, {name: 'Firefox',y: 10.38}, {name: 'Safari',y: 4.77}, {name: 'Opera',y: 0.91}, {name: 'Proprietary or Undetectable',y: 0.2}]}],accessibility: {enabled: false}});var didWait = false;docraptorJavaScriptFinished = function() {if (! didWait) {setTimeout(function(){didWait = true;}, 2000);return false;}return true;}</script><style>@page {size: landscape;margin: 0;}</style>",
"javascript": true
}
DOCUMENT_OPTIONS
curl https://YOUR_API_KEY_HERE@api.docraptor.com/docs \
--fail --silent --show-error \
--header "Content-Type:application/json" \
--data @docraptor_parameters.json \
> highcharts-chart.pdf
Downloads
Here are all the files you need to follow this tutorial:
Related Tutorials & Documentation
See the complete PDF tutorial list or review the documentation for additional info, such as generating asynchronous documents.