Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wkhtmltopdf failure when embed an SVG

Has anyone in this vast space has ever had the luck to successfully create a PDF with an embedded SVG on an HTML? I've been receiving segmentation fault all the time.

Or perhaps is there any other way to embed an SVG into an HTML file and then export it to PDF instead of wkhtmltopdf?

like image 216
Jeebsion Avatar asked Sep 12 '12 20:09

Jeebsion


2 Answers

I had similar problem. Seems like javascript embedded in SVG image can cause segmentation fault.

I was generating SVG graphs using pygal Python module. To successfully generate PDF from HTML with SVG graphs I had to do several things:

  1. Remove reference to javascript. (In case of pygal add js=() key to a graph constructor).
  2. Specify image size in svg tag, like

    <svg ... width="300" height="200">
    

    (In case of pygal use explicit_size keywoard)

  3. Embed SVG image into img tag in base64 encoded form, like

    <img src="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0...">
    

I was using 11th version of wkhtmltopdf.

like image 199
Yuriy Chemerys Avatar answered Sep 18 '22 06:09

Yuriy Chemerys


If this fix doesn't work for others, here's what worked for me with chartist.js, and wkhtmltopdf 0.12.2.1 under Ubuntu 64. (Credit to Panokev)

Add this to your javascript before all other JS.

{

Function.prototype.bind = Function.prototype.bind || function (thisp) {
    var fn = this;
    return function () {
        return fn.apply(thisp, arguments);
    };
};

Definitively set width style for chart div, for example - style="width:950px;"

like image 43
Phil McCarty Avatar answered Sep 21 '22 06:09

Phil McCarty