score:1
Accepted answer
I have fixed these problems
- add id
.attr("id","svg-container")
- use svg size instead of canvas default size
and its working well
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.0/d3.min.js"></script>
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous">
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="https://canvg.github.io/canvg/canvg.js"></script>
<script type="text/javascript" src="https://canvg.github.io/canvg/rgbcolor.js"></script>
<script type="text/javascript" src="https://canvg.github.io/canvg/StackBlur.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.4/jspdf.min.js"></script>
</head>
<body>
<table align="center">
<caption style="text-align: left; font-weight: bold">DETTE ER EN FAKTURABRIK</caption>
<br>
<tr>
<td id="str1">Kategori 1</td>
<td id="nr1">10000</td>
</tr>
<tr>
<td id="str2">Kategori 2</td>
<td id="nr2">20000</td>
</tr>
<tr>
<td id="str3">Kategori 3</td>
<td id="nr3">30000</td>
</tr>
<tr>
<td id="str4">Kategori 4</td>
<td id="nr4">40000</td>
</tr>
<tr>
<td id="str5">Kategori 5</td>
<td id="nr5">50000</td>
</tr>
<tr>
<td id="str6">Kategori 6</td>
<td id="nr6">60000</td>
</tr>
<tr>
<td id="str7">Kategori 7</td>
<td id="nr7">70000</td>
</tr>
<tr>
<td id="str8">Kategori 8</td>
<td id="nr8">80000</td>
</tr>
</table>
<div><button id="getPdf" style="display:block;padding : 20px;width:100%;margin:10px;font-weight:bold;background:#f88">Get PDF</button></div>
<script>//Add event listener
document.getElementById("getPdf").addEventListener("click", getPdf);
function getPdf() {
//Get svg markup as string
var svgElement = document.getElementById('svg-container');
var svg = svgElement.innerHTML;
if (svg) svg = svg.replace(/\r?\n|\r/g, '').trim();
var canvas = document.createElement('canvas');
canvas.width = svgElement.clientWidth;
canvas.height = svgElement.clientHeight;
var context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width , canvas.height);
canvg(canvas, svg);
var imgData = canvas.toDataURL('image/png');
// Generate PDF
var doc = new jsPDF('p', 'pt', 'a4');
doc.addImage(imgData, 'PNG', 40, 40, 100, 100);
//doc.text(20, 20, 'Hello world!');
doc.save('save', 'test.pdf');
}
var data = [
$("#nr1").text(),
$("#nr2").text(),
$("#nr3").text(),
$("#nr4").text(),
$("#nr5").text(),
$("#nr6").text(),
$("#nr7").text(),
$("#nr8").text()];
var margin = {top: 20, right: 10, bottom: 30, left: 70},
width = 800 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var y = d3.scale.linear()
.domain([0,100000])
.range([height, 0]);
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1)
.domain(d3.entries(data).map(function(d) { return "Kategori " + d.key; }));
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var svg = d3.select("body").append("svg").attr("id","svg-container")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(-6," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Kroner");
var bar = svg.selectAll()
.data(d3.entries(data))
.enter().append("rect")
.attr("x", function(d) { return x("Kategori " + d.key) + x.rangeBand()/2 })
.attr("width", x.rangeBand()/2)
.attr("y", function(d) { return y(d.value ); })
.attr("height", function(d) { return height - y(d.value); })
.style( "fill", "#66a3ff" );
bar.append("text")
.attr("x", function(d) { return x("Kategori " + d.key) + x.rangeBand() / 2; })
.attr("dx", -3) // padding-right
.attr("dy", ".35em") // vertical-align: middle
.attr("text-anchor", "end") // text-align: right
.text( function(d) { return d.value; });
</script>
</body>
</html>
Source: stackoverflow.com
Related Query
- D3.js SVG giving it an ID or adding the SVG to PDF
- How to get SVG path data of TopoJSON feature without adding it to the DOM?
- How to access the DOM element that correlates to a D3 SVG object?
- How do I get the width of an svg element using d3js?
- d3.js - How can I set the cursor to hand when mouseover these elements on SVG container?
- clicking a node in d3 from a button outside the svg
- D3 how to change the width and length of SVG
- In d3, how to get the interpolated line data from a SVG line?
- Using ViewBox to resize svg depending on the window size
- SVG element loses event handlers if moved around the DOM
- nvd3.js : unable to bind onClick event with the data points in the svg
- maximum number of svg elements for the browser in a map
- mouseout/mouseleave gets fired when mouse moves INSIDE the svg path element
- cx, cy vs transform in svg and D3, what is the difference?
- D3.js: Pie graph, adding a border only to the outter region
- How to update the fill color on existing svg elements with d3.js?
- Adding text to the center of a D3 Donut Graph
- adding padding to svg g element
- d3.js: Transition the colours in an SVG linear gradient?
- D3, SVG and textPath: How to move the text down?
- Adding a horizontal line to d3 graph displays at the wrong value
- D3 update on node removal always remove the last entry in SVG DOM
- How do I associate SVG elements generated by graphviz to elements in the DOT source code
- How can I set the each line color or width in SVG path
- How to use SVG gradients to display varying colors relative to the size of the colored region
- Adding a filter in dc.js / Crossfilter not updating the chart
- Loading multiple CSV in DC.js, adding a value, and concatenating the results into a single dataTable
- Draw an svg arc using d3 knowing 3 points on the arc
- How can I display a placeholder image in my SVG until the real image is loaded?
- Adding label to the links in D3 force directed graph
More Query from same tag
- display info into tooltip
- Setting a color for click event on a d3 map
- d3.js equivalent to $(this)
- How to create sorted square graph using javascript
- Syntax to import a d3 module using webpack
- Make jQuery mousemove stop
- what is this extra 'parse' call doing? (D3 time formatting)
- d3: import JSON locally while keeping the data separate from my SVG creation
- D3 CSV Import With "%"
- Integrating tabletop.js with d3.js?
- {d3.js} migration v3 to v4: code work on v3 (d3.layout.stack()) error v4 (d3.stack())
- Animating circles along multiple paths
- Integrating d3 with meteor? Trying to draw pie charts
- tree.nodeSize() not working in D3 tree graph to inc. the space between nodes
- add different shapes to d3 force layout
- Remove comma delimitter along xaxis D3
- Resetting filters before adding new ones
- converting to unix timestamp to timestamp in iso format using d3.js
- How can I remove or replace SVG content?
- How to disable some streams by default in a nvd3 Simple Line Chart?
- D3.js line chart with negative values
- Programatically disable series on nvd3 Horizontal Multi-Bar Chart
- Append d3.gantt chart to a selected div
- yScale not acting consistently d3.js
- D3 Undefined Attribute in Tooltip
- Simple d3/dimple drill down example with bar chart
- Dynamically new dimensions with D3 JS parallel sets
- How to save JSON data built in a d3.js script
- Angular-NVD3 Stacked Bar Chart With Line
- Remove outline along the axes in d3.js area chart