javascript - svg to canvas image in jspdf - I Get a black content -
i've used jspdf convert svg image canvas , pdf
the plugins i've used are
<script type="text/javascript" src="assets/js/jquery.js"></script> <script type="text/javascript" src="assets/js/jspdf.js"></script> <script type="text/javascript" src="assets/js/html2canvas.js"></script> <script type="text/javascript" src="assets/js/html2canvas.svg.js"></script> <script type="text/javascript" src="assets/js/canvg.js"></script>
my html code svg
<a href="javascript:genpdf();">genpdf</a> <svg class="bullet" width="416" height="50"> <g transform="translate(75,5)"> <rect class="range s0" width="301" height="25" x="0"/> <rect class="range s1" width="250.83333333333334" height="25" x="0"/> <rect class="range s2" width="200.66666666666666" height="25" x="0"/> <rect class="measure s0" width="230.76666666666668" height="8.333333333333334" x="0" y="8.333333333333334"/> <rect class="measure s1" width="210.7" height="8.333333333333334" x="0" y="8.333333333333334"/> <g class="tick" transform="translate(0,0)" style="opacity: 1;"> <line y1="25" y2="29.166666666666668"/> <text text-anchor="middle" dy="1em" y="29.166666666666668">0</text> </g> <g class="tick" transform="translate(50.16666793823242,0)" style="opacity: 1;"> <line y1="25" y2="29.166666666666668"/> <text text-anchor="middle" dy="1em" y="29.166666666666668">5</text> </g> <g class="tick" transform="translate(100.33333587646484,0)" style="opacity: 1;"> <line y1="25" y2="29.166666666666668"/> <text text-anchor="middle" dy="1em" y="29.166666666666668">10</text> </g> <g class="tick" transform="translate(150.5,0)" style="opacity: 1;"> <line y1="25" y2="29.166666666666668"/> <text text-anchor="middle" dy="1em" y="29.166666666666668">15</text> </g> <g class="tick" transform="translate(200.6666717529297,0)" style="opacity: 1;"> <line y1="25" y2="29.166666666666668"/> <text text-anchor="middle" dy="1em" y="29.166666666666668">20</text> </g> <g class="tick" transform="translate(250.8333282470703,0)" style="opacity: 1;"> <line y1="25" y2="29.166666666666668"/> <text text-anchor="middle" dy="1em" y="29.166666666666668">25</text> </g> <g class="tick" transform="translate(301,0)" style="opacity: 1;"> <line y1="25" y2="29.166666666666668"/> <text text-anchor="middle" dy="1em" y="29.166666666666668">30</text> </g> <g x="200" y="100" style="text-anchor: end;" transform="translate(-6,12.5)"> <text class="title">novora</text> </g> </g> </svg>
i'm unable exact image required output when generate pdf. i'm converting svg canvas still i'm unable desired output. instead of exact image black content inside that. css code above 1 is
.bullet { font: 10 px sans-serif; } .bullet.marker { stroke: #000; stroke-width: 2px; } .bullet .tick line { stroke: # 666; stroke-width: .5px; } .bullet.range.s0 { fill: #eee; } .bullet.range.s1 { fill: #ddd; } .bullet.range.s2 { fill: #ccc; } .bullet.measure.s0 { fill: steelblue; } .bullet.measure.s1 { fill: #fff; } .bullet.measure.s2 { fill: steelblue; } .bullet.measure.s3 { fill: lightsteelblue; } .bullet.title { font-size: 10px; font-weight: bold; } .bullet.subtitle { fill: #999; }
the javascript helps me rendering data is
function genpdf(){ var svgelements = $("#page-content-wrapper").find('svg'); //replace svgs temp canvas svgelements.each(function() { var canvas, xml; canvas = document.createelement("canvas"); canvas.classname = "screenshottempcanvas"; // canvas.getcontext("3d"); //convert svg xml string xml = (new xmlserializer()).serializetostring(this); // removing name space ie throws error xml = xml.replace(/xmlns=\"http:\/\/www\.w3\.org\/2000\/svg\"/, ''); //draw svg onto canvas canvg(canvas, xml); $(canvas).insertafter(this); $(this).addclass('temphide'); $(this).hide(); }); html2canvas(document.getelementbyid('page-content-wrapper'), { onrendered: function (canvas){ var img = canvas.todataurl("image/jpeg",1.0); var doc = new jspdf(); doc.addimage(img, 'jpeg' , 10, 10); doc.save('test.pdf'); } }); settimeout(function(){ $("#page-content-wrapper").find('.screenshottempcanvas').remove(); $("#page-content-wrapper").find('.temphide').show().removeclass('temphide'); $("#page-content-wrapper").find('.bullet').show(); },2000); } function getstyle(el, styleprop) { var camelize = function(str) { return str.replace(/\-(\w)/g, function(str, letter) { return letter.touppercase(); }); }; if (el.currentstyle) { return el.currentstyle[camelize(styleprop)]; } else if (document.defaultview && document.defaultview.getcomputedstyle) { return document.defaultview.getcomputedstyle(el, null) .getpropertyvalue(styleprop); } else { return el.style[camelize(styleprop)]; } }
please me out in this. thanks